104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GameServices.Admin.Pages.DungeonBuilder;
|
|
|
|
public class ObjectsModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
public ObjectsModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
public List<ObjectTemplate> Objects { get; set; } = new();
|
|
public List<string> Biomes { get; set; } = new();
|
|
|
|
public string? Search { get; set; }
|
|
public int? TypeFilter { get; set; }
|
|
public string? BiomeFilter { get; set; }
|
|
|
|
public int CurrentPage { get; set; } = 1;
|
|
public int TotalPages { get; set; }
|
|
public int PageSize { get; set; } = 20;
|
|
|
|
public string? Message { get; set; }
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task OnGetAsync(string? search, int? type, string? biome, int page = 1)
|
|
{
|
|
Search = search;
|
|
TypeFilter = type;
|
|
BiomeFilter = biome;
|
|
CurrentPage = Math.Max(1, page);
|
|
|
|
// Get unique biomes
|
|
Biomes = await _db.ObjectTemplates
|
|
.Where(o => o.Biome != null)
|
|
.Select(o => o.Biome!)
|
|
.Distinct()
|
|
.OrderBy(b => b)
|
|
.ToListAsync();
|
|
|
|
// Build query
|
|
var query = _db.ObjectTemplates
|
|
.Include(o => o.SpriteSheet)
|
|
.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(o => o.Name.Contains(search) || (o.Tags != null && o.Tags.Contains(search)));
|
|
}
|
|
|
|
if (type.HasValue)
|
|
{
|
|
query = query.Where(o => o.Type == (ObjectTemplateType)type.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(biome))
|
|
{
|
|
query = query.Where(o => o.Biome == biome);
|
|
}
|
|
|
|
var total = await query.CountAsync();
|
|
TotalPages = (int)Math.Ceiling(total / (double)PageSize);
|
|
|
|
Objects = await query
|
|
.OrderBy(o => o.Name)
|
|
.Skip((CurrentPage - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
|
{
|
|
var obj = await _db.ObjectTemplates.FindAsync(id);
|
|
if (obj != null)
|
|
{
|
|
_db.ObjectTemplates.Remove(obj);
|
|
await _db.SaveChangesAsync();
|
|
Message = $"Deleted object template: {obj.Name}";
|
|
}
|
|
|
|
return RedirectToPage();
|
|
}
|
|
|
|
public string GetCdnUrl(string? path)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return "";
|
|
|
|
var baseUrl = _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "";
|
|
if (!baseUrl.EndsWith("/downloads"))
|
|
{
|
|
baseUrl += "/downloads";
|
|
}
|
|
|
|
return $"{baseUrl}/{path.TrimStart('/')}";
|
|
}
|
|
}
|