Exemplo n.º 1
0
        public async Task <OneOf <Ok, NotFound> > Handle(RemoveLabelTodoCommand request, CancellationToken cancellationToken)
        {
            var todo = await _todosRepository.Query()
                       .Where(todo => todo.Id == request.TodoId && todo.UserLogin == request.UserLogin)
                       .Include(todo => todo.Labels)
                       .FirstOrDefaultAsync(cancellationToken);

            if (todo == null)
            {
                return(new NotFound());
            }

            var label = await _labelsRepository
                        .ReadAsync(label => label.Id == request.LabelId && label.UserLogin == request.UserLogin);

            if (label == null)
            {
                return(new NotFound());
            }

            if (todo.Labels.All(x => x.Id != label.Id))
            {
                return(new Ok());
            }

            todo.Labels.RemoveWhere(x => x.Id == label.Id);

            await _todosRepository.SaveChangesAsync();

            return(new Ok());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RemoveLabel([FromRoute] int id, [FromRoute] int labelId)
        {
            var request  = new RemoveLabelTodoCommand(User.Identity?.Name, id, labelId);
            var response = await _mediator.Send(request);

            return(response.Match <ActionResult>(
                       ok => Ok(),
                       notFound => NotFound()
                       ));
        }