81 lines
2 KiB
D
81 lines
2 KiB
D
module d_webservice_example.controller.todo_controller;
|
|
|
|
import aermicioi.aedi : autowired, component;
|
|
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;
|
|
}
|
|
|
|
@component class TodoController : TodoApi
|
|
{
|
|
import d_webservice_example.business.todo_service : TodoService;
|
|
import d_webservice_example.mapper.todo_mapper : asTodoTO;
|
|
|
|
private:
|
|
TodoService todoService;
|
|
|
|
public:
|
|
|
|
@autowired this(TodoService todoService) @safe
|
|
{
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|