not working inventory :c
This commit is contained in:
32
Repositories/GameItemsRepository.cs
Normal file
32
Repositories/GameItemsRepository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
67
Repositories/PetInventoryRepository.cs
Normal file
67
Repositories/PetInventoryRepository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
39
Repositories/PetSkillRepository.cs
Normal file
39
Repositories/PetSkillRepository.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user