public async Task <Guid> Handle(UpdateTaskCommand request, CancellationToken cancellationToken) { var entity = await _context.Tasks.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Task), request.Id); } var mappedEntity = _mapper.Map <UpdateTaskCommand, Task>(request); mappedEntity.TaskId = entity.TaskId; _context.Entry(entity).CurrentValues.SetValues(mappedEntity); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <Guid> Handle(CreateTaskCommand request, CancellationToken cancellationToken) { var entity = _mapper.Map <CreateTaskCommand, Domain.Entities.Task>(request); if (request.ParentTaskId != null) { var parent = await _context.Tasks.FirstOrDefaultAsync(u => u.Id == request.ParentTaskId && u.Status != Status.Finished, cancellationToken : cancellationToken); if (parent == null) { throw new NotFoundException($"Task {request.ParentTaskId} is been not found or this task's already finished"); } } await _context.Tasks.AddAsync(entity, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }