Index.cshtml.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using SharedKernel.Helpers;
  2. using Application.Abstractions.Messaging;
  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. )> List { get; set; } = [];
  36. public Pagination? Pagination { get; set; }
  37. public async Task OnGetAsync(CancellationToken ct)
  38. {
  39. if (!ModelState.IsValid)
  40. {
  41. return;
  42. }
  43. var result = await mediator.Send(new SearchDocuments.Query(Query.PageNum, Query.PerPage), ct);
  44. Total = result.Total;
  45. List = [..result.List.Select(c => (
  46. c.Num,
  47. c.ID,
  48. c.Link,
  49. c.Code,
  50. c.Subject,
  51. c.Content,
  52. c.IsActive,
  53. c.Views,
  54. c.UpdatedAt,
  55. c.CreatedAt,
  56. EditURL: $"/Document/Edit/{c.ID}{Request.QueryString}"
  57. ))];
  58. Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
  59. }
  60. public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
  61. {
  62. try
  63. {
  64. await mediator.Send(new DeleteDocument.Command(ids), ct);
  65. TempData["SuccessMessage"] = $"{ids.Length}? ??? ???????.";
  66. }
  67. catch (Exception e)
  68. {
  69. TempData["ErrorMessages"] = e.Message;
  70. }
  71. return RedirectToPage("/Document/Index", Query);
  72. }
  73. }
  74. }