Guest User

Untitled

a guest
Feb 1st, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. using AutoMapper;
  2. using Contracts;
  3. using Entities;
  4. using Entities.Exceptions;
  5. using Microsoft.AspNetCore.Http.HttpResults;
  6. using Service.Contracts;
  7. using Shared.DataTransferObjects;
  8. using System.Security.Claims;
  9.  
  10.  
  11. namespace Service;
  12.  
  13. public class PostService : IPostService
  14. {
  15. private readonly IMapper _mapper;
  16. private readonly IRepositoryManager _repository;
  17.  
  18. public PostService(IRepositoryManager repositoryManager, IMapper mapper)
  19. {
  20. _mapper = mapper;
  21. _repository = repositoryManager;
  22. }
  23.  
  24. public async Task<IEnumerable<PostDto>> GetPostsAsync(string userIdClaim, bool trackChanges)
  25. {
  26. var postsFromDb = await _repository.Post.GetPostsAsync(userIdClaim, trackChanges);
  27. var postsDto = _mapper.Map<IEnumerable<PostDto>>(postsFromDb);
  28.  
  29. return postsDto;
  30. }
  31.  
  32. public async Task<PostDto> GetPostByIdAsync(Guid postId, bool trackChanges)
  33. {
  34. var post = await _repository.Post.GetPostByIdAsync(postId, trackChanges);
  35.  
  36. if (post == null)
  37. {
  38. throw new PostNotFoundException(postId);
  39. }
  40.  
  41. var postDto = _mapper.Map<PostDto>(post);
  42. return postDto;
  43. }
  44.  
  45. public async Task<PostDto> CreatePostForUserAsync(string userId, string displayName, PostForCreationDto postForCreation, bool trackChanges)
  46. {
  47. var postEntity = _mapper.Map<Post>(postForCreation);
  48.  
  49.  
  50. _repository.Post.CreatePostForUser(userId, displayName, postEntity);
  51. await _repository.SaveAsync();
  52.  
  53. var postToReturn = _mapper.Map<PostDto>(postEntity);
  54.  
  55. return postToReturn;
  56. }
  57.  
  58. public async Task DeletePost(Guid postId, bool trackChanges)
  59. {
  60. var post = await _repository.Post.GetPostByIdAsync(postId, trackChanges);
  61. if (post is null)
  62. {
  63. throw new PostNotFoundException(postId);
  64. }
  65.  
  66. _repository.Post.DeletePost(post);
  67. await _repository.SaveAsync();
  68. }
  69.  
  70. public async Task UpdateLikeCount(Guid postId, bool increment, bool trackChanges)
  71. {
  72. var post = await _repository.Post.GetPostByIdAsync(postId, trackChanges);
  73. if (post is null)
  74. {
  75. throw new PostNotFoundException(postId);
  76. }
  77.  
  78. post.LikeCount++;
  79. await _repository.SaveAsync();
  80.  
  81. //if (increment)
  82. //{
  83.  
  84. //}
  85. //else if (!increment)
  86. //{
  87. // post.LikeCount--;
  88. // await _repository.SaveAsync();
  89. //}
  90.  
  91. }
  92. }
  93.  
Add Comment
Please, Sign In to add comment