From 395718da9fbb3d5ee88a789df97a4e88f141f92e Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 06:16:40 +0000 Subject: [PATCH] code: search,hpp,dbal (1 files) --- .../component/crud/search_components.hpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 dbal/cpp/src/entities/component/crud/search_components.hpp diff --git a/dbal/cpp/src/entities/component/crud/search_components.hpp b/dbal/cpp/src/entities/component/crud/search_components.hpp new file mode 100644 index 000000000..09cbfb543 --- /dev/null +++ b/dbal/cpp/src/entities/component/crud/search_components.hpp @@ -0,0 +1,83 @@ +#ifndef DBAL_SEARCH_COMPONENTS_HPP +#define DBAL_SEARCH_COMPONENTS_HPP + +#include "dbal/errors.hpp" +#include "../../../store/in_memory_store.hpp" +#include +#include +#include +#include +#include +#include + +namespace dbal { +namespace entities { +namespace component { + +namespace { + +inline std::string toLower(const std::string& value) { + std::string lowered; + lowered.reserve(value.size()); + std::transform(value.begin(), value.end(), std::back_inserter(lowered), [](unsigned char c) { + return static_cast(std::tolower(c)); + }); + return lowered; +} + +inline bool containsInsensitive(const std::string& text, const std::string& query) { + if (query.empty()) { + return false; + } + return toLower(text).find(toLower(query)) != std::string::npos; +} + +} // namespace + +inline Result> search(InMemoryStore& store, + const std::string& query, + const std::optional& page_id = std::nullopt, + int limit = 20) { + if (query.empty()) { + return Error::validationError("search query is required"); + } + + std::vector matches; + for (const auto& [id, component] : store.components) { + (void)id; + if (page_id.has_value() && component.page_id != page_id.value()) { + continue; + } + bool matchesQuery = containsInsensitive(component.component_type, query); + if (!matchesQuery) { + for (const auto& [key, value] : component.props) { + if (containsInsensitive(key, query) || containsInsensitive(value, query)) { + matchesQuery = true; + break; + } + } + } + if (matchesQuery) { + matches.push_back(component); + } + } + + std::sort(matches.begin(), matches.end(), [](const ComponentHierarchy& a, const ComponentHierarchy& b) { + if (a.component_type != b.component_type) { + return a.component_type < b.component_type; + } + return a.order < b.order; + }); + + if (limit > 0 && static_cast(matches.size()) > limit) { + matches.resize(limit); + } + + return Result>(matches); +} + +} // namespace component +} // namespace entities +} // namespace dbal + +#endif