Curation.cshtml.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using MediatR;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. namespace Admin.Pages.Crypto;
  5. public class CurationModel(IMediator mediator) : PageModel
  6. {
  7. public int Total { get; private set; } = 0;
  8. public List<(
  9. int Num,
  10. int ID,
  11. int Index,
  12. string Symbol,
  13. string KorName,
  14. string EngName,
  15. string? LogoImage,
  16. bool IsFeatured,
  17. short DisplayOrder,
  18. string IsActive
  19. )> List { get; set; } = [];
  20. [BindProperty(Name = "request")]
  21. public List<InputModel> Input { get; private set; } = [];
  22. public sealed class InputModel
  23. {
  24. public int CoinID { get; set; }
  25. public bool IsFeatured { get; set; } = false;
  26. public short DisplayOrder { get; set; } = 0;
  27. }
  28. public async Task OnGetAsync(CancellationToken ct)
  29. {
  30. var result = await mediator.Send(new GetCoinCuration.Query(), ct);
  31. Total = result.Total;
  32. List = [..result.List.Select((c, i) => (
  33. c.Num,
  34. c.ID,
  35. i,
  36. c.Symbol,
  37. c.KorName,
  38. c.EngName,
  39. c.LogoImage,
  40. c.IsFeatured,
  41. c.DisplayOrder,
  42. c.IsActive ? "Y" : "N"
  43. ))];
  44. }
  45. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  46. {
  47. try
  48. {
  49. if (!ModelState.IsValid)
  50. {
  51. throw new Exception();
  52. }
  53. var cmd = new SaveCoinCuration.Command(
  54. [..Input.Select(x => new SaveCoinCuration.Command.Row(
  55. x.CoinID,
  56. x.IsFeatured,
  57. x.DisplayOrder
  58. ))]
  59. );
  60. await mediator.Send(cmd, ct);
  61. TempData["SuccessMessage"] = "저장되었습니다.";
  62. }
  63. catch (Exception e)
  64. {
  65. TempData["ErrorMessages"] = e.Message;
  66. }
  67. return RedirectToPage("/Crypto/Curation");
  68. }
  69. }