aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs6
-rw-r--r--Backend/Api/Api/Interfaces/IPostService.cs1
-rw-r--r--Backend/Api/Api/Models/Post.cs2
-rw-r--r--Backend/Api/Api/Services/PostService.cs25
4 files changed, 34 insertions, 0 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs
index 03c3f81..01a1c64 100644
--- a/Backend/Api/Api/Controllers/PostController.cs
+++ b/Backend/Api/Api/Controllers/PostController.cs
@@ -186,5 +186,11 @@ namespace Api.Controllers
var userid = await _userService.UserIdFromJwt();
return Ok(await _postService.Recommended(userid));
}
+ [HttpGet("favourite/{id}")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<bool>> addRemoveFavourite(string id)
+ {
+ return Ok(await _postService.addRemoveFavourite(id));
+ }
}
}
diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs
index 96786bd..d5f489e 100644
--- a/Backend/Api/Api/Interfaces/IPostService.cs
+++ b/Backend/Api/Api/Interfaces/IPostService.cs
@@ -27,5 +27,6 @@ namespace Api.Interfaces
Task<List<PostSend>> Get10Newest();
Task<List<PostSend>> Recommended(string userid);
+ Task<Boolean> addRemoveFavourite(string postId);
}
} \ No newline at end of file
diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs
index dd007ec..dbe7952 100644
--- a/Backend/Api/Api/Models/Post.cs
+++ b/Backend/Api/Api/Models/Post.cs
@@ -19,6 +19,7 @@ namespace Api.Models
public List<Comment> comments { get; set; }
public List<File> images { get; set; }
public List<string>? tags { get; set; }
+ public List<string>? favorites { get; set; }
}
public class PostReceive
@@ -43,6 +44,7 @@ namespace Api.Models
public List<File> images { get; set; }
public List<string>? tags { get; set; }
public DateTime? lastViewed { get; set; }
+ public List<string>? favorites { get; set; }
}
public class Rating
{
diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs
index 855b231..17c96e3 100644
--- a/Backend/Api/Api/Services/PostService.cs
+++ b/Backend/Api/Api/Services/PostService.cs
@@ -530,5 +530,30 @@ namespace Api.Services
taggedposts = fiveoftop5tags.Distinct().OrderByDescending(x => x.createdAt).ToList();
return taggedposts;
}
+ public async Task<Boolean> addRemoveFavourite(string postId)
+ {
+ string userId = _httpContext.HttpContext.User.FindFirstValue("id");
+ var result = false;
+ Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync();
+ if (userId == null || post==null)
+ return result;
+ if (post.favorites == null)
+ post.favorites = new List<string>();
+ if (post.favorites.Contains(userId))
+ {
+ post.favorites.Remove(userId);
+ result = false;
+ }
+ else
+ {
+ post.favorites.Add(userId);
+ result = true;
+
+ }
+ await _posts.ReplaceOneAsync(x => x._id == postId, post);
+ return result;
+
+ }
}
+
}