Exemplo n.º 1
0
        public async Task <CommandResponse> Handle(CreateTodoCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Handle {nameof(CreateTodoCommand)}");

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            try
            {
                var todo = _mapper.Map <Todo>(request);

                await _repository.CreateTodoAsync(todo);
            }
            catch (TodoValidationException ex)
            {
                _logger.LogError(ex, "Input validation failed, todo could not be created.");
                return(new CommandResponse("Input validation failed, todo could not be created.", false));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unexpected Error occured. Todo could not be created.");
                return(new CommandResponse("Unexpected Error occured. Todo could not be created.", false));
            }

            return(new CommandResponse("Todo was created", true));
        }
        public async Task <IActionResult> CreateTodo(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "todoList")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"{nameof(CreateTodo)} method prosessing...");
            if (!await lineTokenService.VerifyTokenAsync(req.Headers[ApiServer.AccessTokenHeaderName]))
            {
                return(new ForbidResult());
            }

            try
            {
                var json = await req.ReadAsStringAsync();

                var todo = JsonConvert.DeserializeObject <Todo>(json);

                if (string.IsNullOrEmpty(todo?.UserId))
                {
                    return(new BadRequestObjectResult(JsonConvert.SerializeObject(new { Message = $"{nameof(todo.UserId)} is required." })));
                }

                await todoRepository.CreateTodoAsync(todo);

                return(new CreatedResult("", $"{{\"id\":\"{todo.Id}\"}}"));
            }
            catch (JsonSerializationException e)
            {
                return(new BadRequestObjectResult(e.Message));
            }
        }
        public async Task <DisplayTodo> ExecuteAsync(CreateTodoRequest command, DisplayTodo previousResult)
        {
            _logger.LogInformation("Creating todo item");

            var task = await _repository.CreateTodoAsync(_requestToModelMapper.Map(command));

            var displayTodo = _todoToDisplayTodoMapper.Map(task);

            return(displayTodo);
        }