Detail.cshtml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Application.Abstractions.Data;
  2. using Domain.Entities.Developers;
  3. using Domain.Entities.Developers.ValueObject;
  4. using Application.Abstractions.Messaging;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.EntityFrameworkCore;
  8. namespace Admin.Pages.Developers.Apps;
  9. public class DetailModel(IMediator mediator, IAppDbContext db) : PageModel
  10. {
  11. [BindProperty(SupportsGet = true)]
  12. public int ID { get; set; }
  13. public ApiApplication? App { get; set; }
  14. public List<ApiCredential> Credentials { get; set; } = [];
  15. public List<string> Scopes { get; set; } = [];
  16. public int? RateLimitOverride { get; set; }
  17. [BindProperty]
  18. public int LimitPerMinute { get; set; }
  19. public async Task<IActionResult> OnGetAsync(CancellationToken ct)
  20. {
  21. App = await db.ApiApplication
  22. .Include(c => c.Owner)
  23. .FirstOrDefaultAsync(c => c.ID == ID, ct);
  24. if (App is null)
  25. {
  26. return NotFound();
  27. }
  28. Credentials = await db.ApiCredential
  29. .Where(c => c.ApplicationID == ID)
  30. .OrderByDescending(c => c.CreatedAt)
  31. .ToListAsync(ct);
  32. Scopes = await db.ApiApplicationScope
  33. .Where(c => c.ApplicationID == ID)
  34. .Select(c => c.Scope)
  35. .ToListAsync(ct);
  36. RateLimitOverride = await db.ApiRateLimit
  37. .Where(c => c.ApplicationID == ID)
  38. .Select(c => (int?)c.LimitPerMinute)
  39. .FirstOrDefaultAsync(ct);
  40. LimitPerMinute = RateLimitOverride ?? 60;
  41. return Page();
  42. }
  43. public async Task<IActionResult> OnPostApproveAsync(CancellationToken ct)
  44. {
  45. try
  46. {
  47. await mediator.Send(new Application.Features.Admin.Developers.ApproveApp.Command([ID]), ct);
  48. TempData["SuccessMessage"] = "앱이 승인되었습니다.";
  49. }
  50. catch (Exception e)
  51. {
  52. TempData["ErrorMessages"] = e.Message;
  53. }
  54. return RedirectToPage("/Developers/Apps/Detail", new { ID });
  55. }
  56. public async Task<IActionResult> OnPostSuspendAsync(CancellationToken ct)
  57. {
  58. try
  59. {
  60. await mediator.Send(new Application.Features.Admin.Developers.SuspendApp.Command([ID]), ct);
  61. TempData["SuccessMessage"] = "앱이 정지되었습니다.";
  62. }
  63. catch (Exception e)
  64. {
  65. TempData["ErrorMessages"] = e.Message;
  66. }
  67. return RedirectToPage("/Developers/Apps/Detail", new { ID });
  68. }
  69. public async Task<IActionResult> OnPostOverrideRateLimitAsync(CancellationToken ct)
  70. {
  71. var result = await mediator.Send(new Application.Features.Admin.Developers.OverrideRateLimit.Command(ID, LimitPerMinute), ct);
  72. if (result.IsSuccess)
  73. {
  74. TempData["SuccessMessage"] = $"Rate limit 를 {LimitPerMinute}/min 으로 설정했습니다.";
  75. }
  76. else
  77. {
  78. TempData["ErrorMessages"] = result.Error.Description;
  79. }
  80. return RedirectToPage("/Developers/Apps/Detail", new { ID });
  81. }
  82. }