diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-30 21:21:49 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-30 21:21:49 +0200 |
| commit | 278130d86378a6b2db6ba443631f303fb7d7e207 (patch) | |
| tree | 72fa0ad4889a9f18cdd0a54992dc151784323c83 /src/DevHive.Web/Controllers | |
| parent | a96478dd52250c4f5b494c66806d5e97156c48b3 (diff) | |
| download | DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.tar DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.tar.gz DevHive-278130d86378a6b2db6ba443631f303fb7d7e207.zip | |
Implemented Posts and merged Comment to Post
Diffstat (limited to 'src/DevHive.Web/Controllers')
| -rw-r--r-- | src/DevHive.Web/Controllers/CommentController.cs | 72 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/PostController.cs | 132 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/RoleController.cs | 2 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/UserController.cs | 2 |
4 files changed, 134 insertions, 74 deletions
diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs deleted file mode 100644 index 5b6b0ee..0000000 --- a/src/DevHive.Web/Controllers/CommentController.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Comment; -using DevHive.Services.Services; -using DevHive.Web.Models.Comment; -using Microsoft.AspNetCore.Mvc; - -namespace DevHive.Web.Controllers -{ - [ApiController] - [Route("/api/[controller]")] - public class CommentController - { - private readonly CommentService _commentService; - private readonly IMapper _commentMapper; - - public CommentController(CommentService commentService, IMapper mapper) - { - this._commentService = commentService; - this._commentMapper = mapper; - } - - [HttpPost] - public async Task<IActionResult> Create([FromBody] CommentWebModel commentWebModel) - { - CommentServiceModel commentServiceModel = this._commentMapper.Map<CommentServiceModel>(commentWebModel); - - bool result = await this._commentService.CreateComment(commentServiceModel); - - if(!result) - return new BadRequestObjectResult("Could not create the Comment"); - - return new OkResult(); - } - - [HttpGet] - public async Task<IActionResult> GetById(Guid id) - { - GetByIdCommentServiceModel getByIdCommentServiceModel = await this._commentService.GetCommentById(id); - GetByIdCommentWebModel getByIdCommentWebModel = this._commentMapper.Map<GetByIdCommentWebModel>(getByIdCommentServiceModel); - - return new OkObjectResult(getByIdCommentWebModel); - } - - [HttpPut] - public async Task<IActionResult> Update(Guid id, [FromBody] CommentWebModel commentWebModel) - { - UpdateCommentServiceModel updateCommentServiceModel = this._commentMapper.Map<UpdateCommentServiceModel>(commentWebModel); - updateCommentServiceModel.Id = id; - - bool result = await this._commentService.UpdateComment(updateCommentServiceModel); - - if (!result) - return new BadRequestObjectResult("Could not update Comment"); - - return new OkResult(); - } - - [HttpDelete] - public async Task<IActionResult> Delete(Guid id) - { - bool result = await this._commentService.DeleteComment(id); - - if (!result) - return new BadRequestObjectResult("Could not delete Comment"); - - return new OkResult(); - } - } -}
\ No newline at end of file diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..397ddbc --- /dev/null +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,132 @@ +using System.Threading.Tasks; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post.Post; +using DevHive.Services.Models.Post.Post; +using DevHive.Web.Models.Post.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Common.Models.Misc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + //[Authorize(Posts = "Admin")] + public class PostController + { + private readonly PostService _postService; + private readonly IMapper _postMapper; + + public PostController(PostService postService, IMapper mapper) + { + this._postService = postService; + this._postMapper = mapper; + } + + //Create + [HttpPost] + public async Task<IActionResult> Create([FromBody] CreatePostWebModel createPostModel) + { + CreatePostServiceModel postServiceModel = + this._postMapper.Map<CreatePostServiceModel>(createPostModel); + + bool result = await this._postService.CreatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not create post!"); + + return new OkResult(); + } + + [HttpPost] + [Route("Comment")] + public async Task<IActionResult> AddComment([FromBody] CommentWebModel commentWebModel) + { + CommentServiceModel commentServiceModel = this._postMapper.Map<CommentServiceModel>(commentWebModel); + + bool result = await this._postService.AddComment(commentServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Comment"); + + return new OkResult(); + } + + //Read + [HttpGet] + public async Task<IActionResult> GetById(Guid id) + { + PostServiceModel postServiceModel = await this._postService.GetPostById(id); + PostWebModel postWebModel = this._postMapper.Map<PostWebModel>(postServiceModel); + + return new OkObjectResult(postWebModel); + } + + [HttpGet] + [Route("Comment")] + public async Task<IActionResult> GetCommentById(Guid id) + { + CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id); + IdModel idModel = this._postMapper.Map<IdModel>(commentServiceModel); + + return new OkObjectResult(idModel); + } + + //Update + [HttpPut] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) + { + UpdatePostServiceModel postServiceModel = + this._postMapper.Map<UpdatePostServiceModel>(updatePostModel); + postServiceModel.IssuerId = id; + + bool result = await this._postService.UpdatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update post!"); + + return new OkResult(); + } + + [HttpPut] + [Route("Comment")] + public async Task<IActionResult> UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel) + { + UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map<UpdateCommentServiceModel>(commentWebModel); + updateCommentServiceModel.Id = id; + + bool result = await this._postService.UpdateComment(updateCommentServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Comment"); + + return new OkResult(); + } + + //Delete + [HttpDelete] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._postService.DeletePost(id); + + if (!result) + return new BadRequestObjectResult("Could not delete post!"); + + return new OkResult(); + } + + [HttpDelete] + [Route("Comment")] + public async Task<IActionResult> DeleteComment(Guid id) + { + bool result = await this._postService.DeleteComment(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Comment"); + + return new OkResult(); + } + } +} diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 6b81ab8..d710f5a 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -67,7 +67,7 @@ namespace DevHive.Web.Controllers bool result = await this._roleService.DeleteRole(id); if (!result) - return new BadRequestObjectResult("Could nor delete role!"); + return new BadRequestObjectResult("Could not delete role!"); return new OkResult(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 02cae0c..ce972a5 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,7 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; namespace DevHive.Web.Controllers { |
