Guest User

Untitled

a guest
Feb 1st, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using Entities;
  2. using Entities.Exceptions;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Service.Contracts;
  6. using Shared.DataTransferObjects;
  7. using System.ComponentModel.Design;
  8. using System.Diagnostics;
  9.  
  10. [ApiController]
  11. [Route("api/likes")]
  12. public class LikeController : ControllerBase
  13. {
  14. private readonly IServiceManager _service;
  15.  
  16. public LikeController(IServiceManager service)
  17. {
  18. _service = service;
  19. }
  20.  
  21.  
  22. [HttpGet("post/{postId}")]
  23. [Authorize]
  24. public async Task<IActionResult> GetLikesForPost(Guid postId)
  25. {
  26. var likes = await _service.LikeService.GetLikesAsync(postId, "post", trackChanges: false);
  27. return Ok(likes);
  28. }
  29.  
  30. [HttpGet("comment/{commentId}")]
  31. [Authorize]
  32. public async Task<IActionResult> GetLikesForComment(Guid commentId)
  33. {
  34. var likes = await _service.LikeService.GetLikesAsync(commentId, "comment", trackChanges: false);
  35.  
  36. return Ok(likes);
  37. }
  38.  
  39. [HttpPost]
  40. [Authorize]
  41. public async Task<LikeDto> CreateLike([FromBody] LikeForCreationDto likeDto)
  42. {
  43. var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == "UserId").Value;
  44. var displayName = _service.UserService.GetUserName(User);
  45.  
  46. LikeDto like;
  47. if (likeDto.CommentId != null && likeDto.CommentId is Guid)
  48. {
  49. likeDto.PostId = null;
  50. Guid commentId = (Guid)likeDto.CommentId;
  51. like = await _service.LikeService.CreateLikeAsync(userIdClaim, displayName, likeDto);
  52. await _service.CommentService.UpdateLikeCount(commentId, increment: true, trackChanges: true);
  53. return like;
  54. }
  55.  
  56. if (likeDto.PostId != null)
  57. {
  58. likeDto.CommentId = null;
  59. Guid postId = (Guid)likeDto.PostId;
  60. like = await _service.LikeService.CreateLikeAsync(userIdClaim, displayName, likeDto);
  61. await _service.PostService.UpdateLikeCount(postId, increment: true, trackChanges: true);
  62. return like;
  63. }
  64. else
  65. {
  66. return null;
  67. }
  68.  
  69. }
  70.  
  71. [HttpDelete("{likeId}")]
  72. public async Task<IActionResult> DeleteLike(Guid likeId)
  73. {
  74. try
  75. {
  76. await _service.LikeService.DeleteLikeAndUpdatePostAsync(likeId);
  77. return NoContent(); // Returns a 204 No Content response on successful deletion
  78. }
  79. catch (LikeNotFoundException ex)
  80. {
  81. return NotFound(ex.Message); // Returns a 404 Not Found if the like does not exist
  82. }
  83. catch (Exception ex)
  84. {
  85. // Log the exception details
  86. return StatusCode(500, "An error occurred while processing your request."); // Returns a 500 Internal Server Error for any other exceptions
  87. }
  88. }
  89.  
  90. }
  91.  
Add Comment
Please, Sign In to add comment