87 lines
2.4 KiB
D
87 lines
2.4 KiB
D
module d_webservice_example.controller.todo_controller;
|
|
|
|
import d_webservice_example.transport.todo_to : TodoTO;
|
|
import std.typecons : Nullable;
|
|
import std.uuid : UUID;
|
|
import vibe.web.rest : path;
|
|
|
|
@path("/api/v1/")
|
|
interface TodoApi
|
|
{
|
|
@path("todos")
|
|
TodoTO addTodo(string title, string content) @safe;
|
|
|
|
@path("todos")
|
|
TodoTO[] getTodos() @safe;
|
|
|
|
@path("todos/:uuid")
|
|
TodoTO getTodo(UUID _uuid) @safe;
|
|
|
|
@path("todos/:uuid")
|
|
TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe;
|
|
|
|
@path("todos/:uuid")
|
|
void deleteTodo(UUID _uuid) @safe;
|
|
}
|
|
|
|
class TodoController : TodoApi
|
|
{
|
|
import d_webservice_example.facade.todo_facade : TodoFacade;
|
|
import d_webservice_example.mapper.todo_mapper : asTodoTO;
|
|
import vibe.core.log : logDiagnostic;
|
|
|
|
private:
|
|
TodoFacade todoFacade;
|
|
|
|
public:
|
|
|
|
this(TodoFacade todoFacade) nothrow pure @nogc @safe
|
|
{
|
|
this.todoFacade = todoFacade;
|
|
}
|
|
|
|
override TodoTO addTodo(string title, string content) @safe
|
|
{
|
|
import d_webservice_example.model.todo : Todo;
|
|
|
|
logDiagnostic("Received request to add a todo with title '%s' and content '%s'", title, content);
|
|
return todoFacade.createTodo(Todo(title, content)).asTodoTO;
|
|
}
|
|
|
|
override TodoTO[] getTodos() @safe
|
|
{
|
|
import std.algorithm.iteration : map;
|
|
import std.array : array;
|
|
|
|
logDiagnostic("Received request to get all todos");
|
|
return todoFacade.getAllTodos.map!asTodoTO.array;
|
|
}
|
|
|
|
override TodoTO getTodo(UUID _uuid) @safe
|
|
{
|
|
|
|
logDiagnostic("Received request to get todo '%s'", _uuid);
|
|
return todoFacade.getTodoByUuid(_uuid).asTodoTO;
|
|
}
|
|
|
|
override TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe
|
|
{
|
|
import d_webservice_example.data.todo_update_do : TodoUpdateDO;
|
|
|
|
logDiagnostic("Received request to update todo '%s' with title '%s' and content '%s'",
|
|
_uuid, title, content);
|
|
TodoUpdateDO update;
|
|
if (!title.isNull)
|
|
update.title = title.get;
|
|
if (!content.isNull)
|
|
update.content = content.get;
|
|
|
|
return todoFacade.updateTodo(_uuid, update).asTodoTO;
|
|
}
|
|
|
|
override void deleteTodo(UUID _uuid) @safe
|
|
{
|
|
logDiagnostic("Received request to delete todo '%s'", _uuid);
|
|
todoFacade.deleteTodo(_uuid);
|
|
}
|
|
}
|