forked from ddrilling/AsbCloudServer
Фикс базового репозитория
This commit is contained in:
parent
04a6300123
commit
79929dc6d2
@ -7,6 +7,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Exceptions;
|
||||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Repository
|
namespace AsbCloudInfrastructure.Repository
|
||||||
@ -120,15 +121,26 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
if (!dtos.Any())
|
if (!dtos.Any())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var ids = dtos.Select(d => d.Id);
|
var ids = dtos
|
||||||
|
.Select(o => o.Id)
|
||||||
var countExistingEntities = await dbSet
|
.Distinct()
|
||||||
.Where(d => ids.Contains(d.Id))
|
.ToArray();
|
||||||
|
|
||||||
|
if (ids.Any(id => id == default))
|
||||||
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь Id");
|
||||||
|
|
||||||
|
if (ids.Length != dtos.Count())
|
||||||
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны иметь уникальные Id");
|
||||||
|
|
||||||
|
var dbSet = dbContext.Set<TEntity>();
|
||||||
|
|
||||||
|
var existingEntitiesCount = await dbSet
|
||||||
|
.Where(o => ids.Contains(o.Id))
|
||||||
.CountAsync(token);
|
.CountAsync(token);
|
||||||
|
|
||||||
if (ids.Count() > countExistingEntities)
|
if (ids.Length != existingEntitiesCount)
|
||||||
return ICrudRepository<TDto>.ErrorIdNotFound;
|
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны существовать в БД");
|
||||||
|
|
||||||
var entities = dtos.Select(Convert);
|
var entities = dtos.Select(Convert);
|
||||||
var entries = entities.Select(entity => dbSet.Update(entity)).Cast<EntityEntry>().ToList();
|
var entries = entities.Select(entity => dbSet.Update(entity)).Cast<EntityEntry>().ToList();
|
||||||
@ -145,9 +157,8 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
.FirstOrDefault(e => e.Id == id);
|
.FirstOrDefault(e => e.Id == id);
|
||||||
if (entity == default)
|
if (entity == default)
|
||||||
return Task.FromResult(ICrudRepository<TDto>.ErrorIdNotFound);
|
return Task.FromResult(ICrudRepository<TDto>.ErrorIdNotFound);
|
||||||
var entry = dbSet.Remove(entity);
|
dbSet.Remove(entity);
|
||||||
var affected = dbContext.SaveChangesAsync(token);
|
var affected = dbContext.SaveChangesAsync(token);
|
||||||
entry.State = EntityState.Detached;
|
|
||||||
return affected;
|
return affected;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,10 +175,8 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
return ICrudRepository<TDto>.ErrorIdNotFound;
|
return ICrudRepository<TDto>.ErrorIdNotFound;
|
||||||
|
|
||||||
var entities = dbContext.Set<TEntity>().Where(e => ids.Contains(e.Id));
|
var entities = dbContext.Set<TEntity>().Where(e => ids.Contains(e.Id));
|
||||||
var entries = entities.Select(entity => dbSet.Remove(entity)).Cast<EntityEntry>().ToList();
|
dbContext.Set<TEntity>().RemoveRange(entities);
|
||||||
var affected = await dbContext.SaveChangesAsync(token);
|
return await dbContext.SaveChangesAsync(token);
|
||||||
entries.ForEach(e => e.State = EntityState.Detached);
|
|
||||||
return affected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||||||
|
Loading…
Reference in New Issue
Block a user