Skip to content

Commit

Permalink
Merge pull request #62 from Serveis-Neby/develop
Browse files Browse the repository at this point in the history
Merge develop to main
  • Loading branch information
JayexDesigns authored May 13, 2024
2 parents aaded12 + 06d9719 commit cec5408
Showing 54 changed files with 4,525 additions and 250 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Backend

[TEST](test/TEST_README.md)
8 changes: 7 additions & 1 deletion include/controllers/auth_controller.h
Original file line number Diff line number Diff line change
@@ -3,15 +3,21 @@
#include <crow.h>
#include <models/community_model.h>
#include <models/user_model.h>
#include <utils/auth.h>
#include <utils/common.h>
#include <utils/user_validations.h>
#include <utils/utils.h>
#include <bcrypt/BCrypt.hpp>
#include <ctime> // Include the ctime header for time functions
#include <format>
#include <iomanip>
#include <memory>
#include <pqxx/pqxx>
#include <string>

class AuthController {
public:
static void register_user(pqxx::connection& db, const crow::request& req, crow::response& res);
static void login_user(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_self(pqxx::connection& db, const crow::request& req, crow::response& res);
};
18 changes: 18 additions & 0 deletions include/controllers/notification_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <crow.h>
#include <models/notification_model.h>
#include <models/service_model.h>
#include <models/user_model.h>
#include <utils/common.h>
#include <utils/utils.h>
#include <format>
#include <memory>
#include <pqxx/pqxx>
#include <string>

class NotificationController {
public:
static void create_notification(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& service_id);
static void handle_notification(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& notification_id);
};
5 changes: 5 additions & 0 deletions include/controllers/service_controller.h
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
// ** custom includes
#include <models/service_model.h>
#include <models/user_model.h>
#include <utils/common.h>
#include <utils/errors.h>
#include <utils/utils.h>
#include <utils/validations.h>
@@ -23,4 +24,8 @@ 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);
};
4 changes: 3 additions & 1 deletion include/controllers/user_controller.h
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ class UserController {
public:
static void get_users(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
static void delete_user_by_id(pqxx::connection& db, crow::response& res, const std::string& user_id);
static void delete_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
static void update_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
static void update_self(pqxx::connection& db, const crow::request& req, crow::response& res);
static void delete_self(pqxx::connection& db, const crow::request& req, crow::response& res);
};
36 changes: 36 additions & 0 deletions include/db/connection_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <pqxx/pqxx>
#include <queue>

class ConnectionPool {
public:
static ConnectionPool& getInstance(const std::string& connectionString, int poolSize) {
static ConnectionPool instance(connectionString, poolSize);
return instance;
}

ConnectionPool(const ConnectionPool&) = delete;
ConnectionPool& operator=(const ConnectionPool&) = delete;

std::shared_ptr<pqxx::connection> getConnection();

void releaseConnection(std::shared_ptr<pqxx::connection> conn);

private:
ConnectionPool(const std::string& connectionString, int poolSize);

~ConnectionPool() ;

std::shared_ptr<pqxx::connection> createConnection();

private:
std::string connectionString_;
int poolSize_;
std::deque<std::shared_ptr<pqxx::connection>> connections_;
std::mutex mutex_;
std::condition_variable condition_;
};
47 changes: 7 additions & 40 deletions include/middlewares/verify_jwt.h
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
#pragma once

#include <crow.h>
#include <db/connection_pool.h>
#include <jwt-cpp/jwt.h>
#include <utils/auth.h>
#include <utils/common.h>
#include <utils/utils.h>
#include <memory>

struct VerifyJWT : crow::ILocalMiddleware {
struct context {};

void before_handle(crow::request& req, crow::response& res, context& ctx) {
std::string token = get_token_cookie(req);

if (!validate_token(token)) {
handle_error(res, "invalid token", 401);
return;
}

auto decoded = jwt::decode(token);

std::string id;
std::string type;

// Acceder al payload del token decodificado
for (auto& e : decoded.get_payload_json()) {
if (e.first == "id") {
id = e.second.get<std::string>();
} else if (e.first == "type") {
type = e.second.get<std::string>();
}
}

if (req.body == "") {
crow::json::wvalue body;

body["id"] = id;
body["isAdmin"] = (type == "admin");

req.body = body.dump();
} else {
crow::json::wvalue body = crow::json::load(req.body);

body["id"] = id;
body["isAdmin"] = (type == "admin");

req.body = body.dump();
}
}

void after_handle(crow::request& req, crow::response& res, context& ctx) {}
void before_handle(crow::request& req, crow::response& res, context& ctx);
void after_handle(crow::request& req, crow::response& res, context& ctx);
};

using NebyApp = crow::App<VerifyJWT>;
2 changes: 1 addition & 1 deletion include/models/community_model.h
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ class CommunityModel {
std::string get_created_at() const;
std::string get_updated_at() const;

static std::string generate_community_code();
static std::string generate_community_code(const std::string& seed);

static std::unique_ptr<CommunityModel> create_community(pqxx::connection& db, const std::string& name, bool throw_when_null = false);

27 changes: 20 additions & 7 deletions include/models/notification_model.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
#pragma once

#include <utils/common.h>
#include <utils/errors.h>
#include <memory>

class NotificationModel {
private:
std::string _id;
std::string _sender_id;
std::string _receiver_id;
std::string _service_id;
std::string _status;
std::string _created_at;
std::string _updated_at;

public:
NotificationModel(std::string id, std::string sender_id, std::string receiver_id, std::string service_id, std::string status);
NotificationModel(std::string id, std::string sender_id, std::string service_id, std::string status, std::string created_at, std::string updated_at);

std::string get_id();
std::string get_sender_id();
std::string get_receiver_id();
std::string get_service_id();
std::string get_status();
std::string get_id() const;
std::string get_sender_id() const;
std::string get_service_id() const;
std::string get_status() const;
std::string get_created_at() const;
std::string get_updated_at() const;

static std::unique_ptr<NotificationModel> create_notification(pqxx::connection& db, const std::string& sender_id, const std::string& service_id, const std::string& status = NotificationStatus::PENDING, bool throw_when_null = false);

// * if the requester has already requested the service before, it returns true, otherwise false
static bool is_requested(pqxx::connection& db, const std::string& sender_id);

static std::unique_ptr<NotificationModel> handle_notification_status(pqxx::connection& db, const std::string& status, const std::string& notification_id, bool throw_when_null = false);

static bool refused_notifications(pqxx::connection& db, const std::string& service_id, const std::string& notification_id);

static std::unique_ptr<NotificationModel> get_notification_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false);
};
21 changes: 16 additions & 5 deletions include/models/service_model.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <models/user_model.h>
#include <utils/errors.h>
#include <memory>
#include <optional>
@@ -10,7 +11,6 @@
class ServiceModel {
private:
std::string _id;
std::string _community_id;
std::string _creator_id;
std::optional<std::string> _buyer_id;
std::string _title;
@@ -19,14 +19,15 @@ class ServiceModel {
std::string _status;
std::string _type;
std::optional<std::string> _image_url;
std::optional<UserModel> _creator;
std::optional<UserModel> _buyer;
std::string _created_at;
std::string _updated_at;

public:
ServiceModel(std::string id, std::string community_id, std::string creator_id, std::optional<std::string> buyer_id, std::string title, std::string description, int price, std::string status, std::string type, std::optional<std::string> image_url, std::string created_at, std::string updated_at);
ServiceModel(std::string id, std::string creator_id, std::optional<std::string> buyer_id, std::string title, std::string description, int price, std::string status, std::string type, std::optional<std::string> image_url, std::optional<UserModel> creator, std::optional<UserModel> buyer, std::string created_at, std::string updated_at);

std::string get_id() const;
std::string get_community_id() const;
std::string get_creator_id() const;
std::optional<std::string> get_buyer_id() const;
std::string get_title() const;
@@ -37,8 +38,18 @@ class ServiceModel {
std::optional<std::string> get_image_url() const;
std::string get_created_at() const;
std::string get_updated_at() const;
std::optional<UserModel> get_creator() const;
std::optional<UserModel> get_buyer() const;

static std::unique_ptr<ServiceModel> create_service(pqxx::connection& db, const std::string& community_id, const std::string& creator_id, const std::string& title, const std::string& description, const int price, const std::string& type, const std::optional<std::string>& image_url, bool isThrow = false);
static std::unique_ptr<ServiceModel> create_service(pqxx::connection& db, const std::string& creator_id, const std::string& title, const std::string& description, const int price, const std::string& type, const std::optional<std::string>& image_url, bool isThrow = false);

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

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);
};
4 changes: 4 additions & 0 deletions include/models/user_model.h
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@ class UserModel {
std::string _updated_at;

public:
UserModel();
UserModel(std::string id, std::string username);

UserModel(std::string id, std::string community_id, std::string username, std::string email, std::string type, int balance, std::string created_at, std::string updated_at);

std::string get_id() const;
@@ -45,4 +48,5 @@ class UserModel {

static bool delete_user_by_id(pqxx::connection& db, const std::string& id);
static bool update_user_by_id(pqxx::connection& db, const std::string& id, const std::string username = "", const std::string email = "", const std::string password = "");
static bool update_user_admin(pqxx::connection& db, const std::string& id, const std::string username, const int balance);
};
3 changes: 2 additions & 1 deletion include/routes/auth_routes.h
Original file line number Diff line number Diff line change
@@ -5,5 +5,6 @@
#include <middlewares/verify_jwt.h>
#include <utils/common.h>
#include <pqxx/pqxx>
#include <db/connection_pool.h>

void initialize_auth_routes(NebyApp& app, pqxx::connection& db);
void initialize_auth_routes(NebyApp& app);
11 changes: 11 additions & 0 deletions include/routes/notification_routes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <controllers/notification_controller.h>
#include <crow.h>
#include <db/connection_pool.h>
#include <middlewares/verify_jwt.h>
#include <db/connection_pool.h>
#include <utils/common.h>
#include <pqxx/pqxx>

void initialize_notifications_routes(NebyApp& app);
3 changes: 2 additions & 1 deletion include/routes/service_routes.h
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
#include <controllers/service_controller.h>
#include <crow.h>
#include <middlewares/verify_jwt.h>
#include <db/connection_pool.h>
#include <utils/common.h>
#include <pqxx/pqxx>

void initialize_service_routes(NebyApp& app, pqxx::connection& db);
void initialize_service_routes(NebyApp& app);
2 changes: 1 addition & 1 deletion include/routes/user_routes.h
Original file line number Diff line number Diff line change
@@ -6,4 +6,4 @@
#include <utils/common.h>
#include <pqxx/pqxx>

void initialize_user_routes(NebyApp& app, pqxx::connection& db);
void initialize_user_routes(NebyApp& app);
4 changes: 3 additions & 1 deletion include/utils/auth.h
Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@

#include <crow.h>
#include <jwt-cpp/jwt.h>
#include <models/user_model.h>
#include <memory>
#include <string>

std::string create_token(const std::string& userId, const std::string& type);
std::string create_token(std::unique_ptr<UserModel>& user);

bool validate_token(const std::string& token);

13 changes: 11 additions & 2 deletions include/utils/common.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#pragma once
#include <crow.h>
#include <db/connection_pool.h>
#include <middlewares/verify_jwt.h>
#include <bcrypt/BCrypt.hpp>
#include <cstdlib>
#include <map>
#include <string>

using NebyApp = crow::App<VerifyJWT>;

struct Roles {
extern const std::string DB_NAME;
extern const std::string DB_USER;
extern const std::string DB_PASSWORD;
extern const std::string DB_HOST;
extern const int DB_PORT;
extern const int HTTP_PORT;
extern const std::string connection_string;

struct Roles {
static const std::string ADMIN;
static const std::string NEIGHBOR;
};
3 changes: 3 additions & 0 deletions include/utils/validations.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <crow.h>
#include <regex>

bool validate_required_body_fields(const crow::json::rvalue &body, const std::vector<std::string> &required_fields, crow::response &res);

bool isValidUUID(const std::string &uuid);
Loading

0 comments on commit cec5408

Please sign in to comment.