From d0c979d6968c017baa3035532822a14114e0883a Mon Sep 17 00:00:00 2001 From: Alexander Goussas Date: Sat, 27 Jun 2026 11:18:24 -0500 Subject: [PATCH] feat: add endpoint to fetch list of questions --- .clang-format | 35 +++++++++++++++++++++++++++++++++++ .gitignore | 3 +++ .gitmodules | 3 +++ CMakeLists.txt | 16 ++++++++++++++++ README.md | 15 +++++++++++++++ main.cpp | 27 +++++++++++++++++++++++++++ question.cpp | 16 ++++++++++++++++ question.hpp | 14 ++++++++++++++ question_dto.cpp | 13 +++++++++++++ question_dto.hpp | 14 ++++++++++++++ question_repository.cpp | 10 ++++++++++ question_repository.hpp | 15 +++++++++++++++ vendor/Crow | 1 + 13 files changed, 182 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 main.cpp create mode 100644 question.cpp create mode 100644 question.hpp create mode 100644 question_dto.cpp create mode 100644 question_dto.hpp create mode 100644 question_repository.cpp create mode 100644 question_repository.hpp create mode 160000 vendor/Crow diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..64a22d2 --- /dev/null +++ b/.clang-format @@ -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 index 0000000..d4fe85c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.ccls-cache +compile_commands.json +build diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..07b9848 --- /dev/null +++ b/.gitmodules @@ -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 index 0000000..1d87de2 --- /dev/null +++ b/CMakeLists.txt @@ -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 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 index 0000000..a82f30f --- /dev/null +++ b/main.cpp @@ -0,0 +1,27 @@ +#include + +#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 index 0000000..3170000 --- /dev/null +++ b/question.cpp @@ -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 index 0000000..071d093 --- /dev/null +++ b/question.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +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 index 0000000..8ebc3a8 --- /dev/null +++ b/question_dto.cpp @@ -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 index 0000000..34f8ee9 --- /dev/null +++ b/question_dto.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "question.hpp" +#include + +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 index 0000000..aa2545e --- /dev/null +++ b/question_repository.cpp @@ -0,0 +1,10 @@ +#include "question_repository.hpp" + +static std::vector questions + = { "What does your morning routine look like?", + "What do you like the most about your job?" }; + +std::vector InMemoryQuestionRepository::list_questions() noexcept +{ + return questions; +} diff --git a/question_repository.hpp b/question_repository.hpp new file mode 100644 index 0000000..4f5a5cc --- /dev/null +++ b/question_repository.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "question.hpp" +#include +class QuestionRepository +{ + public: + [[nodiscard]] virtual std::vector list_questions() noexcept = 0; +}; + +class InMemoryQuestionRepository : public QuestionRepository +{ + public: + std::vector list_questions() noexcept; +}; diff --git a/vendor/Crow b/vendor/Crow new file mode 160000 index 0000000..bbaf5c1 --- /dev/null +++ b/vendor/Crow @@ -0,0 +1 @@ +Subproject commit bbaf5c1ea0b8f05d53059937a5af6194b5e6b9ff -- 2.43.0