todo_controller.d 1.92 KB
Newer Older
Johannes Loher's avatar
Johannes Loher committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}

27
class TodoController : TodoApi
Johannes Loher's avatar
Johannes Loher committed
28
29
30
31
32
33
34
35
36
{
    import d_webservice_example.business.todo_service : TodoService;
    import d_webservice_example.mapper.todo_mapper : asTodoTO;

private:
    TodoService todoService;

public:

37
    this(TodoService todoService) nothrow pure @nogc @safe
Johannes Loher's avatar
Johannes Loher committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    {
        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;

55
        return todoService.getAllTodos.map!asTodoTO.array;
Johannes Loher's avatar
Johannes Loher committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    }

    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);
    }
}