87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Server.Services.Admin.Pages.DungeonBuilder;
|
|
|
|
public class RoomsModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
|
|
public RoomsModel(GameDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public List<BuilderRoomTemplate> Rooms { 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; } = 18;
|
|
|
|
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.BuilderRoomTemplates
|
|
.Where(r => r.Biome != null)
|
|
.Select(r => r.Biome!)
|
|
.Distinct()
|
|
.OrderBy(b => b)
|
|
.ToListAsync();
|
|
|
|
// Build query
|
|
var query = _db.BuilderRoomTemplates.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(r => r.Name.Contains(search) || (r.Tags != null && r.Tags.Contains(search)));
|
|
}
|
|
|
|
if (type.HasValue)
|
|
{
|
|
query = query.Where(r => r.Type == (RoomType)type.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(biome))
|
|
{
|
|
query = query.Where(r => r.Biome == biome);
|
|
}
|
|
|
|
var total = await query.CountAsync();
|
|
TotalPages = (int)Math.Ceiling(total / (double)PageSize);
|
|
|
|
Rooms = await query
|
|
.OrderBy(r => r.Name)
|
|
.Skip((CurrentPage - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
|
{
|
|
var room = await _db.BuilderRoomTemplates.FindAsync(id);
|
|
if (room != null)
|
|
{
|
|
_db.BuilderRoomTemplates.Remove(room);
|
|
await _db.SaveChangesAsync();
|
|
Message = $"Deleted room template: {room.Name}";
|
|
}
|
|
|
|
return RedirectToPage();
|
|
}
|
|
}
|