public async Task <IReadOnlyList <T> > Handle(ListEntitiesQuery <T> request, CancellationToken cancellationToken) { return(await _context.Set <T>() .OrderBy(x => x.CreationDate) .Skip(Math.Max(request.Skip, 0)) .Take(Math.Max(Math.Min(request.Take, 100), 1)) .ToListAsync(cancellationToken)); }
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); }
public async Task <T> Handle(ReadEntityQuery <T> request, CancellationToken cancellationToken) { return(await _context.Set <T>() .SingleOrDefaultAsync(x => x.Id == request.EntityId, cancellationToken)); }