From 7d2e70fd3770bf7c8c206f7affa860c06de6b271 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 7 Dec 2022 21:15:58 +0100 Subject: Prosiren model korisnika sa favourite. Dodavanje favourite objave i kod korisnika i kod objave. --- Backend/Api/Api/Models/User.cs | 4 +++- Backend/Api/Api/Services/PostService.cs | 6 ++++++ Backend/Api/Api/Services/UserService.cs | 10 +++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Models/User.cs b/Backend/Api/Api/Models/User.cs index cf16dbe..52d0f24 100644 --- a/Backend/Api/Api/Models/User.cs +++ b/Backend/Api/Api/Models/User.cs @@ -21,6 +21,7 @@ namespace Api.Models public List following { get; set; } public int followersCount { get; set; } public int followingCount { get; set; } + public List? favourites { get; set; } } public class Login @@ -68,7 +69,8 @@ namespace Api.Models public List following { get; set; } public int followersCount { get; set; } - public int followingCount { get; set; } + public int followingCount { get; set; } + public List? favourites { get; set; } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 6d28206..cb59330 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -557,22 +557,28 @@ namespace Api.Services { string userId = _httpContext.HttpContext.User.FindFirstValue("id"); var result = false; + var user = await _users.Find(x => x._id == userId).FirstOrDefaultAsync(); Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync(); if (userId == null || post==null) return result; if (post.favourites == null) post.favourites = new List(); + if(user.favourites==null) + user.favourites= new List(); if (post.favourites.Contains(userId)) { post.favourites.Remove(userId); + user.favourites.Remove(post._id); result = false; } else { post.favourites.Add(userId); + user.favourites.Add(post._id); result = true; } + await _users.ReplaceOneAsync(x => x._id == user._id, user); await _posts.ReplaceOneAsync(x => x._id == postId, post); return result; diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 781afa8..2b3f0b8 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -17,9 +17,7 @@ namespace Api.Services private readonly IMongoCollection _posts; private readonly IJwtService _jwtService; private IConfiguration _configuration; - private readonly IFileService _fileService; - - private readonly IMongoCollection _usersSend; + private readonly IFileService _fileService; public UserService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IFileService fileService) { var database = mongoClient.GetDatabase(settings.DatabaseName); @@ -364,6 +362,7 @@ namespace Api.Services tosend.followingCount = user.followingCount; tosend.followers = user.followers; tosend.following = user.following; + tosend.favourites = user.favourites; var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync(); tosend.postcount = userposts.Count(); return tosend; @@ -384,6 +383,7 @@ namespace Api.Services tosend.followingCount = user.followingCount; tosend.followers = user.followers; tosend.following = user.following; + tosend.favourites = user.favourites; var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync(); tosend.postcount = userposts.Count(); return tosend; @@ -476,6 +476,7 @@ namespace Api.Services follower.followers = utemp.followers; follower.followersCount = utemp.followersCount; follower.followingCount = utemp.followingCount; + follower.favourites = utemp.favourites; follower._id = utemp._id; @@ -515,6 +516,7 @@ namespace Api.Services follower._id = utemp._id; follower.followersCount = utemp.followersCount; follower.followingCount = utemp.followingCount; + follower.favourites = utemp.favourites; following.Add((UserSend)follower); } @@ -558,6 +560,7 @@ namespace Api.Services following._id = utemp._id; following.followersCount = utemp.followersCount; following.followingCount = utemp.followingCount; + following.favourites=utemp.favourites; myFollowings.Add((UserSend)following); } @@ -672,6 +675,7 @@ namespace Api.Services follower.followersCount = utemp.followersCount; follower.followingCount = utemp.followingCount; follower._id = utemp._id; + follower.favourites = utemp.favourites; myfollowers.Add((UserSend)follower); } return myfollowers; -- cgit v1.2.3