Commit 940aae65 authored by Johannes Loher's avatar Johannes Loher
Browse files

Use optional package

parent f9f4bbf1
Pipeline #153 passed with stages
......@@ -4,8 +4,9 @@ authors "Johannes Loher"
copyright "Copyright © 2018, Johannes Loher"
license "MIT"
dependency "aedi" version="~>1.0.0"
dependency "fluent-asserts" version="~>0.12.3"
dependency "vibe-d" version="~>0.8.4"
dependency "fluent-asserts" version="~>0.12.4"
dependency "vibe-d" version="~>0.8.5"
dependency "optional" version="~>0.15.0"
configuration "executable" {
targetType "executable"
......
......@@ -2,21 +2,24 @@
"fileVersion": 1,
"versions": {
"aedi": "1.0.0",
"bolts": "0.11.1",
"botan": "1.12.10",
"botan-math": "1.0.3",
"ddmp": "0.0.1-0.dev.3",
"diet-ng": "1.5.0",
"eventcore": "0.8.40",
"fluent-asserts": "0.12.3",
"libasync": "0.8.3",
"eventcore": "0.8.43",
"fluent-asserts": "0.12.4",
"libasync": "0.8.4",
"libdparse": "0.8.8",
"libevent": "2.0.2+2.0.16",
"memutils": "0.4.13",
"mir-linux-kernel": "1.0.1",
"openssl": "1.1.6+1.0.1g",
"optional": "0.15.0",
"silly": "0.8.2",
"stdx-allocator": "2.77.5",
"taggedalgebraic": "0.10.12",
"vibe-core": "1.4.6",
"vibe-d": "0.8.4"
"taggedalgebraic": "0.11.4",
"vibe-core": "1.6.2",
"vibe-d": "0.8.5"
}
}
......@@ -36,10 +36,13 @@ public:
{
import std.exception : enforce;
import vibe.http.common : HTTPStatus, HTTPStatusException;
import optional.optional : orElse;
immutable maybeTodo = todoRepository.findByUuid(uuid);
enforce(!maybeTodo.isNull, new HTTPStatusException(HTTPStatus.NotFound));
return maybeTodo.get;
immutable todo = todoRepository.findByUuid(uuid).orElse!(function Todo() {
throw new HTTPStatusException(HTTPStatus.NotFound);
});
return todo;
}
Todo updateTodo(UUID uuid, const TodoUpdateDO todoUpdate) @safe
......@@ -66,10 +69,10 @@ public:
interface TodoRepository
{
import std.typecons : Nullable;
import optional.optional : Optional;
Todo save(Todo todo) @safe;
bool remove(Todo todo) @safe;
Nullable!Todo findByUuid(UUID uuid) @safe;
Optional!Todo findByUuid(UUID uuid) @safe;
Todo[] findAll() @safe;
}
......@@ -5,22 +5,22 @@ import std.typecons : Nullable;
import std.uuid : UUID;
import vibe.web.rest : path;
@path("/api/v1/todos")
@path("/api/v1/")
interface TodoApi
{
@path("/")
@path("todos")
TodoTO addTodo(string title, string content) @safe;
@path("/")
@path("todos")
TodoTO[] getTodos() @safe;
@path("/:uuid")
@path("todos/:uuid")
TodoTO getTodo(UUID _uuid) @safe;
@path("/:uuid")
@path("todos/:uuid")
TodoTO updateTodo(UUID _uuid, Nullable!string title, Nullable!string content) @safe;
@path("/:uuid")
@path("todos/:uuid")
void deleteTodo(UUID _uuid) @safe;
}
......
......@@ -10,7 +10,7 @@ version (unittest)
class InMemoryTodoRepository : TodoRepository
{
import d_webservice_example.model.todo : Todo;
import std.typecons : Nullable, nullable;
import optional.optional : Optional;
import std.uuid : randomUUID, UUID;
private:
......@@ -71,15 +71,11 @@ public:
return todos.byValue.array;
}
override Nullable!Todo findByUuid(UUID uuid)
override Optional!Todo findByUuid(UUID uuid)
{
import std.algorithm.searching : find;
import d_webservice_example.util.algorithm : findFirst;
auto foundTodos = todos.byValue.find!(todo => todo.uuid == uuid);
if (foundTodos.empty)
return Nullable!Todo.init;
else
return nullable(foundTodos.front);
return todos.byValue.findFirst!(todo => todo.uuid == uuid);
}
override bool remove(Todo todo)
......
......@@ -2,7 +2,6 @@ module d_webservice_example.transport.todo_to;
struct TodoTO
{
import std.typecons : Nullable;
import std.uuid : UUID;
UUID uuid;
......
module d_webservice_example.util.algorithm;
import d_webservice_example.util.meta : from;
auto findFirst(alias pred, InputRange)(InputRange haystack)
if (from!"std.range".isInputRange!InputRange)
{
import optional.optional : toOptional;
import std.algorithm.searching : find;
import std.range : take;
return haystack.find!(pred).take(1).toOptional();
}
module d_webservice_example.util.meta;
template from(string moduleName)
{
mixin("import from = " ~ moduleName ~ ";");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment