2018-12-18 22:18:06 +01:00
|
|
|
module d_webservice_example.business.todo_service;
|
|
|
|
|
|
|
|
import d_webservice_example.model.todo : Todo;
|
|
|
|
import std.uuid : UUID, randomUUID;
|
|
|
|
|
2018-12-21 21:22:06 +01:00
|
|
|
class TodoService
|
2018-12-18 22:18:06 +01:00
|
|
|
{
|
2019-01-03 03:33:42 +01:00
|
|
|
import d_webservice_example.business.todo_notification_service : TodoNotificationService;
|
2018-12-18 22:18:06 +01:00
|
|
|
import d_webservice_example.data.todo_update_do : TodoUpdateDO;
|
2019-01-03 03:33:42 +01:00
|
|
|
import vibe.core.log : logInfo;
|
2018-12-18 22:18:06 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
TodoRepository todoRepository;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-12-21 21:22:06 +01:00
|
|
|
this(TodoRepository todoRepository) nothrow pure @nogc @safe
|
2018-12-18 22:18:06 +01:00
|
|
|
{
|
|
|
|
this.todoRepository = todoRepository;
|
|
|
|
}
|
|
|
|
|
2019-01-03 03:33:42 +01:00
|
|
|
Todo createTodo(const Todo newTodo) @safe
|
2018-12-18 22:18:06 +01:00
|
|
|
{
|
2019-01-03 03:33:42 +01:00
|
|
|
immutable createdTodo = todoRepository.save(Todo(newTodo.title,
|
|
|
|
newTodo.content, randomUUID));
|
|
|
|
logInfo("Created todo %s", createdTodo);
|
|
|
|
return createdTodo;
|
2018-12-18 22:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Todo[] getAllTodos() @safe
|
|
|
|
{
|
2018-12-21 21:22:06 +01:00
|
|
|
return todoRepository.findAll;
|
2018-12-18 22:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Todo getTodoByUuid(const UUID uuid) @safe
|
|
|
|
{
|
|
|
|
import std.exception : enforce;
|
|
|
|
import vibe.http.common : HTTPStatus, HTTPStatusException;
|
|
|
|
|
|
|
|
immutable maybeTodo = todoRepository.findByUuid(uuid);
|
|
|
|
enforce(!maybeTodo.isNull, new HTTPStatusException(HTTPStatus.NotFound));
|
|
|
|
return maybeTodo.get;
|
|
|
|
}
|
|
|
|
|
|
|
|
Todo updateTodo(UUID uuid, const TodoUpdateDO todoUpdate) @safe
|
|
|
|
{
|
|
|
|
auto todo = getTodoByUuid(uuid);
|
|
|
|
if (todoUpdate.title != null)
|
|
|
|
todo.title = todoUpdate.title;
|
|
|
|
if (todoUpdate.content != null)
|
|
|
|
todo.content = todoUpdate.content;
|
|
|
|
|
2019-01-03 03:33:42 +01:00
|
|
|
immutable updatedTodo = todoRepository.save(todo);
|
|
|
|
logInfo("Updated todo %s", updatedTodo);
|
|
|
|
return updatedTodo;
|
2018-12-18 22:18:06 +01:00
|
|
|
}
|
|
|
|
|
2019-01-03 03:33:42 +01:00
|
|
|
Todo deleteTodo(UUID uuid) @safe
|
2018-12-18 22:18:06 +01:00
|
|
|
{
|
|
|
|
immutable todo = getTodoByUuid(uuid);
|
|
|
|
todoRepository.remove(todo);
|
2019-01-03 03:33:42 +01:00
|
|
|
logInfo("Deleted todo %s", todo);
|
|
|
|
return todo;
|
2018-12-18 22:18:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TodoRepository
|
|
|
|
{
|
|
|
|
import std.typecons : Nullable;
|
|
|
|
|
|
|
|
Todo save(Todo todo) @safe;
|
|
|
|
bool remove(Todo todo) @safe;
|
|
|
|
Nullable!Todo findByUuid(UUID uuid) @safe;
|
|
|
|
Todo[] findAll() @safe;
|
|
|
|
}
|