Notify.cshtml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 MediatR;
  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 meta = await mediator.Send(new GetBoardMeta.Query(id), ct);
  39. Input = new InputModel
  40. {
  41. ID = meta.ID,
  42. BoardID = meta.BoardID,
  43. PostWriteNotify = meta.Notify.PostWriteNotify,
  44. CommentWriteNotify = meta.Notify.CommentWriteNotify,
  45. ReplyWriteNotify = meta.Notify.ReplyWriteNotify
  46. };
  47. }
  48. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  49. {
  50. try
  51. {
  52. if (!ModelState.IsValid)
  53. {
  54. throw new Exception(ModelState.GetErrorMessages());
  55. }
  56. await mediator.Send(new UpdateBoardMeta.Command(
  57. Input.ID,
  58. Input.BoardID,
  59. null,
  60. null,
  61. null,
  62. null,
  63. null,
  64. null,
  65. new BoardMetaNotify
  66. {
  67. PostWriteNotify = Input.PostWriteNotify,
  68. CommentWriteNotify = Input.CommentWriteNotify,
  69. ReplyWriteNotify = Input.ReplyWriteNotify
  70. },
  71. null,
  72. null
  73. ), ct);
  74. TempData["SuccessMessage"] = "알림 설정이 저장되었습니다.";
  75. }
  76. catch (Exception e)
  77. {
  78. TempData["ErrorMessages"] = e.Message;
  79. }
  80. return Redirect($"/Forum/Board/Meta/Notify/{Input.BoardID}{Request.QueryString}");
  81. }
  82. }