]> git.frustrated-labs.net Git - open-the-box.git/commitdiff
feat: add endpoint to fetch list of questions
authorAlexander Goussas <[email protected]>
Sat, 27 Jun 2026 16:18:24 +0000 (11:18 -0500)
committerAlexander Goussas <[email protected]>
Sat, 27 Jun 2026 16:18:24 +0000 (11:18 -0500)
13 files changed:
.clang-format [new file with mode: 0644]
.gitignore [new file with mode: 0644]
.gitmodules [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0644]
README.md [new file with mode: 0644]
main.cpp [new file with mode: 0644]
question.cpp [new file with mode: 0644]
question.hpp [new file with mode: 0644]
question_dto.cpp [new file with mode: 0644]
question_dto.hpp [new file with mode: 0644]
question_repository.cpp [new file with mode: 0644]
question_repository.hpp [new file with mode: 0644]
vendor/Crow [new submodule]

diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..64a22d2
--- /dev/null
@@ -0,0 +1,35 @@
+BasedOnStyle: WebKit
+DerivePointerAlignment: false
+PointerAlignment: Left
+SpaceBeforeParens: ControlStatements
+AlignConsecutiveAssignments: true
+AlignConsecutiveMacros: true
+AlignEscapedNewlines: true
+AlignTrailingComments: true
+IndentWidth: 4
+BreakConstructorInitializers: BeforeComma
+BinPackArguments: false
+AllowAllArgumentsOnNextLine: false
+AllowAllParametersOfDeclarationOnNextLine: false
+BinPackParameters: false
+AlignAfterOpenBracket: AlwaysBreak
+AlwaysBreakTemplateDeclarations: Yes
+ColumnLimit: 80
+AllowShortEnumsOnASingleLine: true
+AllowShortCaseLabelsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: Empty
+AllowShortIfStatementsOnASingleLine: WithoutElse
+AccessModifierOffset: -2
+BreakBeforeBraces: Custom
+BraceWrapping:
+  AfterControlStatement: Always
+  AfterFunction: true
+  AfterNamespace: true
+  AfterStruct: true
+  AfterClass: true
+  BeforeElse: true
+  BeforeCatch: true
+  BeforeLambdaBody: false
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..d4fe85c
--- /dev/null
@@ -0,0 +1,3 @@
+.ccls-cache
+compile_commands.json
+build
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..07b9848
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "vendor/Crow"]
+       path = vendor/Crow
+       url = https://github.com/aloussase/Crow
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..1d87de2
--- /dev/null
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.18)
+
+project(open-the-box)
+
+set(CMAKE_CXX_STANDARD 20)
+
+set(DCROW_BUILD_EXAMPLES OFF)
+set(CROW_BUILD_TESTS OFF)
+set(CROW_ENABLE_SSL OFF)
+add_subdirectory(vendor/Crow)
+
+add_executable(open-the-box main.cpp
+  question.cpp
+  question_repository.cpp
+  question_dto.cpp)
+target_link_libraries(open-the-box PUBLIC Crow::Crow)
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..373c77b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,15 @@
+# open-the-box
+
+## Setup
+
+### MacOS
+
+To make homebrew installed libraries work with CMake, you may need to add
+`/opt/homebrew` to its search path:
+
+```bash
+export CMAKE_PREFIX_PATH="/opt/homebrew:$CMAKE_PREFIX_PATH"
+```
+
+More information on:
+https://stackoverflow.com/questions/79481069/cmake-cannot-found-libraries-installed-by-brew.
diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..a82f30f
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,27 @@
+#include <crow.h>
+
+#include "question.hpp"
+#include "question_dto.hpp"
+#include "question_repository.hpp"
+
+auto main() -> int
+{
+    crow::SimpleApp app;
+
+    InMemoryQuestionRepository questions;
+
+    CROW_ROUTE(app, "/questions")([&]() {
+        auto qs = questions.list_questions();
+        crow::json::wvalue::list dtos;
+        for (auto q : qs)
+        {
+            dtos.push_back(QuestionDto(q).json());
+        }
+
+        return crow::json::wvalue { dtos };
+    });
+
+    app.port(18080).multithreaded().run();
+
+    return 0;
+}
diff --git a/question.cpp b/question.cpp
new file mode 100644 (file)
index 0000000..3170000
--- /dev/null
@@ -0,0 +1,16 @@
+#include "question.hpp"
+
+Question::Question(std::string s) noexcept
+    : m_question { std::move(s) }
+{
+}
+
+Question::Question(const char* s) noexcept
+    : m_question { s }
+{
+}
+
+std::string Question::question() noexcept
+{
+    return m_question;
+}
diff --git a/question.hpp b/question.hpp
new file mode 100644 (file)
index 0000000..071d093
--- /dev/null
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <string>
+
+class Question
+{
+  public:
+    std::string question() noexcept;
+    [[nodiscard]] Question(std::string) noexcept;
+    [[nodiscard]] Question(const char*) noexcept;
+
+  private:
+    std::string m_question;
+};
diff --git a/question_dto.cpp b/question_dto.cpp
new file mode 100644 (file)
index 0000000..8ebc3a8
--- /dev/null
@@ -0,0 +1,13 @@
+#include "question_dto.hpp"
+
+QuestionDto::QuestionDto(Question question) noexcept
+    : m_question(std::move(question.question()))
+{
+}
+
+crow::json::wvalue QuestionDto::json() const noexcept
+{
+    crow::json::wvalue json;
+    json["question"] = m_question;
+    return json;
+}
diff --git a/question_dto.hpp b/question_dto.hpp
new file mode 100644 (file)
index 0000000..34f8ee9
--- /dev/null
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "question.hpp"
+#include <crow.h>
+
+class QuestionDto
+{
+  public:
+    [[nodiscard]] QuestionDto(Question) noexcept;
+    crow::json::wvalue json() const noexcept;
+
+  private:
+    std::string m_question;
+};
diff --git a/question_repository.cpp b/question_repository.cpp
new file mode 100644 (file)
index 0000000..aa2545e
--- /dev/null
@@ -0,0 +1,10 @@
+#include "question_repository.hpp"
+
+static std::vector<Question> questions
+    = { "What does your morning routine look like?",
+        "What do you like the most about your job?" };
+
+std::vector<Question> InMemoryQuestionRepository::list_questions() noexcept
+{
+    return questions;
+}
diff --git a/question_repository.hpp b/question_repository.hpp
new file mode 100644 (file)
index 0000000..4f5a5cc
--- /dev/null
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "question.hpp"
+#include <vector>
+class QuestionRepository
+{
+  public:
+    [[nodiscard]] virtual std::vector<Question> list_questions() noexcept = 0;
+};
+
+class InMemoryQuestionRepository : public QuestionRepository
+{
+  public:
+    std::vector<Question> list_questions() noexcept;
+};
diff --git a/vendor/Crow b/vendor/Crow
new file mode 160000 (submodule)
index 0000000..bbaf5c1
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit bbaf5c1ea0b8f05d53059937a5af6194b5e6b9ff