using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using PhoenixLib.DAL; using PhoenixLib.Logging; using Plugin.Database.DB; using WingsAPI.Data.Families; namespace Plugin.Database.Families { public class FamilyWarehouseItemDao : IFamilyWarehouseItemDao { private readonly IDbContextFactory _contextFactory; private readonly IMapper _mapper; public FamilyWarehouseItemDao(IDbContextFactory contextFactory, IMapper mapper) { _contextFactory = contextFactory; _mapper = mapper; } public async Task SaveAsync(IReadOnlyList objs) { try { IEnumerable entities = _mapper.Map(objs); await using GameContext context = _contextFactory.CreateDbContext(); await context.FamilyWarehouseItems.BulkMergeAsync(entities); return await context.SaveChangesAsync(); } catch (Exception e) { Log.Error("[FAMILY_WAREHOUSE_ITEM_DAO][SaveAsync] ", e); throw; } } public async Task DeleteAsync(IEnumerable objs) { try { IEnumerable entities = _mapper.Map(objs); await using GameContext context = _contextFactory.CreateDbContext(); await context.FamilyWarehouseItems.BulkDeleteAsync(entities); return await context.SaveChangesAsync(); } catch (Exception e) { Log.Error("[FAMILY_WAREHOUSE_ITEM_DAO][DeleteAsync] ", e); throw; } } public async Task> GetByFamilyIdAsync(long familyId) { try { await using GameContext context = _contextFactory.CreateDbContext(); IEnumerable items = await context.FamilyWarehouseItems.Where(s => s.FamilyId == familyId).ToListAsync(); return _mapper.Map(items); } catch (Exception e) { Log.Error("[FAMILY_WAREHOUSE_ITEM_DAO][GetByFamilyIdAsync] ", e); throw; } } } }