96 lines
2.6 KiB
C#
96 lines
2.6 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Server.Services.Admin.Pages.DungeonBuilder;
|
|
|
|
public class TilesModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IConfiguration _config;
|
|
|
|
public TilesModel(GameDbContext db, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_config = config;
|
|
}
|
|
|
|
public List<TileTemplate> Tiles { get; set; } = new();
|
|
public List<string> Biomes { get; set; } = new();
|
|
|
|
public string? Search { get; set; }
|
|
public int? CategoryFilter { get; set; }
|
|
public string? BiomeFilter { get; set; }
|
|
|
|
public string? Message { get; set; }
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task OnGetAsync(string? search, int? category, string? biome)
|
|
{
|
|
Search = search;
|
|
CategoryFilter = category;
|
|
BiomeFilter = biome;
|
|
|
|
// Get unique biomes
|
|
Biomes = await _db.TileTemplates
|
|
.Where(t => t.Biome != null)
|
|
.Select(t => t.Biome!)
|
|
.Distinct()
|
|
.OrderBy(b => b)
|
|
.ToListAsync();
|
|
|
|
// Build query
|
|
var query = _db.TileTemplates
|
|
.Include(t => t.PrimaryTileDefinition)
|
|
.ThenInclude(td => td!.TileMapSheet)
|
|
.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(t => t.Name.Contains(search) || (t.Tags != null && t.Tags.Contains(search)));
|
|
}
|
|
|
|
if (category.HasValue)
|
|
{
|
|
query = query.Where(t => t.Category == (TileTemplateCategory)category.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(biome))
|
|
{
|
|
query = query.Where(t => t.Biome == biome);
|
|
}
|
|
|
|
Tiles = await query
|
|
.OrderBy(t => t.Category)
|
|
.ThenBy(t => t.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
|
{
|
|
var tile = await _db.TileTemplates.FindAsync(id);
|
|
if (tile != null)
|
|
{
|
|
_db.TileTemplates.Remove(tile);
|
|
await _db.SaveChangesAsync();
|
|
Message = $"Deleted tile template: {tile.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('/')}";
|
|
}
|
|
}
|