69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Server.Data;
|
|
using Server.Data.Entities;
|
|
using Server.Data.Enums;
|
|
|
|
namespace VoidScavengers.Services.Admin.Pages.Items;
|
|
|
|
public class IndexModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
|
|
public IndexModel(GameDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public List<DungeonItemDefinition> Items { get; set; } = new();
|
|
public string? Search { get; set; }
|
|
public ItemType? TypeFilter { get; set; }
|
|
public ItemRarity? RarityFilter { get; set; }
|
|
public int CurrentPage { get; set; } = 1;
|
|
public int PageSize { get; set; } = 20;
|
|
public int TotalCount { get; set; }
|
|
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
|
|
public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com";
|
|
|
|
public async Task OnGetAsync(string? search, ItemType? type, ItemRarity? rarity, int page = 1)
|
|
{
|
|
Search = search;
|
|
TypeFilter = type;
|
|
RarityFilter = rarity;
|
|
CurrentPage = Math.Max(1, page);
|
|
|
|
var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL");
|
|
if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl;
|
|
|
|
var query = _db.DungeonItemDefinitions.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
query = query.Where(i => i.Name.Contains(search) || i.Description.Contains(search));
|
|
|
|
if (type.HasValue)
|
|
query = query.Where(i => i.Type == type.Value);
|
|
|
|
if (rarity.HasValue)
|
|
query = query.Where(i => i.Rarity == rarity.Value);
|
|
|
|
TotalCount = await query.CountAsync();
|
|
|
|
Items = await query
|
|
.OrderBy(i => i.Id)
|
|
.Skip((CurrentPage - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(int id)
|
|
{
|
|
var item = await _db.DungeonItemDefinitions.FindAsync(id);
|
|
if (item != null)
|
|
{
|
|
_db.DungeonItemDefinitions.Remove(item);
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
return RedirectToPage();
|
|
}
|
|
}
|