| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using SharedKernel.Extensions;
- using MediatR;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using System.ComponentModel.DataAnnotations;
- namespace Admin.Pages.Crypto.News
- {
- public class EditModel(IMediator mediator) : PageModel
- {
- public int SourceID { get; private set; }
- [BindProperty]
- public InputModel Input { get; set; } = new();
- public sealed class InputModel
- {
- [Required(ErrorMessage = "이름은 필수입니다.")]
- [StringLength(200, ErrorMessage = "이름은 {1}자 이하로 입력하세요.")]
- public string Name { get; set; } = null!;
- [Required(ErrorMessage = "URL은 필수입니다.")]
- [StringLength(2048, ErrorMessage = "URL은 {1}자 이하로 입력하세요.")]
- [DataType(DataType.Url)]
- public string Url { get; set; } = null!;
- [StringLength(500, ErrorMessage = "설명은 {1}자 이하로 입력하세요.")]
- public string? Description { get; set; }
- [Range(1, 1440, ErrorMessage = "수집주기는 1~1440분 사이로 입력하세요.")]
- public int IntervalMinutes { get; set; } = 10;
- public bool IsActive { get; set; } = true;
- }
- public async Task<IActionResult> OnGetAsync(int id, CancellationToken ct)
- {
- try
- {
- var source = await mediator.Send(new GetNewsSource.Query(id), ct);
- SourceID = source.ID;
- Input = new InputModel
- {
- Name = source.Name,
- Url = source.Url,
- Description = source.Description,
- IntervalMinutes = source.IntervalMinutes,
- IsActive = source.IsActive
- };
- return Page();
- }
- catch (KeyNotFoundException)
- {
- return NotFound();
- }
- }
- public async Task<IActionResult> OnPostAsync(int id, CancellationToken ct)
- {
- try
- {
- if (!ModelState.IsValid)
- {
- throw new Exception(ModelState.GetErrorMessages());
- }
- await mediator.Send(new UpdateNewsSource.Command(
- id,
- Input.Name,
- Input.Url,
- Input.Description,
- Input.IntervalMinutes,
- Input.IsActive
- ), ct);
- TempData["SuccessMessage"] = $"{Input.Name} 소스가 수정되었습니다.";
- return RedirectToPage("/Crypto/News/Edit", new { id });
- }
- catch (Exception e)
- {
- TempData["ErrorMessages"] = e.Message;
- return RedirectToPage("/Crypto/News/Edit", new { id });
- }
- }
- }
- }
|