not working inventory :c

This commit is contained in:
2025-02-04 17:47:18 -03:00
parent f234b5d84d
commit 7a2c3d2f67
22 changed files with 2713 additions and 49 deletions

View File

@@ -0,0 +1,32 @@
using PetCompanion.Data;
using PetCompanion.Models;
namespace PetCompanion.Repositories
{
public class GameItemsRepository
{
private readonly ApplicationDbContext _context;
public GameItemsRepository(ApplicationDbContext context)
{
_context = context;
}
public GameItem GetById(int id)
{
return _context.GameItems.Find(id);
}
public void Add(GameItem item)
{
_context.GameItems.Add(item);
_context.SaveChanges();
}
public void Update(GameItem item)
{
_context.GameItems.Update(item);
_context.SaveChanges();
}
}
}

View File

@@ -0,0 +1,67 @@
using Microsoft.EntityFrameworkCore;
using PetCompanion.Data;
using PetCompanion.Models;
namespace PetCompanion.Repositories
{
public class PetInventoryRepository
{
private readonly ApplicationDbContext _context;
public PetInventoryRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<Inventory> GetPetInventory(string petId)
{
return await _context.Inventories
.Include(i => i.Items)
.ThenInclude(ii => ii.GameItem)
.FirstOrDefaultAsync(i => i.PetId == petId);
}
public async Task<Dictionary<ItemEquipTarget, int>> GetEquippedItems(string petId)
{
var equippedItems = await _context.EquippedItems
.Where(e => e.PetId == petId)
.ToDictionaryAsync(e => e.EquipTarget, e => e.GameItemId);
return equippedItems;
}
public async Task SaveInventory(Inventory inventory)
{
var existingInventory = await _context.Inventories
.Include(i => i.Items)
.FirstOrDefaultAsync(i => i.PetId == inventory.PetId);
if (existingInventory == null)
{
_context.Inventories.Add(inventory);
}
else
{
// Clear existing items
_context.InventoryItems.RemoveRange(existingInventory.Items);
// Update inventory properties
// existingInventory.Capacity = inventory.Capacity;
// Add new items
foreach (var item in inventory.Items)
{
existingInventory.Items.Add(new InventoryItem
{
InventoryId = existingInventory.PetId,
GameItemId = item.GameItemId,
Quantity = item.Quantity,
GameItem = item.GameItem
});
}
}
await _context.SaveChangesAsync();
}
}
}

View File

@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using PetCompanion.Data;
using PetCompanion.Models;
namespace PetCompanion.Repositories
{
public class PetSkillRepository
{
private readonly ApplicationDbContext _context;
public PetSkillRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<PetSkill>> GetPetSkills(string petId)
{
return await _context.PetSkills
.Include(ps => ps.Skill)
.ThenInclude(s => s.Effects)
.Where(ps => ps.PetId == petId)
.ToListAsync();
}
public async Task<PetSkill> SavePetSkill(PetSkill petSkill)
{
if (petSkill.Id == 0)
{
_context.PetSkills.Add(petSkill);
}
else
{
_context.PetSkills.Update(petSkill);
}
await _context.SaveChangesAsync();
return petSkill;
}
}
}