Notify.cshtml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.ComponentModel.DataAnnotations;
  2. using System.Reflection;
  3. using Domain.Entities.Forum.Boards;
  4. using Domain.Entities.Forum.ValueObject;
  5. using SharedKernel.Extensions;
  6. using Application.Abstractions.Messaging;
  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 NotifyModel(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> NotifyList { 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 byte? PostWriteNotify { get; set; }
  24. public byte? CommentWriteNotify { get; set; }
  25. public byte? ReplyWriteNotify { get; set; }
  26. }
  27. public async Task OnGetAsync(int id, CancellationToken ct)
  28. {
  29. BoardID = id;
  30. QueryString = Request.QueryString.ToString();
  31. NotifyList = [..Enum.GetValues<BoardNotify>().Select(e => new SelectListItem
  32. {
  33. Value = ((byte)e).ToString(),
  34. Text = e.GetType().GetMember(e.ToString()).FirstOrDefault() ?.GetCustomAttribute<DisplayAttribute>()?.Name ?? e.ToString()
  35. })];
  36. var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 100), ct);
  37. BoardList = [..boards.List.Select(c => (c.ID, c.Name))];
  38. var metaResult = await mediator.Send(new GetBoardMeta.Query(id), ct);
  39. if (metaResult.IsFailure)
  40. {
  41. TempData["ErrorMessages"] = metaResult.Error.Description;
  42. return;
  43. }
  44. var meta = metaResult.Value;
  45. Input = new InputModel
  46. {
  47. ID = meta.ID,
  48. BoardID = meta.BoardID,
  49. PostWriteNotify = meta.Notify.PostWriteNotify,
  50. CommentWriteNotify = meta.Notify.CommentWriteNotify,
  51. ReplyWriteNotify = meta.Notify.ReplyWriteNotify
  52. };
  53. }
  54. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  55. {
  56. if (!ModelState.IsValid)
  57. {
  58. TempData["ErrorMessages"] = ModelState.GetErrorMessages();
  59. return Redirect($"/Forum/Board/Meta/Notify/{Input.BoardID}{Request.QueryString}");
  60. }
  61. var result = await mediator.Send(new UpdateBoardMeta.Command(
  62. Input.ID,
  63. Input.BoardID,
  64. null,
  65. null,
  66. null,
  67. null,
  68. null,
  69. null,
  70. new BoardMetaNotify
  71. {
  72. PostWriteNotify = Input.PostWriteNotify,
  73. CommentWriteNotify = Input.CommentWriteNotify,
  74. ReplyWriteNotify = Input.ReplyWriteNotify
  75. },
  76. null,
  77. null
  78. ), ct);
  79. if (result.IsFailure)
  80. {
  81. TempData["ErrorMessages"] = result.Error.Description;
  82. }
  83. else
  84. {
  85. TempData["SuccessMessage"] = "알림 설정이 저장되었습니다.";
  86. }
  87. return Redirect($"/Forum/Board/Meta/Notify/{Input.BoardID}{Request.QueryString}");
  88. }
  89. }