| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using Domain.Entities.Store.ValueObject;
- using SharedKernel.Extensions;
- using SharedKernel.Helpers;
- using Application.Abstractions.Messaging;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- namespace Admin.Pages.Store.Product;
- public class IndexModel(IMediator mediator) : PageModel
- {
- [BindProperty(SupportsGet = true)]
- public QueryParams Query { get; set; } = new();
- public List<(int ID, string Name)> Games { get; set; } = [];
- public sealed class QueryParams
- {
- [Range(1, int.MaxValue)]
- public int PageNum { get; set; } = 1;
- [Range(1, 100)]
- public ushort PerPage { get; set; } = 20;
- [DisplayName("검색 조건")]
- [Range(1, 2, ErrorMessage = "{0}이(가) 올바르지 않습니다.")]
- public int? Search { get; set; }
- [DisplayName("검색어")]
- [MaxLength(100)]
- public string? Keyword { get; set; }
- [DisplayName("시작일")]
- public string? StartAt { get; set; }
- [DisplayName("종료일")]
- public string? EndAt { get; set; }
- [DisplayName("게임")]
- public int? GameID { get; set; }
- [DisplayName("상품 유형")]
- public ProductType? Type { get; set; }
- [DisplayName("활성화")]
- public int? IsActive { get; set; }
- }
- public int Total { get; set; }
- public List<(
- int Num,
- int ID,
- int GameID,
- string GameName,
- string Name,
- string? Thumbnail,
- string TypeText,
- int Price,
- DiscountType DiscountType,
- int DiscountValue,
- int SalePrice,
- int Stock,
- char IsActive,
- int Order,
- string? SaleStartAt,
- string? SaleEndAt,
- string? UpdatedAt,
- string CreatedAt,
- string EditURL
- )> List { get; set; } = [];
- public Pagination? Pagination { get; set; }
- public async Task OnGetAsync(CancellationToken ct)
- {
- if (!ModelState.IsValid)
- {
- return;
- }
- // 게임 드롭다운 (필터용)
- var gamesResult = await mediator.Send(new GetGames.Query(false), ct);
- Games = [.. gamesResult.List.Select(g => (ID: g.ID, Name: g.KorName))];
- bool? isActive = Query.IsActive == 1 ? true : null;
- var result = await mediator.Send(new SearchProducts.Query(
- Query.Search,
- Query.Keyword,
- Query.StartAt,
- Query.EndAt,
- Query.GameID,
- Query.Type,
- isActive,
- Query.PageNum,
- Query.PerPage
- ), ct);
- Total = result.Total;
- var qs = Request.QueryString.ToString();
- List = [.. result.List.Select(c => (
- c.Num,
- c.ID,
- c.GameID,
- c.GameName,
- c.Name,
- c.Thumbnail,
- TypeText: c.Type == ProductType.Physical ? "실물" : "쿠폰",
- c.Price,
- c.DiscountType,
- c.DiscountValue,
- c.SalePrice,
- c.Stock,
- c.IsActive ? 'Y' : 'N',
- c.Order,
- c.SaleStartAt?.ToString("yyyy-MM-dd"),
- c.SaleEndAt?.ToString("yyyy-MM-dd"),
- c.UpdatedAt.GetDateAt() ?? "-",
- c.CreatedAt.GetDateAt(),
- EditURL: $"/Store/Product/Edit/{c.ID}{qs}"
- ))];
- Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
- }
- public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
- {
- try
- {
- if (ids is null || ids.Length == 0)
- {
- throw new Exception("삭제할 항목을 선택해주세요.");
- }
- var result = await mediator.Send(new DeleteProduct.Command(ids), ct);
- if (result.IsFailure)
- {
- throw new Exception(result.Error.Description);
- }
- TempData["SuccessMessage"] = $"{ids.Length}건이 삭제되었습니다.";
- }
- catch (Exception e)
- {
- TempData["ErrorMessages"] = e.Message;
- }
- return Redirect($"{Request.Path}{Request.QueryString}");
- }
- }
|