From f2e21e0e496a050e9b8b768cdc873668c5fffab4 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 3 Dec 2022 19:45:50 +0100 Subject: Prosiren model za objave. Omoguceno dodavanje/brisanje favourite u post servisu i post controlleru. --- Backend/Api/Api/Controllers/PostController.cs | 6 ++++++ Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Models/Post.cs | 2 ++ Backend/Api/Api/Services/PostService.cs | 25 +++++++++++++++++++++++++ 4 files changed, 34 insertions(+) (limited to 'Backend/Api') 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> 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> Get10Newest(); Task> Recommended(string userid); + Task 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 comments { get; set; } public List images { get; set; } public List? tags { get; set; } + public List? favorites { get; set; } } public class PostReceive @@ -43,6 +44,7 @@ namespace Api.Models public List images { get; set; } public List? tags { get; set; } public DateTime? lastViewed { get; set; } + public List? 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 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(); + 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; + + } } + } -- cgit v1.2.3