/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Controllers/ContactController.cpp
113 строк
4 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "ContactController.h" #include <iostream> namespace Controllers { ContactController::ContactController(Services::ContactService& contactService) : m_contactService(contactService) {} nlohmann::json ContactController::getContacts(const Core::Network::RequestContext& req) { if(const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); auto contacts = m_contactService.getContacts(*req.userId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(!contacts.empty()) { response["status"] = "ok"; nlohmann::json arr = nlohmann::json::array(); for(const auto& contact : contacts) { nlohmann::json contactObj; contactObj["id"] = contact.id; contactObj["username"] = contact.username; contactObj["display_name"] = contact.displayName; contactObj["phone"] = contact.phone; arr.push_back(contactObj); } response["contacts"] = arr; } else { response["status"] = "error"; response["message"] = "Failed to load contacts"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json ContactController::addContact(const Core::Network::RequestContext& req) { if(const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string contactPhone = json.at("contact_phone"); std::string alias = json.at("alias"); auto contact = m_contactService.addContact(*req.userId, contactPhone, alias); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(contact) { response["status"] = "ok"; nlohmann::json contactObj; contactObj["id"] = contact->id; contactObj["username"] = contact->username; contactObj["display_name"] = contact->displayName; contactObj["phone"] = contact->phone; response["contact"] = contactObj; } else { response["status"] = "error"; response["message"] = "Failed to add contact"; response["alias"] = alias; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json ContactController::removeContact(const Core::Network::RequestContext& req) { if(const auto& jsonData = req.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string contactId = json.at("contact_id"); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(m_contactService.removeContact(*req.userId, contactId)) { response["status"] = "ok"; response["deleted_id"] = contactId; } else { response["status"] = "error"; response["message"] = "Failed to remove contact"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } } // namespace Controllers