84 lines
2.5 KiB
C#
84 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using DataServer.Data.Enums;
|
|
|
|
namespace GameServices.Admin.Pages.Items;
|
|
|
|
public class IndexModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
|
|
public IndexModel(GameDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public List<DungeonItemDefinition> Items { get; set; } = new();
|
|
|
|
[FromQuery(Name = "search")]
|
|
public string? Search { get; set; }
|
|
|
|
[FromQuery(Name = "type")]
|
|
public ItemType? TypeFilter { get; set; }
|
|
|
|
[FromQuery(Name = "rarity")]
|
|
public ItemRarity? RarityFilter { get; set; }
|
|
|
|
[FromQuery(Name = "page")]
|
|
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 string GetPageUrl(int page)
|
|
{
|
|
var parts = new List<string> { $"page={page}" };
|
|
if (!string.IsNullOrEmpty(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}");
|
|
if (TypeFilter.HasValue) parts.Add($"type={TypeFilter}");
|
|
if (RarityFilter.HasValue) parts.Add($"rarity={RarityFilter}");
|
|
return "/admin/items?" + string.Join("&", parts);
|
|
}
|
|
|
|
public async Task OnGetAsync()
|
|
{
|
|
if (CurrentPage < 1) CurrentPage = 1;
|
|
|
|
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 (TypeFilter.HasValue)
|
|
query = query.Where(i => i.Type == TypeFilter.Value);
|
|
|
|
if (RarityFilter.HasValue)
|
|
query = query.Where(i => i.Rarity == RarityFilter.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();
|
|
}
|
|
}
|