Fix and cleanup for Events.WebApi
This commit is contained in:
@@ -15,42 +15,42 @@ public class GenericCommandHandler<TDal, TDto, TPK> : IRequestHandler<AddCommand
|
||||
where TDto: IHasIdAsPK<TPK>
|
||||
where TPK : IEquatable<TPK>
|
||||
{
|
||||
protected DbContext Ctx { get; }
|
||||
protected ILogger Logger { get; }
|
||||
protected IMapper Mapper { get; }
|
||||
protected DbContext ctx { get; }
|
||||
protected ILogger logger { get; }
|
||||
protected IMapper mapper { get; }
|
||||
|
||||
protected GenericCommandHandler(DbContext ctx, ILogger logger, IMapper mapper)
|
||||
{
|
||||
Ctx = ctx;
|
||||
Logger = logger;
|
||||
Mapper = mapper;
|
||||
this.ctx = ctx;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public virtual async Task<TPK> Handle(AddCommand<TDto, TPK> request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = Mapper.Map<TDto, TDal>(request.Dto);
|
||||
Ctx.Add(entity);
|
||||
await Ctx.SaveChangesAsync(cancellationToken);
|
||||
var entity = mapper.Map<TDto, TDal>(request.Dto);
|
||||
ctx.Add(entity);
|
||||
await ctx.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public virtual async Task Handle(UpdateCommand<TDto> request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await Ctx.Set<TDal>().FindAsync(request.Dto.Id);
|
||||
var entity = await ctx.Set<TDal>().FindAsync(request.Dto.Id);
|
||||
if (entity != null)
|
||||
{
|
||||
Mapper.Map(request.Dto, entity);
|
||||
await Ctx.SaveChangesAsync(cancellationToken);
|
||||
mapper.Map(request.Dto, entity);
|
||||
await ctx.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogError($"UpdateCommand<{typeof(TDto).Name}> : Invalid id #{request.Dto.Id}");
|
||||
logger.LogError($"UpdateCommand<{typeof(TDto).Name}> : Invalid id #{request.Dto.Id}");
|
||||
throw new ArgumentException($"Invalid id: {request.Dto.Id}");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task Handle(DeleteCommand<TDto, TPK> request, CancellationToken cancellationToken)
|
||||
{
|
||||
await Ctx.Set<TDal>().Where(d => d.Id.Equals(request.Id)).ExecuteDeleteAsync(cancellationToken);
|
||||
await ctx.Set<TDal>().Where(d => d.Id.Equals(request.Id)).ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,10 +43,10 @@ public class RegistrationsCommandsHandler : GenericCommandHandler<Registration,
|
||||
|
||||
public override async Task Handle(UpdateCommand<RegistrationDTO> request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await Ctx.Set<Registration>().SingleOrDefaultAsync(r => r.Id == request.Dto.Id, cancellationToken);
|
||||
var entity = await ctx.Set<Registration>().SingleOrDefaultAsync(r => r.Id == request.Dto.Id, cancellationToken);
|
||||
if (entity == null)
|
||||
{
|
||||
Logger.LogError("UpdateCommand<{DtoName}> : Invalid id #{Id}", typeof(RegistrationDTO).Name, request.Dto.Id);
|
||||
logger.LogError("UpdateCommand<{DtoName}> : Invalid id #{Id}", typeof(RegistrationDTO).Name, request.Dto.Id);
|
||||
throw new ArgumentException($"Invalid id: {request.Dto.Id}");
|
||||
}
|
||||
|
||||
@@ -70,13 +70,13 @@ public class RegistrationsCommandsHandler : GenericCommandHandler<Registration,
|
||||
|
||||
public override async Task Handle(DeleteCommand<RegistrationDTO, int> request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await Ctx.Set<Registration>()
|
||||
var entity = await ctx.Set<Registration>()
|
||||
.AsNoTracking()
|
||||
.SingleOrDefaultAsync(r => r.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
Logger.LogError("DeleteCommand<{DtoName}> : Invalid id #{Id}", typeof(RegistrationDTO).Name, request.Id);
|
||||
logger.LogError("DeleteCommand<{DtoName}> : Invalid id #{Id}", typeof(RegistrationDTO).Name, request.Id);
|
||||
throw new ArgumentException($"Invalid id: {request.Id}");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user