Update.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Security.Claims;
  2. using MediatR;
  3. using Web.Api.Common;
  4. using Web.Api.Extensions;
  5. namespace Web.Api.Endpoints.Forum.Comment;
  6. internal sealed class Update : IEndpoint
  7. {
  8. public sealed record Request(
  9. string Content,
  10. bool IsSecret
  11. );
  12. public void MapEndpoint(IEndpointRouteBuilder app)
  13. {
  14. app.MapPut("api/forum/comments/{id}", async (
  15. int id,
  16. ClaimsPrincipal user,
  17. Request request,
  18. ISender sender,
  19. CancellationToken ct
  20. ) => {
  21. var memberID = user.GetMemberID();
  22. if (memberID is null)
  23. {
  24. return ApiResponse.Fail(StatusCodes.Status401Unauthorized, "Invalid token");
  25. }
  26. var command = new Application.Features.Api.Forum.Comment.Update.Command(
  27. id,
  28. memberID.Value,
  29. request.Content,
  30. request.IsSecret
  31. );
  32. var result = await sender.Send(command, ct);
  33. return result.Match(
  34. () => ApiResponse.Ok(),
  35. CustomResults.Problem
  36. );
  37. })
  38. .WithTags("Forum")
  39. .RequireAuthorization();
  40. }
  41. }