GetLoginLogs.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Security.Claims;
  2. using Application.Common;
  3. using MediatR;
  4. using Web.Api.Common;
  5. using Web.Api.Extensions;
  6. namespace Web.Api.Endpoints.MyPage;
  7. internal sealed class GetLoginLogs : IEndpoint
  8. {
  9. public void MapEndpoint(IEndpointRouteBuilder app)
  10. {
  11. app.MapGet("api/mypage/login-logs", async (
  12. string? type,
  13. int? page,
  14. int? perPage,
  15. ClaimsPrincipal user,
  16. ISender sender,
  17. CancellationToken ct
  18. ) => {
  19. var memberID = user.GetMemberID();
  20. if (memberID is null)
  21. {
  22. return ApiResponse.Fail(StatusCodes.Status401Unauthorized, "Invalid token");
  23. }
  24. var searchType = type ?? SearchDateType.Today;
  25. if (!SearchDateType.ValidTypes.Contains(searchType))
  26. {
  27. return ApiResponse.Fail(StatusCodes.Status400BadRequest, "Invalid type");
  28. }
  29. var query = new Application.Features.Api.MyPage.GetLoginLogs.Query(
  30. memberID.Value,
  31. searchType,
  32. page ?? 1,
  33. perPage ?? 20
  34. );
  35. var result = await sender.Send(query, ct);
  36. return result.Match(
  37. data => ApiResponse.Ok(data),
  38. CustomResults.Problem
  39. );
  40. })
  41. .WithTags("MyPage")
  42. .RequireAuthorization();
  43. }
  44. }