예제 #1
0
        public async Task <IActionResult> CreateTodoItemAsync([FromBody] Client.TodoCreationInfo creationInfo,
                                                              CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (creationInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("TodoCreationInfo");
                return(BadRequest(error));
            }

            if (!HttpContext.Items.TryGetValue("userId", out var userId) ||
                !Guid.TryParse(userId.ToString(), out var userGuid))
            {
                var error = ServiceErrorResponses.UnAuthorized();
                return(BadRequest(error));
            }

            var modelCreationInfo = Converter.TodoCreationInfoConverter.Convert(userGuid, creationInfo);
            var modelTodoItem     = await repository.CreateAsync(modelCreationInfo, cancellationToken).ConfigureAwait(false);

            var clientTodoItem = Converter.TodoItemConverter.Convert(modelTodoItem);
            var routeParams    = new Dictionary <string, object>
            {
                { "todoId", clientTodoItem.Id }
            };

            return(CreatedAtRoute("GetTodoItem", routeParams, clientTodoItem));
        }
예제 #2
0
        public static Model.TodoCreationInfo Convert(Guid userId, Client.TodoCreationInfo clientCreationInfo)
        {
            if (clientCreationInfo == null)
            {
                throw new ArgumentNullException(nameof(clientCreationInfo));
            }

            Model.TodoPriority?priority = null;

            if (Enum.TryParse(clientCreationInfo.Priority, true, out Model.TodoPriority tmpPriority))
            {
                priority = tmpPriority;
            }

            var modelCreationInfo = new Model.TodoCreationInfo(userId, clientCreationInfo.Title,
                                                               clientCreationInfo.Text, priority);

            return(modelCreationInfo);
        }