diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-23 22:34:43 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-23 22:34:43 +0200 |
| commit | e01a81954e0fba2c4521e03a76f48a970a87994f (patch) | |
| tree | d2fdacbed68de0f4f32b1d949d75bc1de9f861d3 /src | |
| parent | fac1bf2d1772e60cbecc1cf0381c6063a0e97ffd (diff) | |
| download | DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.tar DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.tar.gz DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.zip | |
All Post&Comment Implemented; Initializing testing...
Diffstat (limited to 'src')
11 files changed, 195 insertions, 167 deletions
diff --git a/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs new file mode 100644 index 0000000..b80c5a0 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface ICommentRepository : IRepository<Comment> + { + Task<bool> DoesCommentExist(Guid id); + Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs index 7a9c02e..aa0afc7 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs @@ -7,18 +7,7 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IPostRepository : IRepository<Post> { - Task<bool> AddCommentAsync(Comment entity); - - Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); - - Task<Comment> GetCommentByIdAsync(Guid id); - Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); - - Task<bool> EditCommentAsync(Comment newEntity); - - Task<bool> DeleteCommentAsync(Comment entity); - Task<bool> DoesCommentExist(Guid id); - + Task<Post> GetPostByCreatorAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); Task<bool> DoesPostExist(Guid postId); } } diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs new file mode 100644 index 0000000..880631a --- /dev/null +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class CommentRepository : BaseRepository<Comment>, ICommentRepository + { + private readonly DevHiveContext _context; + + public CommentRepository(DevHiveContext context) + : base(context) + { + this._context = context; + } + + #region Read + public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + { + return await this._context.Comments + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); + } + #endregion + + #region Validations + public async Task<bool> DoesCommentExist(Guid id) + { + return await this._context.Comments + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + #endregion + } +} diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 9230a2e..a79eacf 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -16,56 +16,13 @@ namespace DevHive.Data.Repositories this._context = context; } - #region Create - public async Task<bool> AddCommentAsync(Comment entity) - { - await this._context.Comments - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + public async Task<Post> GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) { return await this._context.Posts - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + .FirstOrDefaultAsync(p => p.CreatorId == creatorId && p.TimeCreated == timeCreated); } - - public async Task<Comment> GetCommentByIdAsync(Guid id) - { - return await this._context.Comments - .FindAsync(id); - } - - public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) - { - return await this._context.Comments - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && - p.TimeCreated == timeCreated); - } - #endregion - - #region Update - public async Task<bool> EditCommentAsync(Comment newEntity) - { - this._context.Comments - .Update(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task<bool> DeleteCommentAsync(Comment entity) - { - this._context.Comments - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } #endregion #region Validations @@ -75,13 +32,6 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(r => r.Id == postId); } - - public async Task<bool> DoesCommentExist(Guid id) - { - return await this._context.Comments - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } #endregion } } diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 4364c67..37c3354 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -7,18 +7,19 @@ namespace DevHive.Services.Interfaces { public interface IPostService { - Task<Guid> CreatePost(CreatePostServiceModel postServiceModel); - Task<Guid> AddComment(CreateCommentServiceModel commentServiceModel); + Task<Guid> CreatePost(CreatePostServiceModel createPostServiceModel); + Task<Guid> AddComment(CreateCommentServiceModel createPostServiceModel); - Task<CommentServiceModel> GetCommentById(Guid id); - Task<PostServiceModel> GetPostById(Guid id); + Task<ReadPostServiceModel> GetPostById(Guid id); + Task<ReadCommentServiceModel> GetCommentById(Guid id); - Task<bool> UpdateComment(UpdateCommentServiceModel commentServiceModel); - Task<bool> UpdatePost(UpdatePostServiceModel postServiceModel); + Task<Guid> UpdatePost(UpdatePostServiceModel updatePostServiceModel); + Task<Guid> UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); - Task<bool> DeleteComment(Guid id); Task<bool> DeletePost(Guid id); + Task<bool> DeleteComment(Guid id); + Task<bool> ValidateJwtForPost(Guid postId, string rawTokenData); Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 2df3b41..377fe05 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -15,117 +15,141 @@ namespace DevHive.Services.Services { public class PostService : IPostService { - private readonly IPostRepository _postRepository; private readonly IUserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly ICommentRepository _commentRepository; private readonly IMapper _postMapper; - public PostService(IPostRepository postRepository, IUserRepository userRepository, IMapper postMapper) + public PostService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) { - this._postRepository = postRepository; this._userRepository = userRepository; + this._postRepository = postRepository; + this._commentRepository = commentRepository; this._postMapper = postMapper; } - //Create - public async Task<Guid> CreatePost(CreatePostServiceModel postServiceModel) + #region Create + public async Task<Guid> CreatePost(CreatePostServiceModel createPostServiceModel) { - Post post = this._postMapper.Map<Post>(postServiceModel); + Post post = this._postMapper.Map<Post>(createPostServiceModel); + post.TimeCreated = DateTime.Now; bool success = await this._postRepository.AddAsync(post); - if (success) { - Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated); + Post newPost = await this._postRepository + .GetPostByCreatorAndTimeCreatedAsync(createPostServiceModel.IssuerId, createPostServiceModel.TimeCreated); + return newPost.Id; } else return Guid.Empty; } - public async Task<Guid> AddComment(CreateCommentServiceModel commentServiceModel) + public async Task<Guid> AddComment(CreateCommentServiceModel createCommentServiceModel) { - commentServiceModel.TimeCreated = DateTime.Now; - Comment comment = this._postMapper.Map<Comment>(commentServiceModel); + if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); - bool success = await this._postRepository.AddCommentAsync(comment); + Comment comment = this._postMapper.Map<Comment>(createCommentServiceModel); + createCommentServiceModel.TimeCreated = DateTime.Now; + bool success = await this._commentRepository.AddAsync(comment); if (success) { - Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated); + Comment newComment = await this._commentRepository + .GetCommentByIssuerAndTimeCreatedAsync(createCommentServiceModel.IssuerId, createCommentServiceModel.TimeCreated); + return newComment.Id; } else return Guid.Empty; } + #endregion - //Read - public async Task<PostServiceModel> GetPostById(Guid id) + #region Read + public async Task<ReadPostServiceModel> GetPostById(Guid id) { - Post post = await this._postRepository.GetByIdAsync(id) - ?? throw new ArgumentException("Post does not exist!"); + Post post = await this._postRepository.GetByIdAsync(id) ?? + throw new ArgumentException("The post does not exist!"); - return this._postMapper.Map<PostServiceModel>(post); + return this._postMapper.Map<ReadPostServiceModel>(post); } - public async Task<CommentServiceModel> GetCommentById(Guid id) + public async Task<ReadCommentServiceModel> GetCommentById(Guid id) { - Comment comment = await this._postRepository.GetCommentByIdAsync(id); - - if (comment == null) + Comment comment = await this._commentRepository.GetByIdAsync(id) ?? throw new ArgumentException("The comment does not exist"); - return this._postMapper.Map<CommentServiceModel>(comment); + return this._postMapper.Map<ReadCommentServiceModel>(comment); } + #endregion - //Update - public async Task<bool> UpdatePost(UpdatePostServiceModel postServiceModel) + #region Update + public async Task<Guid> UpdatePost(UpdatePostServiceModel updatePostServiceModel) { - if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId)) - throw new ArgumentException("Comment does not exist!"); + if (!await this._postRepository.DoesPostExist(updatePostServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); + + Post post = this._postMapper.Map<Post>(updatePostServiceModel); + bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); - Post post = this._postMapper.Map<Post>(postServiceModel); - return await this._postRepository.EditAsync(postServiceModel.Id, post); + if (result) + return (await this._postRepository.GetByIdAsync(updatePostServiceModel.PostId)).Id; + else + return Guid.Empty; } - public async Task<bool> UpdateComment(UpdateCommentServiceModel commentServiceModel) + public async Task<Guid> UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) { - if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id)) + if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) throw new ArgumentException("Comment does not exist!"); - Comment comment = this._postMapper.Map<Comment>(commentServiceModel); - bool result = await this._postRepository.EditCommentAsync(comment); + Comment comment = this._postMapper.Map<Comment>(updateCommentServiceModel); + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); - return result; + if (result) + return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; + else + return Guid.Empty; } + #endregion - //Delete + #region Delete public async Task<bool> DeletePost(Guid id) { + if (!await this._postRepository.DoesPostExist(id)) + throw new ArgumentException("Post does not exist!"); + Post post = await this._postRepository.GetByIdAsync(id); return await this._postRepository.DeleteAsync(post); } public async Task<bool> DeleteComment(Guid id) { - if (!await this._postRepository.DoesCommentExist(id)) + if (!await this._commentRepository.DoesCommentExist(id)) throw new ArgumentException("Comment does not exist!"); - Comment comment = await this._postRepository.GetCommentByIdAsync(id); - bool result = await this._postRepository.DeleteCommentAsync(comment); + Comment comment = await this._commentRepository.GetByIdAsync(id); + return await this._commentRepository.DeleteAsync(comment); + } + #endregion - return result; + #region Validations + public async Task<bool> ValidateJwtForPost(Guid postId, string rawTokenData) + { + Post post = await this._postRepository.GetByIdAsync(postId); + User user = await this.GetUserForValidation(rawTokenData); + + return post.CreatorId == user.Id; } - //Validate public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData) { - Comment comment = await this._postRepository.GetCommentByIdAsync(commentId); + Comment comment = await this._commentRepository.GetByIdAsync(commentId); User user = await this.GetUserForValidation(rawTokenData); - if (comment.IssuerId != user.Id) - return false; - - return true; + return comment.IssuerId == user.Id; } private async Task<User> GetUserForValidation(string rawTokenData) @@ -151,5 +175,6 @@ namespace DevHive.Services.Services return toReturn; } + #endregion } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index f93f801..bcf16ac 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -16,6 +16,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient<ITechnologyRepository, TechnologyRepository>(); services.AddTransient<IUserRepository, UserRepository>(); services.AddTransient<IPostRepository, PostRepository>(); + services.AddTransient<ICommentRepository, CommentRepository>(); services.AddTransient<ILanguageService, LanguageService>(); services.AddTransient<IRoleService, RoleService>(); diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 8b7344b..8b8b525 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -13,52 +13,56 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] + // [Authorize(Roles = "User")] public class PostController { private readonly IPostService _postService; private readonly IMapper _postMapper; - public PostController(IPostService postService, IMapper mapper) + public PostController(IPostService postService, IMapper postMapper) { this._postService = postService; - this._postMapper = mapper; + this._postMapper = postMapper; } - //Create + #region Create [HttpPost] - [Authorize(Roles = "User")] - public async Task<IActionResult> Create([FromBody] CreatePostWebModel createPostModel) + public async Task<IActionResult> Create(Guid userId, [FromBody] CreatePostWebModel createPostWebModel) { - CreatePostServiceModel postServiceModel = - this._postMapper.Map<CreatePostServiceModel>(createPostModel); + CreatePostServiceModel createPostServiceModel = + this._postMapper.Map<CreatePostServiceModel>(createPostWebModel); + createPostServiceModel.IssuerId = userId; - Guid id = await this._postService.CreatePost(postServiceModel); + Guid id = await this._postService.CreatePost(createPostServiceModel); return id == Guid.Empty ? - new BadRequestObjectResult("Could not create post") : + new BadRequestObjectResult("Could not create post!") : new OkObjectResult(new { Id = id }); } [HttpPost] [Route("Comment")] - public async Task<IActionResult> AddComment([FromBody] CommentWebModel commentWebModel) + public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel) { - CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map<CreateCommentServiceModel>(commentWebModel); + CreateCommentServiceModel createCommentServiceModel = + this._postMapper.Map<CreateCommentServiceModel>(createCommentWebModel); + createCommentServiceModel.IssuerId = userId; Guid id = await this._postService.AddComment(createCommentServiceModel); return id == Guid.Empty ? - new BadRequestObjectResult("Could not create comment") : + new BadRequestObjectResult("Could not create comment!") : new OkObjectResult(new { Id = id }); } + #endregion - //Read + #region Read [HttpGet] [AllowAnonymous] public async Task<IActionResult> GetById(Guid id) { - PostServiceModel postServiceModel = await this._postService.GetPostById(id); - PostWebModel postWebModel = this._postMapper.Map<PostWebModel>(postServiceModel); + ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); + ReadPostWebModel postWebModel = this._postMapper.Map<ReadPostWebModel>(postServiceModel); return new OkObjectResult(postWebModel); } @@ -68,56 +72,58 @@ namespace DevHive.Web.Controllers [AllowAnonymous] public async Task<IActionResult> GetCommentById(Guid id) { - CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id); - CommentWebModel commentWebModel = this._postMapper.Map<CommentWebModel>(commentServiceModel); + ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id); + ReadCommentWebModel readCommentWebModel = this._postMapper.Map<ReadCommentWebModel>(readCommentServiceModel); - return new OkObjectResult(commentWebModel); + return new OkObjectResult(readCommentWebModel); } + #endregion - //Update + #region Update [HttpPut] - public async Task<IActionResult> Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) + public async Task<IActionResult> Update(Guid userId, [FromBody] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) { - UpdatePostServiceModel postServiceModel = - this._postMapper.Map<UpdatePostServiceModel>(updatePostModel); - postServiceModel.IssuerId = id; + if (!await this._postService.ValidateJwtForPost(userId, authorization)) + return new UnauthorizedResult(); - bool result = await this._postService.UpdatePost(postServiceModel); + UpdatePostServiceModel updatePostServiceModel = + this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel); - if (!result) - return new BadRequestObjectResult("Could not update post!"); + Guid id = await this._postService.UpdatePost(updatePostServiceModel); - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update post!") : + new OkObjectResult(new { Id = id }); } [HttpPut] [Route("Comment")] - public async Task<IActionResult> UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel, [FromHeader] string authorization) + public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForComment(id, authorization)) + if (!await this._postService.ValidateJwtForComment(userId, authorization)) return new UnauthorizedResult(); - UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map<UpdateCommentServiceModel>(commentWebModel); - updateCommentServiceModel.Id = id; - - bool result = await this._postService.UpdateComment(updateCommentServiceModel); + UpdateCommentServiceModel updateCommentServiceModel = + this._postMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel); - if (!result) - return new BadRequestObjectResult("Could not update Comment"); + Guid id = await this._postService.UpdateComment(updateCommentServiceModel); - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); } + #endregion - //Delete + #region Delete [HttpDelete] - public async Task<IActionResult> Delete(Guid id) + public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization) { - bool result = await this._postService.DeletePost(id); - - if (!result) - return new BadRequestObjectResult("Could not delete post!"); + if (!await this._postService.ValidateJwtForPost(id, authorization)) + return new UnauthorizedResult(); - return new OkResult(); + return await this._postService.DeletePost(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); } [HttpDelete] @@ -127,12 +133,10 @@ namespace DevHive.Web.Controllers if (!await this._postService.ValidateJwtForComment(id, authorization)) return new UnauthorizedResult(); - bool result = await this._postService.DeleteComment(id); - - if (!result) - return new BadRequestObjectResult("Could not delete Comment"); - - return new OkResult(); + return await this._postService.DeleteComment(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); } + #endregion } } diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 9c6c094..3d7568b 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -17,10 +17,10 @@ namespace DevHive.Web.Controllers private readonly ITechnologyService _technologyService; private readonly IMapper _technologyMapper; - public TechnologyController(ITechnologyService technologyService, IMapper mapper) + public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper) { this._technologyService = technologyService; - this._technologyMapper = mapper; + this._technologyMapper = technologyMapper; } [HttpPost] diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs index 8e78a48..6dff49e 100644 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs @@ -1,7 +1,11 @@ +using System; + namespace DevHive.Web.Models.Post.Comment { public class UpdateCommentWebModel { + public Guid CommentId { get; set; } + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs index 5b66436..fe42715 100644 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs @@ -6,6 +6,10 @@ namespace DevHive.Web.Models.Post.Post { public class UpdatePostWebModel { + [Required] + [NotNull] + public Guid PostId { get; set; } + [NotNull] [Required] public string Message { get; set; } |
