Beispiel #1
0
        public static Model.TodoPatchInfo Convert(Guid todoId, Guid userId, Client.TodoPatchInfo clientPatchInfo)
        {
            if (clientPatchInfo == null)
            {
                throw new ArgumentNullException(nameof(clientPatchInfo));
            }

            Model.TodoPriority?priority = null;

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

            Model.TodoState?state = null;

            if (Enum.TryParse(clientPatchInfo.State, true, out Model.TodoState tmpState))
            {
                state = tmpState;
            }

            var modelPatchInfo = new Model.TodoPatchInfo(todoId, userId)
            {
                Text     = clientPatchInfo.Text,
                Title    = clientPatchInfo.Title,
                Priority = priority,
                State    = state
            };

            return(modelPatchInfo);
        }
Beispiel #2
0
        public async Task <IActionResult> PatchTodoItemAsync([FromRoute] string todoId,
                                                             [FromBody] ClientModels.TodoItems.TodoPatchInfo patchInfo, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

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

            if (!Guid.TryParse(todoId, out var todoItemGuid))
            {
                var error = ServiceErrorResponses.TodoNotFound(todoId);
                return(NotFound(error));
            }

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

            var modelPathInfo = ModelsConverters.TodoItems.TodoPatchConverter.Convert(todoItemGuid, userGuid, patchInfo);

            Models.TodoItems.TodoItem modelTodoItem = null;

            try
            {
                modelTodoItem = await repository.PatchAsync(modelPathInfo, cancellationToken).ConfigureAwait(false);
            }
            catch (Model.TodoNotFoundException)
            {
                var error = ServiceErrorResponses.TodoNotFound(todoId);
                return(NotFound(error));
            }

            var clientTodoItem = ModelsConverters.TodoItems.TodoItemConverter.Convert(modelTodoItem);

            return(Ok(clientTodoItem));
        }