| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using SharedKernel.Extensions;
- using MediatR;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using System.ComponentModel.DataAnnotations;
- namespace Admin.Pages.Popup;
- public class PositionModel(IMediator mediator) : PageModel
- {
- public int Total { get; private set; }
- public List<(
- int Num,
- int ID,
- int Index,
- string Code,
- string Subject,
- char IsActive,
- int PopupRows,
- string? UpdatedAt,
- string CreatedAt
- )> List { get; set; } = [];
- [BindProperty(Name = "request")]
- public List<InputModel> Input { get; private set; } = [];
- public List<InputModel> Data { get; private set; } = [];
- public sealed class InputModel
- {
- public int? ID { get; set; }
- [Required]
- [StringLength(30)]
- public required string Code { get; set; }
- [Required]
- [StringLength(255)]
- public required string Subject { get; set; }
- public bool IsActive { get; set; }
- }
- public async Task OnGetAsync(CancellationToken ct)
- {
- if (!ModelState.IsValid)
- {
- return;
- }
- var result = await mediator.Send(new GetPopupPositions.Query(), ct);
- Total = result.Total;
- List = [..result.List.Select(c => (
- c.Num,
- c.ID,
- c.Index,
- c.Code,
- c.Subject,
- c.IsActive ? 'Y' : 'N',
- c.PopupRows,
- c.UpdatedAt.GetDateAt() ?? "-",
- c.CreatedAt.GetDateAt()
- ))];
- Data = [..result.List.Select(x => new InputModel
- {
- ID = x.ID,
- Code = x.Code,
- Subject = x.Subject,
- IsActive = x.IsActive
- })];
- }
- public async Task<IActionResult> OnPostAsync(CancellationToken ct)
- {
- try
- {
- if (!ModelState.IsValid)
- {
- throw new Exception();
- }
- var cmd = new SavePopupPositions.Command(
- [..Input.Select(x => new SavePopupPositions.Command.Row(
- x.ID,
- x.Code,
- x.Subject,
- x.IsActive
- ))]
- );
- var response = await mediator.Send(cmd, ct);
- TempData["SuccessMessage"] = $"저장 완료 (추가: {response.Inserted}, 수정: {response.Updated}, 삭제: {response.Deleted})";
- }
- catch (Exception e)
- {
- TempData["ErrorMessages"] = e.Message;
- }
- return RedirectToPage("/Popup/Position");
- }
- }
|