Skip to content

Commit

Permalink
Merge pull request #61 from Serveis-Neby/task/152-get-service-self
Browse files Browse the repository at this point in the history
Task/152 get service self
  • Loading branch information
JayexDesigns authored May 13, 2024
2 parents 5073d9e + 694ff7d commit 06d9719
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/controllers/service_controller.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <crow.h>

#include <format>
#include <memory>
#include <pqxx/pqxx>
Expand All @@ -26,6 +25,7 @@ class ServiceController {
static void create_service(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_services(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_service_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& service_id);
static void get_services_self(pqxx::connection& db, const crow::request& req, crow::response& res);
static void delete_service(pqxx::connection& db, const crow::request& req, crow::response& res, std::string service_id);
static void update_service(pqxx::connection& db, const crow::request& req, crow::response& res, std::string service_id);
};
2 changes: 2 additions & 0 deletions include/models/service_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ServiceModel {

static std::unique_ptr<ServiceModel> get_service_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false);

static std::vector<std::unique_ptr<ServiceModel>> get_services_self(pqxx::connection& db, const std::string& creator_id, const std::string& status = "");

static std::unique_ptr<ServiceModel> delete_service_by_id(pqxx::connection& db, const std::string id, bool throw_when_null = false);

static bool update_service_by_id(pqxx::connection& db, const std::string id, const std::string tittle, const std::string description, const int price);
Expand Down
78 changes: 77 additions & 1 deletion src/controllers/service_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ void ServiceController::get_services(pqxx::connection &db, const crow::request &
std::vector<std::unique_ptr<ServiceModel>> all_services;

auto status = req.url_params.get("status");

if (!status) {
all_services = ServiceModel::get_services(db, user.get()->get_community_id());

Expand Down Expand Up @@ -202,6 +201,83 @@ void ServiceController::get_services(pqxx::connection &db, const crow::request &
}
}

void ServiceController::get_services_self(pqxx::connection &db, const crow::request &req, crow::response &res) {
try {
crow::json::rvalue get = crow::json::load(req.body);
std::string user_id = get["id"].s();

std::vector<std::unique_ptr<ServiceModel>> all_services;

auto status = req.url_params.get("status");

if (!status) {
all_services = ServiceModel::get_services_self(db, user_id);

} else if (status && (std::string(status) == ServiceStatus::CLOSED || std::string(status) == ServiceStatus::OPEN)) {
all_services = ServiceModel::get_services_self(db, user_id, status);
} else {
handle_error(res, "status not valid value", 400);
return;
}

crow::json::wvalue::list services;
for (unsigned int i = 0; i < all_services.size(); ++i) {
crow::json::wvalue service;

service["id"] = all_services[i].get()->get_id();
service["creator_id"] = all_services[i].get()->get_creator_id();
if (all_services[i].get()->get_buyer_id().has_value())
service["buyer_id"] = all_services[i].get()->get_buyer_id().value();
service["title"] = all_services[i].get()->get_title();
service["description"] = all_services[i].get()->get_description();
service["price"] = all_services[i].get()->get_price();
service["status"] = all_services[i].get()->get_status();
service["type"] = all_services[i].get()->get_type();
if (all_services[i].get()->get_image_url().has_value())
service["image_url"] = all_services[i].get()->get_image_url().value();
service["created_at"] = all_services[i].get()->get_created_at();
service["updated_at"] = all_services[i].get()->get_updated_at();

if (all_services[i].get()->get_creator().has_value()) {
crow::json::wvalue creator;
creator["id"] = all_services[i].get()->get_creator().value().get_id();
creator["username"] = all_services[i].get()->get_creator().value().get_username();
creator["type"] = all_services[i].get()->get_creator().value().get_type();
creator["email"] = all_services[i].get()->get_creator().value().get_email();
creator["balance"] = all_services[i].get()->get_creator().value().get_balance();
creator["created_at"] = all_services[i].get()->get_creator().value().get_created_at();
creator["updated_at"] = all_services[i].get()->get_creator().value().get_updated_at();

service["creator"] = crow::json::wvalue(creator);
}

if (all_services[i].get()->get_buyer().has_value()) {
crow::json::wvalue buyer;
buyer["id"] = all_services[i].get()->get_buyer().value().get_id();
buyer["username"] = all_services[i].get()->get_buyer().value().get_username();
buyer["type"] = all_services[i].get()->get_buyer().value().get_type();
buyer["email"] = all_services[i].get()->get_buyer().value().get_email();
buyer["balance"] = all_services[i].get()->get_buyer().value().get_balance();
buyer["created_at"] = all_services[i].get()->get_buyer().value().get_created_at();
buyer["updated_at"] = all_services[i].get()->get_buyer().value().get_updated_at();

service["buyer"] = crow::json::wvalue(buyer);
}

services.push_back(service);
}

crow::json::wvalue data{{"self_services", services}};

res.write(data.dump());
res.code = 200;
res.end();
} catch (const std::exception &e) {
std::cerr << "Error getting user services: " << e.what() << std::endl;
handle_error(res, "internal server error", 500);
}
}

void ServiceController::delete_service(pqxx::connection &db, const crow::request &req, crow::response &res, std::string service_id) {
try {
crow::json::rvalue request = crow::json::load(req.body);
Expand Down
109 changes: 109 additions & 0 deletions src/models/service_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,115 @@ std::unique_ptr<ServiceModel> ServiceModel::get_service_by_id(pqxx::connection&
return get_service(db, "id", id, throw_when_null);
}

std::vector<std::unique_ptr<ServiceModel>> ServiceModel::get_services_self(pqxx::connection& db, const std::string& creator_id, const std::string& status) {
std::vector<std::unique_ptr<ServiceModel>> all_services;

pqxx::work txn(db);

std::string query =
"SELECT s.id AS service_id, "
"s.creator_id, "
"s.buyer_id, "
"s.title, "
"s.description, "
"s.price, "
"s.status, "
"s.type, "
"s.image_url, "
"s.created_at, "
"s.updated_at, "
"uc.id AS creator_id, "
"uc.community_id AS creator_community_id, "
"uc.username AS creator_username, "
"uc.email AS creator_email, "
"uc.type AS creator_type, "
"uc.balance AS creator_balance, "
"uc.created_at AS creator_created_at, "
"uc.updated_at AS creator_updated_at, "
"ub.id AS buyer_id, "
"ub.community_id AS buyer_community_id, "
"ub.username AS buyer_username, "
"ub.email AS buyer_email, "
"ub.type AS buyer_type, "
"ub.balance AS buyer_balance, "
"ub.created_at AS buyer_created_at, "
"ub.updated_at AS buyer_updated_at "
"FROM services AS s "
"JOIN users AS uc ON s.creator_id = uc.id "
"LEFT JOIN users AS ub ON s.buyer_id = ub.id "
"WHERE s.creator_id = $1";

// Agregar filtro de status si se proporciona
if (!status.empty()) {
query += " AND s.status = $2";
}

pqxx::result result;
if (!status.empty()) {
result = txn.exec_params(query, creator_id, status);
} else {
result = txn.exec_params(query, creator_id);
}

txn.commit();

for (const auto& row : result) {
std::optional<std::string> buyer_id_field;
std::optional<std::string> image_url_field;
if (!row["buyer_id"].is_null())
buyer_id_field = row["buyer_id"].as<std::string>();
else
buyer_id_field = std::nullopt;
if (!row["image_url"].is_null())
image_url_field = row["image_url"].as<std::string>();
else
image_url_field = std::nullopt;

// Crear instancia de UserModel para el creador
UserModel creator(
row["creator_id"].as<std::string>(),
row["creator_community_id"].as<std::string>(),
row["creator_username"].as<std::string>(),
row["creator_email"].as<std::string>(),
row["creator_type"].as<std::string>(),
row["creator_balance"].as<int>(),
row["creator_created_at"].as<std::string>(),
row["creator_updated_at"].as<std::string>());

// Crear instancia de UserModel para el comprador, si existe
UserModel buyer;
if (buyer_id_field) {
buyer = UserModel(
row["buyer_id"].as<std::string>(),
row["buyer_community_id"].as<std::string>(),
row["buyer_username"].as<std::string>(),
row["buyer_email"].as<std::string>(),
row["buyer_type"].as<std::string>(),
row["buyer_balance"].as<int>(),
row["buyer_created_at"].as<std::string>(),
row["buyer_updated_at"].as<std::string>());
}

// Crear instancia de ServiceModel con UserModel como argumento adicional
all_services.push_back(std::make_unique<ServiceModel>(
row["service_id"].as<std::string>(),
row["creator_id"].as<std::string>(),
buyer_id_field,
row["title"].as<std::string>(),
row["description"].as<std::string>(),
row["price"].as<int>(),
row["status"].as<std::string>(),
row["type"].as<std::string>(),
image_url_field,
creator,
buyer,
row["created_at"].as<std::string>(),
row["updated_at"].as<std::string>()));
}

return all_services;
}

std::unique_ptr<ServiceModel> ServiceModel::delete_service_by_id(pqxx::connection& db, const std::string id, bool throw_when_null) {
try {
pqxx::work txn(db);
Expand Down
6 changes: 6 additions & 0 deletions src/routes/service_routes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ void initialize_service_routes(NebyApp& app) {
pool.releaseConnection(conn);
});

CROW_ROUTE(app, "/api/services/self").methods(crow::HTTPMethod::GET).CROW_MIDDLEWARES(app, VerifyJWT)([&pool](const crow::request& req, crow::response& res) {
auto conn = pool.getConnection();
ServiceController::get_services_self(*conn.get(), req, res);
pool.releaseConnection(conn);
});

// ** GET /api/services/:id

// ** POST /api/services
Expand Down
Loading

0 comments on commit 06d9719

Please sign in to comment.