55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
|
|
namespace GameServices.Admin.Pages.Npcs;
|
|
|
|
public class DialogTreesModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
|
|
public DialogTreesModel(GameDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
[FromQuery(Name = "search")]
|
|
public string? Search { get; set; }
|
|
|
|
public List<DungeonNpcDialogTree> Trees { get; set; } = new();
|
|
public Dictionary<int, int> NodeCountByTreeId { get; set; } = new();
|
|
|
|
public async Task OnGetAsync()
|
|
{
|
|
var query = _db.DungeonNpcDialogTrees.AsQueryable();
|
|
if (!string.IsNullOrWhiteSpace(Search))
|
|
{
|
|
query = query.Where(t => t.Name.Contains(Search) || t.Description.Contains(Search));
|
|
}
|
|
|
|
Trees = await query
|
|
.OrderBy(t => t.Id)
|
|
.ToListAsync();
|
|
|
|
var treeIds = Trees.Select(t => t.Id).ToList();
|
|
NodeCountByTreeId = await _db.DungeonNpcDialogNodes
|
|
.Where(n => treeIds.Contains(n.DialogTreeId))
|
|
.GroupBy(n => n.DialogTreeId)
|
|
.Select(g => new { TreeId = g.Key, Count = g.Count() })
|
|
.ToDictionaryAsync(x => x.TreeId, x => x.Count);
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
|
{
|
|
var tree = await _db.DungeonNpcDialogTrees.FindAsync(id);
|
|
if (tree != null)
|
|
{
|
|
_db.DungeonNpcDialogTrees.Remove(tree);
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
|
|
return RedirectToPage();
|
|
}
|
|
}
|