// WingsEmu // // Developed by NosWings Team using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace PhoenixLib.DAL.EFCore.PGSQL { /// /// GenericAsyncMappedRepository is an asynchronous Data Access Object /// /// /// public sealed class GenericLongRepository : IGenericAsyncLongRepository where TEntity : class, ILongEntity, new() where TDbContext : DbContext { private static readonly string _typeName = typeof(TEntity).Name; private readonly IDbContextFactory _contextFactory; private readonly ILogger> _logger; public GenericLongRepository(IDbContextFactory contextFactory, ILogger> logger) { _contextFactory = contextFactory; _logger = logger; } public async Task> GetAllAsync() { try { await using TDbContext context = _contextFactory.CreateDbContext(); List tmp = await context.Set().ToListAsync(); return tmp; } catch (Exception e) { _logger.LogError(e, $"GetAllAsync {_typeName}"); throw; } } public async Task GetByIdAsync(long id) { try { await using DbContext context = _contextFactory.CreateDbContext(); TEntity tmp = await context.Set().FindAsync(id); return tmp; } catch (Exception e) { _logger.LogError(e, $"GetByIdAsync {_typeName}"); throw; } } public async Task> GetByIdsAsync(IEnumerable ids) { try { await using DbContext context = _contextFactory.CreateDbContext(); List entities = await context.Set().Where(s => ids.Contains(s.Id)).ToListAsync(); return entities; } catch (Exception e) { _logger.LogError(e, $"GetByIdsAsync {_typeName}"); throw; } } public async Task SaveAsync(TEntity obj) { try { await using DbContext context = _contextFactory.CreateDbContext(); await context.SingleMergeAsync(obj, operation => { operation.InsertKeepIdentity = true; operation.IsCheckConstraintOnInsertDisabled = false; }); return obj; } catch (Exception e) { _logger.LogError(e, $"SaveAsync {_typeName}"); throw; } } public async Task> SaveAsync(IReadOnlyList objs) { try { await using DbContext context = _contextFactory.CreateDbContext(); await context.BulkMergeAsync(objs, operation => { operation.InsertKeepIdentity = true; operation.IsCheckConstraintOnInsertDisabled = false; }); return objs; } catch (Exception e) { _logger.LogError(e, $"SaveAsync {_typeName}"); throw; } } public async Task DeleteByIdAsync(long id) { try { await using DbContext context = _contextFactory.CreateDbContext(); TEntity entity = await context.FindAsync(id); if (entity == null) { return; } context.Set().Remove(entity); await context.SaveChangesAsync(); } catch (Exception e) { _logger.LogError(e, $"DeleteByIdAsync {_typeName}"); throw; } } public async Task DeleteByIdsAsync(IEnumerable ids) { try { await using DbContext context = _contextFactory.CreateDbContext(); foreach (long id in ids) { TEntity entity = await context.FindAsync(id); if (entity == null) { continue; } context.Set().Remove(entity); } await context.SaveChangesAsync(); } catch (Exception e) { _logger.LogError(e, $"DeleteByIdsAsync {_typeName}"); throw; } } } }