From 278130d86378a6b2db6ba443631f303fb7d7e207 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 30 Dec 2020 21:21:49 +0200 Subject: Implemented Posts and merged Comment to Post --- src/DevHive.Services/Services/PostService.cs | 98 ++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/DevHive.Services/Services/PostService.cs (limited to 'src/DevHive.Services/Services/PostService.cs') diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs new file mode 100644 index 0000000..0c0fd5c --- /dev/null +++ b/src/DevHive.Services/Services/PostService.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Services +{ + public class PostService + { + private readonly PostRepository _postRepository; + private readonly IMapper _postMapper; + + public PostService(PostRepository postRepository, IMapper postMapper) + { + this._postRepository = postRepository; + this._postMapper = postMapper; + } + + //Create + public async Task CreatePost(CreatePostServiceModel postServiceModel) + { + Post post = this._postMapper.Map(postServiceModel); + + return await this._postRepository.AddAsync(post); + } + + public async Task AddComment(CreateCommentServiceModel commentServiceModel) + { + commentServiceModel.TimeCreated = DateTime.Now; + Comment comment = this._postMapper.Map(commentServiceModel); + + bool result = await this._postRepository.AddCommentAsync(comment); + + return result; + } + + //Read + public async Task GetPostById(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Post does not exist!"); + + return this._postMapper.Map(post); + } + + public async Task GetCommentById(Guid id) + { + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + + if(comment == null) + throw new ArgumentException("The comment does not exist"); + + return this._postMapper.Map(comment); + } + + //Update + public async Task UpdatePost(UpdatePostServiceModel postServiceModel) + { + if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId)) + throw new ArgumentException("Comment does not exist!"); + + Post post = this._postMapper.Map(postServiceModel); + return await this._postRepository.EditAsync(post); + } + + public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) + { + if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._postMapper.Map(commentServiceModel); + bool result = await this._postRepository.EditCommentAsync(comment); + + return result; + } + + //Delete + public async Task DeletePost(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id); + return await this._postRepository.DeleteAsync(post); + } + + public async Task DeleteComment(Guid id) + { + if (!await this._postRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + bool result = await this._postRepository.DeleteCommentAsync(comment); + + return result; + } + } +} \ No newline at end of file -- cgit v1.2.3