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.PostFile.Download;
  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 file = await db.PostFile.FirstOrDefaultAsync(x => x.ID == request.PostFileID && !x.IsDisabled, ct);
  12. if (file is null)
  13. {
  14. return Result.Failure<Response>(Error.NotFound("PostFile.NotFound", "파일을 찾을 수 없습니다."));
  15. }
  16. file.Downloads++;
  17. var log = new PostFileDownLog
  18. {
  19. PostID = file.PostID,
  20. PostFileID = file.ID,
  21. MemberID = request.MemberID,
  22. IpAddress = request.IpAddress,
  23. UserAgent = request.UserAgent
  24. };
  25. await db.PostFileDownLog.AddAsync(log, ct);
  26. await db.SaveChangesAsync(ct);
  27. return new Response(file.Url, file.FileName);
  28. }
  29. }