From 53c6faae4ddde5e861d8365ffcc2a889747ef043 Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 00:51:52 +0000 Subject: [PATCH] code: hpp,dbal,cpp (6 files) --- .../entities/lua_script/create_lua_script.hpp | 50 +++++++++++++++ .../entities/lua_script/delete_lua_script.hpp | 38 ++++++++++++ .../entities/lua_script/get_lua_script.hpp | 36 +++++++++++ dbal/cpp/src/entities/lua_script/index.hpp | 14 +++++ .../entities/lua_script/list_lua_scripts.hpp | 62 +++++++++++++++++++ .../entities/lua_script/update_lua_script.hpp | 61 ++++++++++++++++++ 6 files changed, 261 insertions(+) create mode 100644 dbal/cpp/src/entities/lua_script/index.hpp diff --git a/dbal/cpp/src/entities/lua_script/create_lua_script.hpp b/dbal/cpp/src/entities/lua_script/create_lua_script.hpp index e69de29bb..206797774 100644 --- a/dbal/cpp/src/entities/lua_script/create_lua_script.hpp +++ b/dbal/cpp/src/entities/lua_script/create_lua_script.hpp @@ -0,0 +1,50 @@ +/** + * @file create_lua_script.hpp + * @brief Create Lua script operation + */ +#ifndef DBAL_CREATE_LUA_SCRIPT_HPP +#define DBAL_CREATE_LUA_SCRIPT_HPP + +#include "dbal/types.hpp" +#include "dbal/errors.hpp" +#include "../../store/in_memory_store.hpp" +#include "../../validation/lua_script_validation.hpp" + +namespace dbal { +namespace entities { +namespace lua_script { + +/** + * Create a new Lua script in the store + */ +inline Result create(InMemoryStore& store, const CreateLuaScriptInput& input) { + if (input.name.empty() || input.name.length() > 100) { + return Error::validationError("Name must be between 1 and 100 characters"); + } + if (input.code.empty()) { + return Error::validationError("Script code cannot be empty"); + } + if (!validation::isValidLuaSyntax(input.code)) { + return Error::validationError("Invalid Lua syntax"); + } + + LuaScript script; + script.id = store.generateId("lua_script", ++store.lua_script_counter); + script.name = input.name; + script.description = input.description; + script.code = input.code; + script.category = input.category; + script.is_active = input.is_active; + script.created_at = std::chrono::system_clock::now(); + script.updated_at = script.created_at; + + store.lua_scripts[script.id] = script; + + return Result(script); +} + +} // namespace lua_script +} // namespace entities +} // namespace dbal + +#endif diff --git a/dbal/cpp/src/entities/lua_script/delete_lua_script.hpp b/dbal/cpp/src/entities/lua_script/delete_lua_script.hpp index e69de29bb..fce4a606e 100644 --- a/dbal/cpp/src/entities/lua_script/delete_lua_script.hpp +++ b/dbal/cpp/src/entities/lua_script/delete_lua_script.hpp @@ -0,0 +1,38 @@ +/** + * @file delete_lua_script.hpp + * @brief Delete Lua script operation + */ +#ifndef DBAL_DELETE_LUA_SCRIPT_HPP +#define DBAL_DELETE_LUA_SCRIPT_HPP + +#include "dbal/types.hpp" +#include "dbal/errors.hpp" +#include "../../store/in_memory_store.hpp" + +namespace dbal { +namespace entities { +namespace lua_script { + +/** + * Delete a Lua script by ID + */ +inline Result remove(InMemoryStore& store, const std::string& id) { + if (id.empty()) { + return Error::validationError("Script ID cannot be empty"); + } + + auto it = store.lua_scripts.find(id); + if (it == store.lua_scripts.end()) { + return Error::notFound("Lua script not found: " + id); + } + + store.lua_scripts.erase(it); + + return Result(true); +} + +} // namespace lua_script +} // namespace entities +} // namespace dbal + +#endif diff --git a/dbal/cpp/src/entities/lua_script/get_lua_script.hpp b/dbal/cpp/src/entities/lua_script/get_lua_script.hpp index e69de29bb..6d76b02f5 100644 --- a/dbal/cpp/src/entities/lua_script/get_lua_script.hpp +++ b/dbal/cpp/src/entities/lua_script/get_lua_script.hpp @@ -0,0 +1,36 @@ +/** + * @file get_lua_script.hpp + * @brief Get Lua script by ID operation + */ +#ifndef DBAL_GET_LUA_SCRIPT_HPP +#define DBAL_GET_LUA_SCRIPT_HPP + +#include "dbal/types.hpp" +#include "dbal/errors.hpp" +#include "../../store/in_memory_store.hpp" + +namespace dbal { +namespace entities { +namespace lua_script { + +/** + * Get a Lua script by ID + */ +inline Result get(InMemoryStore& store, const std::string& id) { + if (id.empty()) { + return Error::validationError("Script ID cannot be empty"); + } + + auto it = store.lua_scripts.find(id); + if (it == store.lua_scripts.end()) { + return Error::notFound("Lua script not found: " + id); + } + + return Result(it->second); +} + +} // namespace lua_script +} // namespace entities +} // namespace dbal + +#endif diff --git a/dbal/cpp/src/entities/lua_script/index.hpp b/dbal/cpp/src/entities/lua_script/index.hpp new file mode 100644 index 000000000..c7e79924b --- /dev/null +++ b/dbal/cpp/src/entities/lua_script/index.hpp @@ -0,0 +1,14 @@ +/** + * @file index.hpp + * @brief Barrel include for Lua script operations + */ +#ifndef DBAL_LUA_SCRIPT_INDEX_HPP +#define DBAL_LUA_SCRIPT_INDEX_HPP + +#include "create_lua_script.hpp" +#include "get_lua_script.hpp" +#include "update_lua_script.hpp" +#include "delete_lua_script.hpp" +#include "list_lua_scripts.hpp" + +#endif diff --git a/dbal/cpp/src/entities/lua_script/list_lua_scripts.hpp b/dbal/cpp/src/entities/lua_script/list_lua_scripts.hpp index e69de29bb..e3ee3b91f 100644 --- a/dbal/cpp/src/entities/lua_script/list_lua_scripts.hpp +++ b/dbal/cpp/src/entities/lua_script/list_lua_scripts.hpp @@ -0,0 +1,62 @@ +/** + * @file list_lua_scripts.hpp + * @brief List Lua scripts with filtering and pagination + */ +#ifndef DBAL_LIST_LUA_SCRIPTS_HPP +#define DBAL_LIST_LUA_SCRIPTS_HPP + +#include "dbal/types.hpp" +#include "dbal/errors.hpp" +#include "../../store/in_memory_store.hpp" +#include + +namespace dbal { +namespace entities { +namespace lua_script { + +/** + * List Lua scripts with filtering and pagination + */ +inline Result> list(InMemoryStore& store, const ListOptions& options) { + std::vector scripts; + + for (const auto& [id, script] : store.lua_scripts) { + bool matches = true; + + if (options.filter.find("is_active") != options.filter.end()) { + bool filter_active = options.filter.at("is_active") == "true"; + if (script.is_active != filter_active) matches = false; + } + + if (options.filter.find("category") != options.filter.end()) { + if (script.category != options.filter.at("category")) matches = false; + } + + if (matches) scripts.push_back(script); + } + + if (options.sort.find("name") != options.sort.end()) { + std::sort(scripts.begin(), scripts.end(), [](const LuaScript& a, const LuaScript& b) { + return a.name < b.name; + }); + } else if (options.sort.find("created_at") != options.sort.end()) { + std::sort(scripts.begin(), scripts.end(), [](const LuaScript& a, const LuaScript& b) { + return a.created_at < b.created_at; + }); + } + + int start = (options.page - 1) * options.limit; + int end = std::min(start + options.limit, static_cast(scripts.size())); + + if (start < static_cast(scripts.size())) { + return Result>(std::vector(scripts.begin() + start, scripts.begin() + end)); + } + + return Result>(std::vector()); +} + +} // namespace lua_script +} // namespace entities +} // namespace dbal + +#endif diff --git a/dbal/cpp/src/entities/lua_script/update_lua_script.hpp b/dbal/cpp/src/entities/lua_script/update_lua_script.hpp index e69de29bb..249f3cf35 100644 --- a/dbal/cpp/src/entities/lua_script/update_lua_script.hpp +++ b/dbal/cpp/src/entities/lua_script/update_lua_script.hpp @@ -0,0 +1,61 @@ +/** + * @file update_lua_script.hpp + * @brief Update Lua script operation + */ +#ifndef DBAL_UPDATE_LUA_SCRIPT_HPP +#define DBAL_UPDATE_LUA_SCRIPT_HPP + +#include "dbal/types.hpp" +#include "dbal/errors.hpp" +#include "../../store/in_memory_store.hpp" +#include "../../validation/lua_script_validation.hpp" + +namespace dbal { +namespace entities { +namespace lua_script { + +/** + * Update an existing Lua script + */ +inline Result update(InMemoryStore& store, const std::string& id, const UpdateLuaScriptInput& input) { + if (id.empty()) { + return Error::validationError("Script ID cannot be empty"); + } + + auto it = store.lua_scripts.find(id); + if (it == store.lua_scripts.end()) { + return Error::notFound("Lua script not found: " + id); + } + + LuaScript& script = it->second; + + if (input.name.has_value()) { + if (input.name.value().empty() || input.name.value().length() > 100) { + return Error::validationError("Name must be between 1 and 100 characters"); + } + script.name = input.name.value(); + } + + if (input.code.has_value()) { + if (input.code.value().empty()) { + return Error::validationError("Script code cannot be empty"); + } + if (!validation::isValidLuaSyntax(input.code.value())) { + return Error::validationError("Invalid Lua syntax"); + } + script.code = input.code.value(); + } + + if (input.description.has_value()) script.description = input.description.value(); + if (input.category.has_value()) script.category = input.category.value(); + if (input.is_active.has_value()) script.is_active = input.is_active.value(); + + script.updated_at = std::chrono::system_clock::now(); + return Result(script); +} + +} // namespace lua_script +} // namespace entities +} // namespace dbal + +#endif