Permission.cshtml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Domain.Entities.Forum.Boards;
  2. using Domain.Entities.Forum.ValueObject;
  3. using SharedKernel.Extensions;
  4. using MediatR;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Reflection;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.RazorPages;
  9. using Microsoft.AspNetCore.Mvc.Rendering;
  10. namespace Admin.Pages.Forum.Board.Meta;
  11. public class PermissionModel(IMediator mediator) : PageModel
  12. {
  13. public int BoardID { get; set; }
  14. public List<(int ID, string Name)> BoardList { get; set; } = [];
  15. public string? QueryString { get; set; }
  16. public List<SelectListItem> PermissionList { get; set; } = [];
  17. [BindProperty]
  18. public InputModel Input { get; set; } = new();
  19. public sealed class InputModel
  20. {
  21. public int ID { get; set; }
  22. public int BoardID { get; set; }
  23. public short BoardAccess { get; set; }
  24. public short PostView { get; set; }
  25. public short PostWrite { get; set; }
  26. public short CommentView { get; set; }
  27. public short CommentWrite { get; set; }
  28. public short ReplyWrite { get; set; }
  29. public short FileUpload { get; set; }
  30. public short FileDownload { get; set; }
  31. }
  32. public async Task OnGetAsync(int id, CancellationToken ct)
  33. {
  34. BoardID = id;
  35. QueryString = Request.QueryString.ToString();
  36. PermissionList = [..Enum.GetValues<BoardPermission>().Select(e => new SelectListItem
  37. {
  38. Value = ((short)e).ToString(),
  39. Text = e.GetType().GetMember(e.ToString()).FirstOrDefault() ?.GetCustomAttribute<DisplayAttribute>()?.Name ?? e.ToString()
  40. })];
  41. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 100), ct);
  42. BoardList = [..boards.List.Select(c => (c.ID, c.Name))];
  43. var meta = await mediator.Send(new GetBoardMeta.Query(id), ct);
  44. Input = new InputModel
  45. {
  46. ID = meta.ID,
  47. BoardID = meta.BoardID,
  48. BoardAccess = meta.Permission.BoardAccess,
  49. PostView = meta.Permission.PostView,
  50. PostWrite = meta.Permission.PostWrite,
  51. CommentView = meta.Permission.CommentView,
  52. CommentWrite = meta.Permission.CommentWrite,
  53. ReplyWrite = meta.Permission.ReplyWrite,
  54. FileUpload = meta.Permission.FileUpload,
  55. FileDownload = meta.Permission.FileDownload
  56. };
  57. }
  58. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  59. {
  60. try
  61. {
  62. if (!ModelState.IsValid)
  63. {
  64. throw new Exception(ModelState.GetErrorMessages());
  65. }
  66. await mediator.Send(new UpdateBoardMeta.Command(
  67. Input.ID,
  68. Input.BoardID,
  69. null, null, null, null, null,
  70. new BoardMetaPermission
  71. {
  72. BoardAccess = Input.BoardAccess,
  73. PostView = Input.PostView,
  74. PostWrite = Input.PostWrite,
  75. CommentView = Input.CommentView,
  76. CommentWrite = Input.CommentWrite,
  77. ReplyWrite = Input.ReplyWrite,
  78. FileUpload = Input.FileUpload,
  79. FileDownload = Input.FileDownload
  80. },
  81. null, null, null), ct);
  82. TempData["SuccessMessage"] = "권한 설정이 저장되었습니다.";
  83. }
  84. catch (Exception e)
  85. {
  86. TempData["ErrorMessages"] = e.Message;
  87. }
  88. return Redirect($"/Forum/Board/Meta/Permission/{Input.BoardID}{Request.QueryString}");
  89. }
  90. }