Create.cs 1.5 KB

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