using Application.Features.Admin.Cache.ClearCache;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Server;

public class CacheModel(IMediator mediator) : PageModel
{
    public void OnGet()
    {
    }

    public async Task<IActionResult> OnPostAsync(string? prefix, CancellationToken ct)
    {
        try
        {
            await mediator.Send(new Command(prefix), ct);

            TempData["SuccessMessage"] = string.IsNullOrEmpty(prefix) ? "전체 캐시가 삭제되었습니다." : $"'{prefix}' 캐시가 삭제되었습니다.";
        }
        catch (Exception e)
        {
            TempData["ErrorMessages"] = e.Message;
        }

        return RedirectToPage();
    }
}