Update.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using MediatR;
  2. using Web.Api.Common;
  3. using Web.Api.Extensions;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Security.Claims;
  6. namespace Web.Api.Endpoints.Forum.Comment;
  7. internal sealed class Update : IEndpoint
  8. {
  9. public sealed class Request
  10. {
  11. public string? Mention { get; set; }
  12. public string Content { get; set; } = "";
  13. public bool IsSecret { get; set; }
  14. public List<string>? Medias { get; set; }
  15. }
  16. public void MapEndpoint(IEndpointRouteBuilder app)
  17. {
  18. app.MapPut("api/forum/comments/{id}", async (
  19. int id,
  20. ClaimsPrincipal user,
  21. [FromForm] Request request,
  22. HttpRequest httpRequest,
  23. ISender sender,
  24. CancellationToken ct
  25. ) => {
  26. var memberID = user.GetMemberID();
  27. if (memberID is null)
  28. {
  29. return ApiResponse.Fail(StatusCodes.Status401Unauthorized, "Invalid token");
  30. }
  31. var images = httpRequest.Form.Files.GetFiles("images").ToList();
  32. var files = httpRequest.Form.Files.GetFiles("files").ToList();
  33. var command = new Application.Features.Api.Forum.Comment.Update.Command(
  34. id,
  35. memberID.Value,
  36. request.Mention,
  37. request.Content,
  38. request.IsSecret,
  39. images.Count > 0 ? images : null,
  40. request.Medias,
  41. files.Count > 0 ? files : null
  42. );
  43. var result = await sender.Send(command, ct);
  44. return result.Match(
  45. () => ApiResponse.Ok(),
  46. CustomResults.Problem
  47. );
  48. })
  49. .WithTags("Forum")
  50. .RequireAuthorization()
  51. .DisableAntiforgery();
  52. }
  53. }