From e8b69739edd5ae969720221e0d27d32d32c36f2f Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 05:55:42 +0000 Subject: [PATCH] code: list,hpp,dbal (1 files) --- .../component/crud/list_components.hpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 dbal/cpp/src/entities/component/crud/list_components.hpp diff --git a/dbal/cpp/src/entities/component/crud/list_components.hpp b/dbal/cpp/src/entities/component/crud/list_components.hpp new file mode 100644 index 000000000..28ffd7bec --- /dev/null +++ b/dbal/cpp/src/entities/component/crud/list_components.hpp @@ -0,0 +1,70 @@ +#ifndef DBAL_LIST_COMPONENTS_HPP +#define DBAL_LIST_COMPONENTS_HPP + +#include "../../../store/in_memory_store.hpp" +#include +#include +#include + +namespace dbal { +namespace entities { +namespace component { + +inline Result> list(InMemoryStore& store, const ListOptions& options) { + std::vector components; + std::string page_filter; + + auto filter_it = options.filter.find("pageId"); + if (filter_it != options.filter.end()) { + page_filter = filter_it->second; + } + + for (const auto& [id, component] : store.components) { + (void)id; + if (!page_filter.empty() && component.page_id != page_filter) { + continue; + } + components.push_back(component); + } + + std::sort(components.begin(), components.end(), [](const ComponentHierarchy& a, const ComponentHierarchy& b) { + if (a.page_id != b.page_id) { + return a.page_id < b.page_id; + } + if (a.parent_id.has_value() != b.parent_id.has_value()) { + return a.parent_id.has_value() < b.parent_id.has_value(); + } + if (a.parent_id != b.parent_id) { + return a.parent_id < b.parent_id; + } + if (a.order != b.order) { + return a.order < b.order; + } + return a.id < b.id; + }); + + int page = options.page > 1 ? options.page : 1; + int limit = options.limit > 0 ? options.limit : static_cast(components.size()); + if (limit <= 0) { + limit = static_cast(components.size()); + } + + if (limit == 0 || components.empty()) { + return Result>(std::vector()); + } + + int start = (page - 1) * limit; + if (start >= static_cast(components.size())) { + return Result>(std::vector()); + } + + int end = std::min(start + limit, static_cast(components.size())); + return Result>(std::vector(components.begin() + start, + components.begin() + end)); +} + +} // namespace component +} // namespace entities +} // namespace dbal + +#endif