Index.cshtml.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using SharedKernel.Extensions;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.NavMenu;
  6. public class IndexModel(IMediator mediator) : PageModel
  7. {
  8. public int Total { get; set; }
  9. public List<(
  10. int Num,
  11. int ID,
  12. string Label,
  13. string Href,
  14. int SortOrder,
  15. char IsVisible,
  16. string UpdatedAt,
  17. string CreatedAt
  18. )> List { get; set; } = [];
  19. public async Task OnGetAsync(CancellationToken ct)
  20. {
  21. var result = await mediator.Send(new Application.Features.Admin.NavMenu.List.Query(), ct);
  22. Total = result.Total;
  23. List = [..result.List.Select(c => (
  24. c.Num,
  25. c.ID,
  26. c.Label,
  27. c.Href,
  28. c.SortOrder,
  29. c.IsVisible ? 'Y' : 'N',
  30. c.UpdatedAt.GetDateAt(),
  31. c.CreatedAt.GetDateAt()
  32. ))];
  33. }
  34. public async Task<IActionResult> OnPostToggleAsync(int id, CancellationToken ct)
  35. {
  36. var result = await mediator.Send(new Application.Features.Admin.NavMenu.ToggleVisible.Command(id), ct);
  37. if (result.IsFailure)
  38. {
  39. TempData["ErrorMessages"] = result.Error.Description;
  40. }
  41. else
  42. {
  43. TempData["SuccessMessage"] = "노출 여부가 변경되었습니다.";
  44. }
  45. return RedirectToPage("/NavMenu/Index");
  46. }
  47. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  48. {
  49. try
  50. {
  51. await mediator.Send(new Application.Features.Admin.NavMenu.Delete.Command(ids), ct);
  52. TempData["SuccessMessage"] = $"{ids.Length}개 메뉴가 삭제되었습니다.";
  53. }
  54. catch (Exception e)
  55. {
  56. TempData["ErrorMessages"] = e.Message;
  57. }
  58. return RedirectToPage("/NavMenu/Index");
  59. }
  60. }