Index.cshtml.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using SharedKernel.Helpers;
  2. using MediatR;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. namespace Admin.Pages.Document
  8. {
  9. public class IndexModel(IMediator mediator) : PageModel
  10. {
  11. [BindProperty(SupportsGet = true)]
  12. public QueryParams Query { get; set; } = new();
  13. public sealed class QueryParams
  14. {
  15. [Range(1, int.MaxValue)]
  16. [DisplayName("페이지 번호")]
  17. public int PageNum { get; set; } = 1;
  18. [Range(1, 100)]
  19. [DisplayName("페이지 목록 수")]
  20. public ushort PerPage { get; set; } = 10;
  21. }
  22. public int Total { get; set; } = 0;
  23. public List<(
  24. int Num,
  25. int ID,
  26. string Link,
  27. string Code,
  28. string Subject,
  29. string? Content,
  30. char IsActive,
  31. string Views,
  32. string? UpdatedAt,
  33. string CreatedAt,
  34. string EditURL,
  35. string DeleteURL
  36. )> List { get; set; } = [];
  37. public Pagination? Pagination { get; set; }
  38. public async Task OnGetAsync(CancellationToken ct)
  39. {
  40. if (!ModelState.IsValid)
  41. {
  42. return;
  43. }
  44. var result = await mediator.Send(new SearchDocuments.Query(Query.PageNum, Query.PerPage), ct);
  45. Total = result.Total;
  46. List = [..result.List.Select(c => (
  47. c.Num,
  48. c.ID,
  49. c.Link,
  50. c.Code,
  51. c.Subject,
  52. c.Content,
  53. c.IsActive,
  54. c.Views,
  55. c.UpdatedAt,
  56. c.CreatedAt,
  57. EditURL: $"/Document/Edit/{c.ID}{Request.QueryString}",
  58. DeleteURL: $"/Document/Delete/{c.ID}{Request.QueryString}"
  59. ))];
  60. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  61. }
  62. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  63. {
  64. try
  65. {
  66. await mediator.Send(new DeleteDocument.Command(ids), ct);
  67. TempData["SuccessMessage"] = $"{ids.Length}개 문서가 삭제되었습니다.";
  68. }
  69. catch (Exception e)
  70. {
  71. TempData["ErrorMessages"] = e.Message;
  72. }
  73. return RedirectToPage("/Document/Index", Query);
  74. }
  75. }
  76. }