using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using PhoenixLib.DAL; using PhoenixLib.Logging; using Plugin.Database.DB; using Plugin.Database.Entities.ServerData; using WingsAPI.Data.TimeSpace; namespace Plugin.Database.DAOs { public class TimeSpaceRecordDao : ITimeSpaceRecordDao { private readonly IDbContextFactory _contextFactory; private readonly IMapper _mapper; public TimeSpaceRecordDao(IMapper mapper, IDbContextFactory contextFactory) { _mapper = mapper; _contextFactory = contextFactory; } public async Task GetRecordById(long timeSpaceId) { try { await using GameContext context = _contextFactory.CreateDbContext(); DbTimeSpaceRecord entity = await context.Set().FindAsync(timeSpaceId); return _mapper.Map(entity); } catch (Exception e) { Log.Error("GetRecordById", e); throw; } } public async Task SaveRecord(TimeSpaceRecordDto recordDto) { await using GameContext context = _contextFactory.CreateDbContext(); try { DbTimeSpaceRecord obj = _mapper.Map(recordDto); DbTimeSpaceRecord entity = await context.Set().FindAsync(obj.TimeSpaceId); if (entity == null) { entity = obj; await context.Set().AddAsync(entity); } else { context.Update(obj); } await context.SaveChangesAsync(); } catch (Exception e) { Log.Error("SaveRecord", e); throw; } } public async Task> GetAllRecords() { await using GameContext context = _contextFactory.CreateDbContext(); var list = new List(); try { List relations = await context.TimeSpaceRecords.ToListAsync(); foreach (DbTimeSpaceRecord relation in relations) { list.Add(_mapper.Map(relation)); } return list; } catch (Exception e) { Log.Error("GetAllRecords", e); throw; } } } }