Пример #1
0
        public async Task <IActionResult> GetTodoItemAsync([FromRoute] string todoId,
                                                           CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!Guid.TryParse(todoId, out var modelTodoItemId))
            {
                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));
            }

            Model.TodoItem modelTodoItem = null;

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

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

            return(Ok(clientTodoItem));
        }
Пример #2
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));
        }
Пример #3
0
        public async Task <IActionResult> DeleteTodoItemAsync([FromRoute] string todoId,
                                                              CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!Guid.TryParse(todoId, out var todoGuid))
            {
                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));
            }

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

            return(NoContent());
        }
Пример #4
0
        public async Task <IActionResult> SearchTodoItemsAsync([FromQuery] Client.TodoSearchInfo searchInfo,
                                                               CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

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

            var modelSearchInfo = Converter.TodoSearchInfoConverter.Convert(userGuid, searchInfo ?? new Client.TodoSearchInfo());
            var modelTodoList   = await repository.SearchAsync(modelSearchInfo, cancellationToken).ConfigureAwait(false);

            var clientTodoList = modelTodoList.Select(Converter.TodoItemConverter.Convert).ToImmutableList();

            return(Ok(clientTodoList));
        }