| 486 | } |
| 487 | |
| 488 | int HTTPServer::GetNamespaces(http::Context& ctx) { |
| 489 | const std::string_view sortOrder = ctx.request->params.Get("sort_order"sv); |
| 490 | ComparationResult comp = ComparationResult::NotComparable; |
| 491 | if (sortOrder == "asc"sv) { |
| 492 | comp = ComparationResult::Lt; |
| 493 | } else if (sortOrder == "desc"sv) { |
| 494 | comp = ComparationResult::Gt; |
| 495 | } else if (!sortOrder.empty()) { |
| 496 | return jsonStatus(ctx, http::HttpStatus(http::StatusBadRequest, "Invalid `sort_order` parameter")); |
| 497 | } |
| 498 | |
| 499 | std::vector<reindexer::NamespaceDef> nsDefs; |
| 500 | const auto err = getDB<kRoleDataRead>(ctx).EnumNamespaces(nsDefs, EnumNamespacesOpts().OnlyNames()); |
| 501 | if (!err.ok()) { |
| 502 | return jsonStatus(ctx, http::HttpStatus(err)); |
| 503 | } |
| 504 | |
| 505 | if (comp != ComparationResult::NotComparable) { |
| 506 | boost::sort::pdqsort(nsDefs.begin(), nsDefs.end(), [comp](const NamespaceDef& lhs, const NamespaceDef& rhs) { |
| 507 | return collateCompare<CollateASCII>(lhs.name, rhs.name, SortingPrioritiesTable()) == comp; |
| 508 | }); |
| 509 | } |
| 510 | |
| 511 | auto ser = makeRestrictedWrSerializer(ctx); |
| 512 | { |
| 513 | JsonBuilder builder(ser); |
| 514 | builder.Put("total_items", nsDefs.size()); |
| 515 | auto arrNode = builder.Array("items"); |
| 516 | for (auto& nsDef : nsDefs) { |
| 517 | auto objNode = arrNode.Object(); |
| 518 | objNode.Put("name", nsDef.name); |
| 519 | } |
| 520 | } |
| 521 | return ctx.JSON(http::StatusOK, ser.DetachChunk()); |
| 522 | } |
| 523 | |
| 524 | int HTTPServer::GetNamespace(http::Context& ctx) { |
| 525 | const std::string nsName = urldecode2(ctx.request->urlParams[1]); |