TickerConfig.cshtml.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using MediatR;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. using System.ComponentModel;
  5. using System.ComponentModel.DataAnnotations;
  6. namespace Admin.Pages.Crypto;
  7. public class TickerConfigModel(IMediator mediator) : PageModel
  8. {
  9. [BindProperty]
  10. public InputModel Input { get; set; } = new();
  11. public sealed class InputModel
  12. {
  13. [DisplayName("시세 업데이트 주기 (초)")]
  14. [Range(1, 300)]
  15. public int TickerRefreshSeconds { get; set; } = 5;
  16. [DisplayName("급등 임계값 (%)")]
  17. [Range(0.1, 100)]
  18. public decimal SurgeThreshold { get; set; } = 5.0m;
  19. [DisplayName("급락 임계값 (%)")]
  20. [Range(-100, -0.1)]
  21. public decimal PlungeThreshold { get; set; } = -5.0m;
  22. [DisplayName("메인 페이지 기본 표시 코인 수")]
  23. [Range(1, 100)]
  24. public int MainPageCoinCount { get; set; } = 10;
  25. }
  26. public async Task OnGetAsync(CancellationToken ct)
  27. {
  28. var result = await mediator.Send(new GetTickerConfig.Query(), ct);
  29. Input = new InputModel
  30. {
  31. TickerRefreshSeconds = result.TickerRefreshSeconds,
  32. SurgeThreshold = result.SurgeThreshold,
  33. PlungeThreshold = result.PlungeThreshold,
  34. MainPageCoinCount = result.MainPageCoinCount
  35. };
  36. }
  37. public async Task<IActionResult> OnPostAsync(CancellationToken ct)
  38. {
  39. if (!ModelState.IsValid)
  40. {
  41. return Page();
  42. }
  43. try
  44. {
  45. await mediator.Send(new SaveTickerConfig.Command(
  46. Input.TickerRefreshSeconds,
  47. Input.SurgeThreshold,
  48. Input.PlungeThreshold,
  49. Input.MainPageCoinCount
  50. ), ct);
  51. TempData["SuccessMessage"] = "저장되었습니다.";
  52. }
  53. catch (Exception e)
  54. {
  55. TempData["ErrorMessages"] = e.Message;
  56. }
  57. return RedirectToPage("/Crypto/TickerConfig");
  58. }
  59. }