From ff91162eb83dcf19402240ae8fa06f70cbf2b9e0 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 11:31:21 +0200 Subject: Separated comment models, controler and service from post's --- src/DevHive.Services/Interfaces/ICommentService.cs | 20 ++++++++++++++++++++ src/DevHive.Services/Interfaces/IPostService.cs | 10 ++-------- 2 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 src/DevHive.Services/Interfaces/ICommentService.cs (limited to 'src/DevHive.Services/Interfaces') diff --git a/src/DevHive.Services/Interfaces/ICommentService.cs b/src/DevHive.Services/Interfaces/ICommentService.cs new file mode 100644 index 0000000..e7409a8 --- /dev/null +++ b/src/DevHive.Services/Interfaces/ICommentService.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Comment; + +namespace DevHive.Services.Interfaces +{ + public interface ICommentService + { + Task AddComment(CreateCommentServiceModel createPostServiceModel); + + Task GetCommentById(Guid id); + + Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); + + Task DeleteComment(Guid id); + + Task ValidateJwtForCreating(Guid userId, string rawTokenData); + Task ValidateJwtForComment(Guid commentId, string rawTokenData); + } +} diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 71b558c..d35acfd 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -1,26 +1,20 @@ using System; using System.Threading.Tasks; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Interfaces { - public interface IPostService + public interface IPostService { Task CreatePost(CreatePostServiceModel createPostServiceModel); - Task AddComment(CreateCommentServiceModel createPostServiceModel); Task GetPostById(Guid id); - Task GetCommentById(Guid id); Task UpdatePost(UpdatePostServiceModel updatePostServiceModel); - Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); Task DeletePost(Guid id); - Task DeleteComment(Guid id); Task ValidateJwtForCreating(Guid userId, string rawTokenData); Task ValidateJwtForPost(Guid postId, string rawTokenData); - Task ValidateJwtForComment(Guid commentId, string rawTokenData); } } -- 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.Services/Interfaces') 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