0xc3 1 lună în urmă
părinte
comite
b48a7dd277

+ 89 - 3
src/saura/cmd/sandbox/sandbox.cpp

@@ -10,6 +10,7 @@
 
 #include "saura/cmd/sandbox/client/client.hpp"
 #include "saura/cmd/sandbox/folder/folder.hpp"
+#include "saura/cmd/sandbox/user/user.hpp"
 #include "server/server.hpp"
 
 namespace saura {
@@ -20,6 +21,7 @@ static int32_t selected_node = -1;
 struct SandboxApp : AppBase {
   std::shared_ptr<ServerAPI> server_api_ctx;
   std::vector<ClientInfo> clients;
+  std::vector<UserInfo> users;
   Folder category_root;
 
   void start() override {
@@ -27,6 +29,7 @@ struct SandboxApp : AppBase {
 
     this->server_api_ctx = std::make_shared<ServerAPI>();
     this->clients = server_api_ctx->fetch_clients(10, 0);
+    this->users = server_api_ctx->fetch_users(10, 0);
 
     category_root = Folder("root");
     category_root.add("A")->add("B");
@@ -51,8 +54,9 @@ struct SandboxApp : AppBase {
 
         ImGuiTreeNodeFlags node_flags =
             ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth;
-        bool open = ImGui::TreeNodeEx(fmt::format("{}#{}", f.name.c_str(), i).c_str(), node_flags, "%s",
-                                      f.name.c_str());
+        bool open =
+            ImGui::TreeNodeEx(fmt::format("{}#{}", f.name.c_str(), i).c_str(),
+                              node_flags, "%s", f.name.c_str());
 
         if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
           spdlog::info("Clieked: {}", i);
@@ -259,8 +263,90 @@ struct SandboxApp : AppBase {
         ImGui::EndTabItem();
       }
 
+      if (ImGui::BeginTabItem("Пользователи")) {
+        ImGuiTableFlags flags =
+            ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders |
+            ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_ScrollY;
+
+        if (ImGui::Button("Создать пользователя")) {
+          ImGui::OpenPopup("Test");
+        }
+
+        if (ImGui::BeginPopupModal("Test", NULL,
+                                   ImGuiWindowFlags_AlwaysAutoResize)) {
+          ImGui::Text("This is the modal content!");
+          ImGui::Separator();
+
+          if (ImGui::Button("Close", ImVec2(120, 0))) {
+            ImGui::CloseCurrentPopup();
+          }
+          ImGui::EndPopup();
+        }
+
+        if (ImGui::BeginTable("UserDataTable", 9, flags)) {
+          ImGui::TableSetupColumn("#");
+          ImGui::TableSetupColumn("Логин");
+          ImGui::TableSetupColumn("Роль");
+          ImGui::TableSetupColumn("ФИО");
+          ImGui::TableSetupColumn("Телефон");
+          ImGui::TableSetupColumn("Эл. почта");
+          ImGui::TableSetupColumn("Дата регистрации");
+          ImGui::TableSetupColumn(""); // Edit
+          ImGui::TableSetupColumn(""); // Delete
+          ImGui::TableHeadersRow();
+
+          int32_t selected_row_idx = -1;
+          for (size_t idx = 0; idx < this->users.size(); idx++) {
+            auto &client = this->users[idx];
+
+            ImGui::TableNextRow();
+
+            // #
+            ImGui::TableNextColumn();
+            ImGui::Text("%03d", (int)idx + 1);
+
+            // Логин
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.login.c_str());
+
+            // Роль
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.role.c_str());
+
+            // ФИО
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.full_name.c_str());
+
+            // Телефон
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.phone.c_str());
+
+            // Эл. почта
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.email.c_str());
+
+            // Дата регистрации
+            ImGui::TableNextColumn();
+            ImGui::Text("%s", client.reg_date.c_str());
+
+            ImGui::TableNextColumn();
+            ImGui::PushID(fmt::format("{}Edit", idx).c_str());
+            ImGui::Button("Edit");
+            ImGui::PopID();
+
+            ImGui::TableNextColumn();
+            ImGui::PushID(fmt::format("{}Delete", idx).c_str());
+            ImGui::Button("Delete");
+            ImGui::PopID();
+          }
+
+          ImGui::EndTable();
+        }
+        ImGui::EndTabItem();
+      }
+
       if (ImGui::BeginTabItem("Настройки")) {
-        ImGui::Text("Hello from the third tab.");
+        ImGui::Text("Hello");
         ImGui::EndTabItem();
       }
       ImGui::EndTabBar();

