public async Task <T> Handle(DeleteEntityCommand <T> request, CancellationToken cancellationToken) { var entity = await _context.Set <T>().SingleOrDefaultAsync(x => x.Id == request.EntityId, cancellationToken); if (entity is null) { return(null); } _context.Attach(entity); _context.Entry(entity).State = EntityState.Deleted; await _context.SaveChangesAsync(cancellationToken); return(entity); }
public async Task <T> Handle(UpdateEntityCommand <T> request, CancellationToken cancellationToken) { var exists = await _context.Set <T>().AnyAsync(x => x.Id == request.EntityId, cancellationToken); if (!exists) { return(null); } var entity = request.Entity; entity.Id = request.EntityId; _context.Attach(entity); _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(cancellationToken); return(entity); }