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

81 lines
1.9 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
{
import d_webservice_example.business.todo_service : TodoService;
import d_webservice_example.mapper.todo_mapper : asTodoTO;
private:
TodoService todoService;
public:
this(TodoService todoService) nothrow pure @nogc @safe
2018-12-18 22:18:06 +01:00
{
this.todoService = todoService;
}
// TODO: validation
override TodoTO addTodo(string title, string content) @safe
{
import d_webservice_example.model.todo : Todo;
return todoService.createTodo(Todo(title, content)).asTodoTO;
}
override TodoTO[] getTodos() @safe
{
import std.algorithm.iteration : map;
import std.array : array;
return todoService.getAllTodos.map!asTodoTO.array;
2018-12-18 22:18:06 +01:00
}
override TodoTO getTodo(UUID _uuid) @safe
{
return todoService.getTodoByUuid(_uuid).asTodoTO;
}
override TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe
{
import d_webservice_example.data.todo_update_do : TodoUpdateDO;
TodoUpdateDO update;
if (!title.isNull)
update.title = title.get;
if (!content.isNull)
update.content = content.get;
return todoService.updateTodo(_uuid, update).asTodoTO;
}
override void deleteTodo(UUID _uuid) @safe
{
todoService.deleteTodo(_uuid);
}
}