PlaceOrder.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Security.Claims;
  2. using System.Text.Json.Serialization;
  3. using Application.Abstractions.Messaging;
  4. using Domain.Entities.Paper.ValueObject;
  5. using SharedKernel.Results;
  6. using Web.Api.Common;
  7. using Web.Api.Extensions;
  8. namespace Web.Api.Endpoints.Paper;
  9. public sealed class PlaceOrderRequest
  10. {
  11. [JsonPropertyName("stockCode")]
  12. public string StockCode { get; set; } = "";
  13. /// <summary>주문 방향 — "Buy" | "Sell" (대소문자 무시).</summary>
  14. [JsonPropertyName("side")]
  15. public string Side { get; set; } = "";
  16. [JsonPropertyName("quantity")]
  17. public int Quantity { get; set; }
  18. }
  19. /// <summary>모의투자 — 주문 접수.</summary>
  20. internal sealed class PlaceOrder : IEndpoint
  21. {
  22. public void MapEndpoint(IEndpointRouteBuilder app)
  23. {
  24. app.MapPost("api/paper/orders", async (
  25. PlaceOrderRequest body,
  26. ClaimsPrincipal user,
  27. ISender sender,
  28. CancellationToken ct
  29. ) => {
  30. if (!Enum.TryParse<PaperOrderSide>(body.Side, ignoreCase: true, out var side) || !Enum.IsDefined(side))
  31. {
  32. return CustomResults.Problem(Result.Failure(Error.Problem("Paper.InvalidSide", "주문 방향(side)이 올바르지 않습니다. (Buy | Sell)")));
  33. }
  34. var memberID = user.GetRequiredMemberID();
  35. var result = await sender.Send(new Application.Features.Api.Paper.PlaceOrder.Command(memberID, body.StockCode, side, body.Quantity), ct);
  36. if (result.IsFailure)
  37. {
  38. return CustomResults.Problem(result);
  39. }
  40. return ApiResponse.Ok(result.Value);
  41. })
  42. .WithTags("Paper")
  43. .RequireAuthorization();
  44. }
  45. }