Admin Panel v6
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Server.Data;
|
||||
using Server.Data.Entities;
|
||||
|
||||
namespace VoidScavengers.Services.Admin.Pages.Items;
|
||||
|
||||
public class EditModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
|
||||
public EditModel(GameDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public DungeonItemDefinition Item { get; set; } = new();
|
||||
|
||||
public bool IsNew => Item.Id == 0;
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com";
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
{
|
||||
var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL");
|
||||
if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl;
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
var item = await _db.DungeonItemDefinitions.FindAsync(id.Value);
|
||||
if (item != null) Item = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Defaults for new item
|
||||
Item.IsEnabled = true;
|
||||
Item.RequiredLevel = 1;
|
||||
Item.MaxStack = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync(string? action)
|
||||
{
|
||||
var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL");
|
||||
if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl;
|
||||
|
||||
// Handle checkbox binding (unchecked = not sent)
|
||||
Item.IsEnabled = Request.Form.ContainsKey("Item.IsEnabled");
|
||||
Item.Stackable = Request.Form.ContainsKey("Item.Stackable");
|
||||
Item.IsTeleportScroll = Request.Form.ContainsKey("Item.IsTeleportScroll");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Item.Name))
|
||||
{
|
||||
Message = "Name is required.";
|
||||
IsError = true;
|
||||
return Page();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (action == "duplicate")
|
||||
{
|
||||
var newItem = new DungeonItemDefinition
|
||||
{
|
||||
Name = Item.Name + " (Copy)",
|
||||
Description = Item.Description,
|
||||
Type = Item.Type,
|
||||
Rarity = Item.Rarity,
|
||||
EquipSlot = Item.EquipSlot,
|
||||
RequiredStrength = Item.RequiredStrength,
|
||||
RequiredIntelligence = Item.RequiredIntelligence,
|
||||
RequiredWisdom = Item.RequiredWisdom,
|
||||
RequiredDexterity = Item.RequiredDexterity,
|
||||
RequiredLevel = Item.RequiredLevel,
|
||||
BonusStrength = Item.BonusStrength,
|
||||
BonusIntelligence = Item.BonusIntelligence,
|
||||
BonusWisdom = Item.BonusWisdom,
|
||||
BonusEndurance = Item.BonusEndurance,
|
||||
BonusDexterity = Item.BonusDexterity,
|
||||
BonusHealth = Item.BonusHealth,
|
||||
BonusMana = Item.BonusMana,
|
||||
MinDamage = Item.MinDamage,
|
||||
MaxDamage = Item.MaxDamage,
|
||||
AttackRange = Item.AttackRange,
|
||||
DamageType = Item.DamageType,
|
||||
Armor = Item.Armor,
|
||||
MagicResist = Item.MagicResist,
|
||||
HealAmount = Item.HealAmount,
|
||||
ManaRestore = Item.ManaRestore,
|
||||
StaminaRestore = Item.StaminaRestore,
|
||||
IsTeleportScroll = Item.IsTeleportScroll,
|
||||
Stackable = Item.Stackable,
|
||||
MaxStack = Item.MaxStack,
|
||||
SpritePath = Item.SpritePath,
|
||||
TintColor = Item.TintColor,
|
||||
IsEnabled = Item.IsEnabled
|
||||
};
|
||||
_db.DungeonItemDefinitions.Add(newItem);
|
||||
await _db.SaveChangesAsync();
|
||||
return RedirectToPage("/Items/Edit", new { id = newItem.Id });
|
||||
}
|
||||
|
||||
if (Item.Id == 0)
|
||||
{
|
||||
// Create new
|
||||
_db.DungeonItemDefinitions.Add(Item);
|
||||
await _db.SaveChangesAsync();
|
||||
Message = $"Item '{Item.Name}' created with ID {Item.Id}";
|
||||
return RedirectToPage("/Items/Edit", new { id = Item.Id });
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update existing
|
||||
var existing = await _db.DungeonItemDefinitions.FindAsync(Item.Id);
|
||||
if (existing == null)
|
||||
{
|
||||
Message = "Item not found.";
|
||||
IsError = true;
|
||||
return Page();
|
||||
}
|
||||
|
||||
existing.Name = Item.Name;
|
||||
existing.Description = Item.Description;
|
||||
existing.Type = Item.Type;
|
||||
existing.Rarity = Item.Rarity;
|
||||
existing.EquipSlot = Item.EquipSlot;
|
||||
existing.RequiredStrength = Item.RequiredStrength;
|
||||
existing.RequiredIntelligence = Item.RequiredIntelligence;
|
||||
existing.RequiredWisdom = Item.RequiredWisdom;
|
||||
existing.RequiredDexterity = Item.RequiredDexterity;
|
||||
existing.RequiredLevel = Item.RequiredLevel;
|
||||
existing.BonusStrength = Item.BonusStrength;
|
||||
existing.BonusIntelligence = Item.BonusIntelligence;
|
||||
existing.BonusWisdom = Item.BonusWisdom;
|
||||
existing.BonusEndurance = Item.BonusEndurance;
|
||||
existing.BonusDexterity = Item.BonusDexterity;
|
||||
existing.BonusHealth = Item.BonusHealth;
|
||||
existing.BonusMana = Item.BonusMana;
|
||||
existing.MinDamage = Item.MinDamage;
|
||||
existing.MaxDamage = Item.MaxDamage;
|
||||
existing.AttackRange = Item.AttackRange;
|
||||
existing.DamageType = Item.DamageType;
|
||||
existing.Armor = Item.Armor;
|
||||
existing.MagicResist = Item.MagicResist;
|
||||
existing.HealAmount = Item.HealAmount;
|
||||
existing.ManaRestore = Item.ManaRestore;
|
||||
existing.StaminaRestore = Item.StaminaRestore;
|
||||
existing.IsTeleportScroll = Item.IsTeleportScroll;
|
||||
existing.Stackable = Item.Stackable;
|
||||
existing.MaxStack = Item.MaxStack;
|
||||
existing.SpritePath = Item.SpritePath;
|
||||
existing.TintColor = Item.TintColor;
|
||||
existing.IsEnabled = Item.IsEnabled;
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
Item = existing;
|
||||
Message = "Changes saved successfully.";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Message = $"Error: {ex.Message}";
|
||||
IsError = true;
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user