using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using DataServer.Data; using DataServer.Data.Entities; namespace GameServices.Admin.Pages.Npcs; public class ShopEditModel : AdminPageModel { private readonly GameDbContext _db; public ShopEditModel(GameDbContext db) { _db = db; } [BindProperty] public DungeonNpcShop Shop { get; set; } = new(); public List Items { get; set; } = new(); public List ItemDefinitions { get; set; } = new(); public bool IsNew => Shop.Id == 0; public string? Message { get; set; } public bool IsError { get; set; } public async Task OnGetAsync(int? id) { await LoadItemDefinitionsAsync(); if (id.HasValue) { var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == id.Value); if (shop != null) { Shop = shop; Items = await _db.DungeonNpcShopItems .Include(i => i.ItemDefinition) .Where(i => i.ShopId == shop.Id) .OrderBy(i => i.SlotIndex) .ToListAsync(); return; } } Shop = new DungeonNpcShop { Name = "", IsEnabled = true }; } public async Task OnPostAsync() { await LoadItemDefinitionsAsync(); Shop.Name = (Shop.Name ?? "").Trim(); Shop.IsEnabled = Request.Form.ContainsKey("Shop.IsEnabled"); if (string.IsNullOrWhiteSpace(Shop.Name)) { IsError = true; Message = "Shop name is required."; return Page(); } try { if (Shop.Id == 0) { Shop.CreatedAt = DateTime.UtcNow; Shop.UpdatedAt = DateTime.UtcNow; _db.DungeonNpcShops.Add(Shop); await _db.SaveChangesAsync(); return RedirectToPage("/Npcs/ShopEdit", new { id = Shop.Id }); } var existing = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == Shop.Id); if (existing == null) { IsError = true; Message = "Shop not found."; return Page(); } existing.Name = Shop.Name; existing.IsEnabled = Shop.IsEnabled; existing.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); Shop = existing; Items = await _db.DungeonNpcShopItems .Include(i => i.ItemDefinition) .Where(i => i.ShopId == Shop.Id) .OrderBy(i => i.SlotIndex) .ToListAsync(); Message = "Shop saved."; } catch (Exception ex) { IsError = true; Message = $"Error: {ex.Message}"; } return Page(); } public async Task OnPostUpsertItemAsync( int shopId, int slotIndex, int itemDefinitionId, int quantity, long priceGold, bool restocks, int restockIntervalSeconds) { var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == shopId); if (shop == null) return RedirectToPage("/Npcs/Shops"); var item = await _db.DungeonNpcShopItems.FirstOrDefaultAsync(i => i.ShopId == shopId && i.SlotIndex == slotIndex); if (item == null) { item = new DungeonNpcShopItem { ShopId = shopId, SlotIndex = Math.Max(0, slotIndex) }; _db.DungeonNpcShopItems.Add(item); } item.ItemDefinitionId = itemDefinitionId; item.Quantity = Math.Max(1, quantity); item.PriceGold = Math.Max(0, priceGold); item.Restocks = restocks; item.RestockIntervalSeconds = Math.Max(1, restockIntervalSeconds); if (item.Restocks && !item.LastRestockAt.HasValue) { item.LastRestockAt = DateTime.UtcNow; } shop.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); return RedirectToPage("/Npcs/ShopEdit", new { id = shopId }); } public async Task OnPostDeleteItemAsync(int shopId, int itemId) { var item = await _db.DungeonNpcShopItems.FirstOrDefaultAsync(i => i.Id == itemId && i.ShopId == shopId); if (item != null) { _db.DungeonNpcShopItems.Remove(item); var shop = await _db.DungeonNpcShops.FirstOrDefaultAsync(s => s.Id == shopId); if (shop != null) shop.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); } return RedirectToPage("/Npcs/ShopEdit", new { id = shopId }); } private async Task LoadItemDefinitionsAsync() { ItemDefinitions = await _db.DungeonItemDefinitions .Where(i => i.IsEnabled) .OrderBy(i => i.Name) .ToListAsync(); } }