Last active
November 21, 2025 21:02
-
-
Save sean3z/eb92d0c7db29f03fa5f24ae23c22437a to your computer and use it in GitHub Desktop.
C++ Hero REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <crow/json.h> | |
| #include <string> | |
| #include <optional> | |
| #include <memory> | |
| namespace models { | |
| class Hero { | |
| public: | |
| // Constructor with default values (C++20 designated initializers would be nice here) | |
| Hero() = default; | |
| ~Hero() = default; | |
| // Constructor from JSON (explicit prevents implicit conversions) | |
| explicit Hero(const crow::json::rvalue& json); | |
| // Getters (const-correct, return by const ref to avoid copies) | |
| std::optional<int64_t> getId() const noexcept { return id_; } | |
| const std::string& getName() const noexcept { return name_; } | |
| const std::string& getSuperPower() const noexcept { return superPower_; } | |
| int getLevel() const noexcept { return level_; } | |
| // Setters (take by value to enable move semantics) | |
| void setId(int64_t id) noexcept { id_ = id; } | |
| void setName(std::string name) noexcept { name_ = std::move(name); } | |
| void setSuperPower(std::string power) noexcept { superPower_ = std::move(power); } | |
| void setLevel(int level) noexcept { level_ = level; } | |
| // Serialization (returns crow::json::wvalue for writing) | |
| crow::json::wvalue toJson() const; | |
| // Validation | |
| bool isValid() const noexcept; | |
| private: | |
| std::optional<int64_t> id_; // optional because it's set by DB | |
| std::string name_; | |
| std::string superPower_; | |
| int level_ = 1; | |
| }; | |
| } // namespace models |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment