Handler.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Domain.Entities.Forum.Logs;
  4. using SharedKernel.Results;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Api.Forum.PostLink.Click;
  7. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result<Response>>
  8. {
  9. public async Task<Result<Response>> Handle(Command request, CancellationToken ct)
  10. {
  11. var link = await db.PostLink.FirstOrDefaultAsync(x => x.ID == request.PostLinkID && !x.IsDisabled, ct);
  12. if (link is null)
  13. {
  14. return Result.Failure<Response>(Error.NotFound("PostLink.NotFound", "링크를 찾을 수 없습니다."));
  15. }
  16. link.Clicks++;
  17. var log = new PostLinkClickLog
  18. {
  19. PostID = link.PostID,
  20. PostLinkID = link.ID,
  21. MemberID = request.MemberID,
  22. IpAddress = request.IpAddress,
  23. UserAgent = request.UserAgent
  24. };
  25. await db.PostLinkClickLog.AddAsync(log, ct);
  26. await db.SaveChangesAsync(ct);
  27. return new Response(link.Url);
  28. }
  29. }