+ 34 - 0
src/saura/cmd/sandbox/server/server.cpp

@@ -1,5 +1,6 @@
 #include "server.hpp"
 #include "saura/cmd/sandbox/client/client.hpp"
+#include "saura/cmd/sandbox/user/user.hpp"
 #include <chrono>
 #include <cpr/api.h>
 #include <cpr/cprtypes.h>
@@ -67,4 +68,37 @@ std::vector<ClientInfo> ServerAPI::fetch_clients(uint32_t limit,
   return clients;
 }
 
+std::vector<UserInfo> ServerAPI::fetch_users(uint32_t limit,
+                                                 uint32_t offset) {
+
+  std::vector<UserInfo> users;
+
+  std::string url = fmt::format(
+      "http://api.localhost:8090/v1/example/users?limit={}&offset={}", limit,
+      offset);
+  auto res = cpr::Get(cpr::Url(url), cpr::Timeout(1000));
+  if (res.status_code == 200) {
+    auto res_json = nlohmann::json::parse(res.text);
+    spdlog::info("Response: {}", res_json.dump(2));
+
+    if (!res_json.is_array()) {
+      throw std::runtime_error("Expected JSON array but got " +
+                               std::string(res_json.type_name()));
+    }
+    for (size_t i = 0; i < res_json.size(); ++i) {
+      const auto &user_json = res_json[i];
+
+      UserInfo user = UserInfo::from_json(user_json);
+      users.push_back(user);
+
+      spdlog::info("✅ User");
+      spdlog::info("   ID: {}", user.id);
+      spdlog::info("   Login: {}", user.login);
+      spdlog::info("   FullName: {}", user.full_name);
+    }
+  }
+
+  return users;
+}
+
 } // namespace saura

+ 2 - 0
src/saura/cmd/sandbox/server/server.hpp

@@ -4,6 +4,7 @@
 #include <thread>
 
 #include "saura/cmd/sandbox/client/client.hpp"
+#include "saura/cmd/sandbox/user/user.hpp"
 
 namespace saura {
 class ServerAPI {
@@ -17,6 +18,7 @@ public:
   void run();
 
   std::vector<ClientInfo> fetch_clients(uint32_t limit, uint32_t offset);
+  std::vector<UserInfo> fetch_users(uint32_t limit, uint32_t offset);
 };
 }; // namespace saura
 

+ 73 - 0
src/saura/cmd/sandbox/user/user.hpp

@@ -0,0 +1,73 @@
+#ifndef SANDBOX_USER_HPP_
+#define SANDBOX_USER_HPP_
+
+#include <fmt/core.h>
+#include <fmt/format.h>
+#include <string>
+
+#include <nlohmann/json.hpp>
+
+namespace saura {
+
+class UserInfo {
+public:
+  std::string id;
+  std::string login;
+  std::string password;
+  std::string phone;
+  std::string email;
+  std::string full_name;
+  std::string role;
+  std::string reg_date;
+
+public:
+  UserInfo() = default;
+
+  static void to_json(nlohmann::json &j, const UserInfo &client) {
+    j = nlohmann::json{
+        {"id", client.id},
+        {"login", client.login},
+        {"password", client.password},
+        {"phone", client.phone},
+        {"email", client.email},
+        {"full_name", client.full_name},
+        {"role", client.role},
+        {"reg_date", client.reg_date},
+    };
+  }
+
+  static void from_json(const nlohmann::json &j, UserInfo &client) {
+    j.at("id").get_to(client.id);
+    j.at("login").get_to(client.login);
+    j.at("password").get_to(client.password);
+    j.at("phone").get_to(client.phone);
+    j.at("email").get_to(client.email);
+    j.at("full_name").get_to(client.full_name);
+    j.at("role").get_to(client.role);
+    j.at("reg_date").get_to(client.reg_date);
+  }
+
+  static UserInfo from_json(const nlohmann::json &j) {
+    UserInfo user;
+    UserInfo::from_json(j, user);
+    return user;
+  }
+
+  nlohmann::json to_json() const {
+    nlohmann::json j;
+    UserInfo::to_json(j, *this);
+    return j;
+  }
+};
+
+inline void to_json(nlohmann::json &j, const UserInfo &user) {
+  UserInfo::to_json(j, user);
+}
+
+inline void from_json(const nlohmann::json &j, UserInfo &user) {
+  UserInfo::from_json(j, user);
+}
+
+} // namespace saura
+
+#endif