d-webservice-example/source/d_webservice_example/controller/todo_controller.d

88 lines
2.4 KiB
D
Raw Normal View History

2018-12-18 22:18:06 +01:00
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/todos")
interface TodoApi
{
@path("/")
TodoTO addTodo(string title, string content) @safe;
@path("/")
TodoTO[] getTodos() @safe;
@path("/:uuid")
TodoTO getTodo(UUID _uuid) @safe;
@path("/:uuid")
TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe;
@path("/:uuid")
void deleteTodo(UUID _uuid) @safe;
}
class TodoController : TodoApi
2018-12-18 22:18:06 +01:00
{
2019-01-03 03:33:42 +01:00
import d_webservice_example.facade.todo_facade : TodoFacade;
2018-12-18 22:18:06 +01:00
import d_webservice_example.mapper.todo_mapper : asTodoTO;
2019-01-03 03:33:42 +01:00
import vibe.core.log : logDiagnostic;
2018-12-18 22:18:06 +01:00
private:
2019-01-03 03:33:42 +01:00
TodoFacade todoFacade;
2018-12-18 22:18:06 +01:00
public:
2019-01-03 03:33:42 +01:00
this(TodoFacade todoFacade) nothrow pure @nogc @safe
2018-12-18 22:18:06 +01:00
{
2019-01-03 03:33:42 +01:00
this.todoFacade = todoFacade;
2018-12-18 22:18:06 +01:00
}
override TodoTO addTodo(string title, string content) @safe
{
import d_webservice_example.model.todo : Todo;
2019-01-03 03:33:42 +01:00
logDiagnostic("Received request to add a todo with title '%s' and content '%s'", title, content);
return todoFacade.createTodo(Todo(title, content)).asTodoTO;
2018-12-18 22:18:06 +01:00
}
override TodoTO[] getTodos() @safe
{
import std.algorithm.iteration : map;
import std.array : array;
2019-01-03 03:33:42 +01:00
logDiagnostic("Received request to get all todos");
return todoFacade.getAllTodos.map!asTodoTO.array;
2018-12-18 22:18:06 +01:00
}
override TodoTO getTodo(UUID _uuid) @safe
{
2019-01-03 03:33:42 +01:00
logDiagnostic("Received request to get todo '%s'", _uuid);
return todoFacade.getTodoByUuid(_uuid).asTodoTO;
2018-12-18 22:18:06 +01:00
}
override TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe
{
import d_webservice_example.data.todo_update_do : TodoUpdateDO;
2019-01-03 03:33:42 +01:00
logDiagnostic("Received request to update todo '%s' with title '%s' and content '%s'",
_uuid, title, content);
2018-12-18 22:18:06 +01:00
TodoUpdateDO update;
if (!title.isNull)
update.title = title.get;
if (!content.isNull)
update.content = content.get;
2019-01-03 03:33:42 +01:00
return todoFacade.updateTodo(_uuid, update).asTodoTO;
2018-12-18 22:18:06 +01:00
}
override void deleteTodo(UUID _uuid) @safe
{
2019-01-03 03:33:42 +01:00
logDiagnostic("Received request to delete todo '%s'", _uuid);
todoFacade.deleteTodo(_uuid);
2018-12-18 22:18:06 +01:00
}
}