Files

53 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
namespace GameServices.Admin.Pages.Npcs;
public class ShopsModel : AdminPageModel
{
private readonly GameDbContext _db;
public ShopsModel(GameDbContext db)
{
_db = db;
}
[FromQuery(Name = "search")]
public string? Search { get; set; }
public List<DungeonNpcShop> Shops { get; set; } = new();
public Dictionary<int, int> ItemCountByShopId { get; set; } = new();
public async Task OnGetAsync()
{
var query = _db.DungeonNpcShops.AsQueryable();
if (!string.IsNullOrWhiteSpace(Search))
{
query = query.Where(s => s.Name.Contains(Search));
}
Shops = await query.OrderBy(s => s.Id).ToListAsync();
var ids = Shops.Select(s => s.Id).ToList();
ItemCountByShopId = await _db.DungeonNpcShopItems
.Where(i => ids.Contains(i.ShopId))
.GroupBy(i => i.ShopId)
.Select(g => new { ShopId = g.Key, Count = g.Count() })
.ToDictionaryAsync(x => x.ShopId, x => x.Count);
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var shop = await _db.DungeonNpcShops.FindAsync(id);
if (shop != null)
{
_db.DungeonNpcShops.Remove(shop);
await _db.SaveChangesAsync();
}
return RedirectToPage();
}
}