Beispiel #1
0
        public async Task <Todo> UpdateTodoAsync(Todo todo)
        {
            var translatedTodo = TodoTranslator.Translate(todo);

            try
            {
                var foundTodo = TodoDbContext.Todos.FirstOrDefault(t => t.Id == translatedTodo.Id);

                if (foundTodo == null)
                {
                    throw new TodoNotFoundException(foundTodo.Id.ToString());
                }

                foundTodo.Name      = string.IsNullOrEmpty(translatedTodo.Name) ? foundTodo.Name : translatedTodo.Name;
                foundTodo.Completed = translatedTodo.Completed;

                TodoDbContext.Todos.Update(foundTodo);
                await TodoDbContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoExists(translatedTodo))
                {
                    throw new TodoNotFoundException(translatedTodo.Id.ToString());
                }
                else
                {
                    throw;
                }
            }

            return(TodoTranslator.Translate(translatedTodo));
        }
Beispiel #2
0
        public async Task <Todo> CreateTodoAsync(Todo todo)
        {
            var translatedTodo = TodoTranslator.Translate(todo);

            TodoDbContext.Add(translatedTodo);
            await TodoDbContext.SaveChangesAsync();

            return(TodoTranslator.Translate(translatedTodo));
        }
        public async Task <Todo> GetTodoAsync(long id)
        {
            var todo = await TodoDbContext.Todos.FindAsync(id);

            if (todo == null)
            {
                throw new TodoNotFoundException(id.ToString());
            }

            return(TodoTranslator.Translate(todo));
        }
Beispiel #4
0
        public async Task <ActionResult <TodoDTO> > GetTodo(long id)
        {
            try
            {
                var result = await TodoService.GetTodoAsync(id);

                return(TodoTranslator.Translate(result));
            }
            catch (TodoNotFoundException)
            {
                return(NotFound());
            }
        }
Beispiel #5
0
        public async Task <Todo> DeleteTodoAsync(Todo todo)
        {
            var foundTodo = await TodoDbContext.Todos.FindAsync(todo.Id);

            if (foundTodo == null)
            {
                throw new TodoNotFoundException(todo.Id.ToString());
            }

            TodoDbContext.Todos.Remove(foundTodo);
            await TodoDbContext.SaveChangesAsync();

            return(TodoTranslator.Translate(foundTodo));
        }
Beispiel #6
0
        public async Task <ActionResult <TodoDTO> > PostTodo(CreateTodoRequest request)
        {
            var result =
                await TodoService.CreateTodoAsync(
                    new Core.Models.Todo()
            {
                Name      = request.Name,
                Completed = false
            });

            var translatedResult = TodoTranslator.Translate(result);

            return(CreatedAtAction(nameof(GetTodo), new { translatedResult.Id }, translatedResult));
        }
Beispiel #7
0
        public async Task <ActionResult <TodoDTO> > DeleteTodo(long id)
        {
            try
            {
                var deletedTodo =
                    await TodoService.DeleteTodoAsync(
                        new Core.Models.Todo()
                {
                    Id = id
                });

                return(TodoTranslator.Translate(deletedTodo));
            }
            catch (TodoNotFoundException)
            {
                return(NotFound());
            }
        }