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/DevHive.Web | |
| parent | fac1bf2d1772e60cbecc1cf0381c6063a0e97ffd (diff) | |
| download | DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.tar DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.tar.gz DevHive-e01a81954e0fba2c4521e03a76f48a970a87994f.zip | |
All Post&Comment Implemented; Initializing testing...
Diffstat (limited to 'src/DevHive.Web')
5 files changed, 64 insertions, 51 deletions
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; } |
