From 19ef5e2cdbad996da58787c0fb22a35ab3dd59e8 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 11:44:56 +0200 Subject: Fixed comment and post getting and updating --- src/DevHive.Data/Repositories/CommentRepository.cs | 23 ++++++++++++++++++++ src/DevHive.Data/Repositories/PostRepository.cs | 25 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index d33b7bf..8ddc628 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -17,6 +17,14 @@ namespace DevHive.Data.Repositories } #region Read + public override async Task GetByIdAsync(Guid id) + { + return await this._context.Comments + .Include(x => x.Creator) + .Include(x => x.Post) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments @@ -25,6 +33,21 @@ namespace DevHive.Data.Repositories } #endregion + #region Update + public override async Task EditAsync(Guid id, Comment newEntity) + { + Comment comment = await this.GetByIdAsync(id); + + this._context + .Entry(comment) + .CurrentValues + .SetValues(newEntity); + + return await this.SaveChangesAsync(this._context); + } + #endregion + + #region Validations public async Task DoesCommentExist(Guid id) { diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 78b40cd..07b5875 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -23,6 +23,7 @@ namespace DevHive.Data.Repositories { return await this._context.Posts .Include(x => x.Comments) + .Include(x => x.Creator) .FirstOrDefaultAsync(x => x.Id == id); } @@ -39,6 +40,30 @@ namespace DevHive.Data.Repositories } #endregion + #region Update + public override async Task EditAsync(Guid id, Post newEntity) + { + Post post = await this.GetByIdAsync(id); + + this._context + .Entry(post) + .CurrentValues + .SetValues(newEntity); + + post.FileUrls.Clear(); + foreach(var fileUrl in newEntity.FileUrls) + post.FileUrls.Add(fileUrl); + + post.Comments.Clear(); + foreach(var comment in newEntity.Comments) + post.Comments.Add(comment); + + this._context.Entry(post).State = EntityState.Modified; + + return await this.SaveChangesAsync(this._context); + } + #endregion + #region Validations public async Task DoesPostExist(Guid postId) { -- cgit v1.2.3 From 498af41f38b14372bd2f5eb9a0add7af95c40168 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 13:07:54 +0200 Subject: Fixed GetUserPosts implementation --- .../Interfaces/Repositories/IFeedRepository.cs | 1 + src/DevHive.Data/Repositories/FeedRepository.cs | 13 ++++++++++++ src/DevHive.Services/Interfaces/IFeedService.cs | 1 + src/DevHive.Services/Services/FeedService.cs | 24 ++++++++++++++++++++++ src/DevHive.Web/Controllers/FeedController.cs | 2 +- 5 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs index e9fd48a..7262510 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs @@ -8,5 +8,6 @@ namespace DevHive.Data.Interfaces.Repositories public interface IFeedRepository { Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize); + Task> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize); } } diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs index efcb8e0..7ab9a91 100644 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -32,5 +32,18 @@ namespace DevHive.Data.Repositories return posts; } + + public async Task> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize) + { + List posts = await this._context.Posts + .Where(post => post.TimeCreated < firstRequestIssued) + .Where(p => p.Creator.Id == user.Id) + .OrderByDescending(x => x.TimeCreated) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + + return posts; + } } } diff --git a/src/DevHive.Services/Interfaces/IFeedService.cs b/src/DevHive.Services/Interfaces/IFeedService.cs index 1edba5a..b507b3b 100644 --- a/src/DevHive.Services/Interfaces/IFeedService.cs +++ b/src/DevHive.Services/Interfaces/IFeedService.cs @@ -6,5 +6,6 @@ namespace DevHive.Services.Interfaces public interface IFeedService { Task GetPage(GetPageServiceModel getPageServiceModel); + Task GetUserPage(GetPageServiceModel model); } } diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs index 269471e..ceb5ebf 100644 --- a/src/DevHive.Services/Services/FeedService.cs +++ b/src/DevHive.Services/Services/FeedService.cs @@ -54,5 +54,29 @@ namespace DevHive.Services.Services return readPageServiceModel; } + + public async Task GetUserPage(GetPageServiceModel model) { + User user = null; + + if (!string.IsNullOrEmpty(model.Username)) + user = await this._userRepository.GetByUsernameAsync(model.Username); + else + throw new ArgumentException("Invalid given data!"); + + if (user == null) + throw new ArgumentException("User doesn't exist!"); + + List posts = await this._feedRepository + .GetUsersPosts(user, model.FirstRequestIssued, model.PageNumber, model.PageSize); + + if (posts.Count <= 0) + throw new ArgumentException("User hasn't posted anything yet!"); + + ReadPageServiceModel readPageServiceModel = new(); + foreach (Post post in posts) + readPageServiceModel.Posts.Add(this._mapper.Map(post)); + + return readPageServiceModel; + } } } diff --git a/src/DevHive.Web/Controllers/FeedController.cs b/src/DevHive.Web/Controllers/FeedController.cs index 4fd3ae9..b04c37a 100644 --- a/src/DevHive.Web/Controllers/FeedController.cs +++ b/src/DevHive.Web/Controllers/FeedController.cs @@ -43,7 +43,7 @@ namespace DevHive.Web.Controllers GetPageServiceModel getPageServiceModel = this._mapper.Map(getPageWebModel); getPageServiceModel.Username = username; - ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageServiceModel readPageServiceModel = await this._feedService.GetUserPage(getPageServiceModel); ReadPageWebModel readPageWebModel = this._mapper.Map(readPageServiceModel); return new OkObjectResult(readPageWebModel); -- cgit v1.2.3 From 5e07474a813e82dd2071c5d9d233beccbe6a8430 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 21:15:55 +0200 Subject: Fixed ordering of posts in feed repository --- src/DevHive.Data/Repositories/FeedRepository.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs index 7ab9a91..d8170d0 100644 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -25,11 +25,14 @@ namespace DevHive.Data.Repositories List posts = await this._context.Posts .Where(post => post.TimeCreated < firstRequestIssued) .Where(p => friendsIds.Contains(p.Creator.Id)) - .OrderByDescending(x => x.TimeCreated) .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ToListAsync(); + // Ordering by descending can't happen in query, because it doesn't order it + // completely correctly (example: in query these two times are ordered + // like this: 2021-01-30T11:49:45, 2021-01-28T21:37:40.701244) + posts = posts.OrderByDescending(x => x.TimeCreated.ToFileTime()).ToList(); return posts; } @@ -38,11 +41,12 @@ namespace DevHive.Data.Repositories List posts = await this._context.Posts .Where(post => post.TimeCreated < firstRequestIssued) .Where(p => p.Creator.Id == user.Id) - .OrderByDescending(x => x.TimeCreated) .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ToListAsync(); + // Look at GetFriendsPosts on why this is done like this + posts = posts.OrderByDescending(x => x.TimeCreated.ToFileTime()).ToList(); return posts; } } -- cgit v1.2.3