Write.cshtml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using SharedKernel.Attributes;
  2. using SharedKernel.Extensions;
  3. using MediatR;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using System.ComponentModel.DataAnnotations;
  8. namespace Admin.Pages.Crypto.List
  9. {
  10. public class WriteModel(IMediator mediator) : PageModel
  11. {
  12. public string? QueryString { get; set; }
  13. public List<SelectListItem> Categories { get; set; } = [];
  14. [BindProperty]
  15. public InputModel Input { get; set; } = new();
  16. public sealed class InputModel
  17. {
  18. [Required(ErrorMessage = "심볼은 필수입니다.")]
  19. [StringLength(30, ErrorMessage = "심볼은 {1}자 이하로 입력하세요.")]
  20. public string Symbol { get; set; } = null!;
  21. [Required(ErrorMessage = "한글명은 필수입니다.")]
  22. [StringLength(200, ErrorMessage = "한글명은 {1}자 이하로 입력하세요.")]
  23. public string KorName { get; set; } = null!;
  24. [Required(ErrorMessage = "영문명은 필수입니다.")]
  25. [StringLength(200, ErrorMessage = "영문명은 {1}자 이하로 입력하세요.")]
  26. public string EngName { get; set; } = null!;
  27. [AllowedExtensions("jpg,jpeg,png,gif,webp,svg", ErrorMessage = "이미지 확장자는 jpg, jpeg, png, gif, webp, svg 이어야 합니다.")]
  28. public IFormFile? LogoImageFile { get; set; }
  29. [StringLength(5000)]
  30. public string? Description { get; set; }
  31. [StringLength(100)]
  32. public string? ContractAddress { get; set; }
  33. [StringLength(500)]
  34. [DataType(DataType.Url)]
  35. public string? WebsiteUrl { get; set; }
  36. [StringLength(500)]
  37. [DataType(DataType.Url)]
  38. public string? WhitepaperUrl { get; set; }
  39. [StringLength(500)]
  40. [DataType(DataType.Url)]
  41. public string? TwitterUrl { get; set; }
  42. [StringLength(500)]
  43. [DataType(DataType.Url)]
  44. public string? TelegramUrl { get; set; }
  45. public bool IsActive { get; set; } = false;
  46. public bool IsWarning { get; set; } = false;
  47. public bool IsNew { get; set; } = false;
  48. public bool IsDelisted { get; set; } = false;
  49. public int[] CategoryIDs { get; set; } = [];
  50. }
  51. public async Task OnGetAsync(CancellationToken ct)
  52. {
  53. var categories = await mediator.Send(new GetCryptoCategories.Query(), ct);
  54. Categories = [..categories.List.Select(c => new SelectListItem(c.Name, c.ID.ToString()))];
  55. QueryString = Request.QueryString.ToString();
  56. }
  57. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  58. {
  59. try
  60. {
  61. if (!ModelState.IsValid)
  62. {
  63. throw new Exception(ModelState.GetErrorMessages());
  64. }
  65. await mediator.Send(new CreateCoin.Command(
  66. Input.Symbol,
  67. Input.KorName,
  68. Input.EngName,
  69. Input.LogoImageFile,
  70. Input.Description,
  71. Input.ContractAddress,
  72. Input.WebsiteUrl,
  73. Input.WhitepaperUrl,
  74. Input.TwitterUrl,
  75. Input.TelegramUrl,
  76. Input.IsActive,
  77. Input.IsWarning,
  78. Input.IsNew,
  79. Input.IsDelisted,
  80. Input.CategoryIDs
  81. ), ct);
  82. TempData["SuccessMessage"] = $"{Input.Symbol} 코인이 등록되었습니다.";
  83. return RedirectToPage("/Crypto/List/Index");
  84. }
  85. catch (Exception e)
  86. {
  87. TempData["ErrorMessages"] = e.Message;
  88. return Redirect($"/Crypto/List/Write?{Request.QueryString}");
  89. }
  90. }
  91. }
  92. }