Skip to content

Commit

Permalink
Merge pull request #102 from Serveis-Neby/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ddbaque authored Jun 3, 2024
2 parents cec5408 + 0e20e74 commit 8cf9a30
Show file tree
Hide file tree
Showing 68 changed files with 3,712 additions and 1,104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/
.editorconfig
1 change: 1 addition & 0 deletions include/controllers/auth_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <crow.h>
#include <models/community_model.h>
#include <models/user_model.h>
#include <models/user_achievement_model.h>
#include <utils/auth.h>
#include <utils/common.h>
#include <utils/user_validations.h>
Expand Down
11 changes: 9 additions & 2 deletions include/controllers/notification_controller.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#pragma once

#include <crow.h>
#include <models/achievement_notification_model.h>
#include <models/notification_model.h>
#include <models/notification_service_model.h>
#include <models/service_model.h>
#include <models/user_achievement_model.h>
#include <models/user_model.h>
#include <utils/common.h>
#include <utils/utils.h>
#include <format>
#include <map>
#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);
static void create_notification(pqxx::connection& db, const crow::request& req, crow::response& res);
static void handle_notification(pqxx::connection& db, crow::request& req, crow::response& res, const std::string& notification_id);
static void create_service_notification(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_notification_service_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& service_id);
static void get_notifications(pqxx::connection& db, const crow::request& req, crow::response& res);
};
27 changes: 27 additions & 0 deletions include/controllers/rating_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <crow.h>
#include <format>
#include <memory>
#include <optional>
#include <pqxx/pqxx>
#include <string>
#include <vector>

// ** custom includes
#include <models/rating_model.h>
#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>
// ** ---------------------------------------------

class RatingController {
public:
static void create_rating(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& service_id);
static void update_rating(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& rating_id);
static void get_user_ratings(pqxx::connection& db, crow::response& res, const std::string& user_id);
static void get_service_rating(pqxx::connection& db, crow::response& res, const std::string& service_id);
};
13 changes: 4 additions & 9 deletions include/controllers/service_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@

#include <crow.h>
#include <format>
#include <map>
#include <memory>
#include <pqxx/pqxx>
#include <string>
#include <vector>

// ** 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>
// ** ---------------------------------------------

class ServiceController {
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, const std::string& user_id, crow::response& res);
static void delete_user_by_id(pqxx::connection& db, const std::string& user_id, crow::response& res); */

static void create_service(pqxx::connection& db, const crow::request& req, crow::response& res);
public:
static void create_service(pqxx::connection& db, 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 get_service_by_user_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
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);
};
18 changes: 18 additions & 0 deletions include/controllers/user_achievement_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

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

class UserAchievementController {
public:
static void get_user_achievements_self(pqxx::connection& db, crow::request& req, crow::response& res);
static void claim_user_achievement(pqxx::connection& db, crow::request& req, crow::response& res, const std::string& arch_id);
};
29 changes: 10 additions & 19 deletions include/db/connection_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,20 @@

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

ConnectionPool(const ConnectionPool&) = delete;
ConnectionPool& operator=(const ConnectionPool&) = delete;
std::shared_ptr<pqxx::connection> get_connection();

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

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

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

~ConnectionPool() ;

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

private:
std::string connectionString_;
int poolSize_;
std::deque<std::shared_ptr<pqxx::connection>> connections_;
std::mutex mutex_;
std::condition_variable condition_;
std::string _connection_string;
int _pool_size;
std::deque<std::shared_ptr<pqxx::connection>> _connections;
std::mutex _mutex;
std::condition_variable _condition;
};
7 changes: 7 additions & 0 deletions include/db/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <vector>
#include <string>
#include <sstream>

std::string join_query_update(const std::vector<std::string>& elements, const std::string& delimiter);
18 changes: 18 additions & 0 deletions include/middlewares/handle_achievements.h
Original file line number Diff line number Diff line change
@@ -0,0 +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>
#include <utils/user_achievements_handler.h>

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

void before_handle(crow::request& req, crow::response& res, context& ctx);
void after_handle(crow::request& req, crow::response& res, context& ctx);
};
5 changes: 5 additions & 0 deletions include/middlewares/index_middlewares.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <crow.h>
#include <middlewares/handle_achievements.h>
#include <middlewares/verify_jwt.h>

using NebyApp = crow::App<VerifyJWT, HandleAchievements>;
1 change: 0 additions & 1 deletion include/middlewares/verify_jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ struct VerifyJWT : crow::ILocalMiddleware {
void after_handle(crow::request& req, crow::response& res, context& ctx);
};

using NebyApp = crow::App<VerifyJWT>;
22 changes: 22 additions & 0 deletions include/models/achievement_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <utils/errors.h>
#include <ctime>
#include <format>
#include <memory>
#include <pqxx/pqxx>
#include <string>

class AchievementModel {
private:
std::string _title;
std::string _description;
int _reward;

public:
std::string get_title() const;
std::string get_description() const;
int get_reward() const;

AchievementModel(std::string title, std::string description, int reward);
};
22 changes: 22 additions & 0 deletions include/models/achievement_notification_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

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

class AchievementNotificationModel {
private:
std::string _id;
std::string _achievement_user_id;
std::string _notification_id;

public:
AchievementNotificationModel(std::string id, std::string achievement_user_id, std::string notification_id);

std::string get_id() const;
std::string get_achievement_user_id() const;
std::string get_notification_id() const;

static std::unique_ptr<AchievementNotificationModel> create_notification_service(pqxx::connection& db, const std::string& notification_id, const std::string& achievement_id, bool throw_when_null = false);
};
3 changes: 0 additions & 3 deletions include/models/community_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class CommunityModel {

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

static bool exists_name(pqxx::connection& db, const std::string& name);
static bool exists_code(pqxx::connection& db, const std::string& code);

static std::unique_ptr<CommunityModel> get_community_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false);
static std::unique_ptr<CommunityModel> get_community_by_code(pqxx::connection& db, const std::string& code, bool throw_when_null = false);
};
30 changes: 14 additions & 16 deletions include/models/notification_model.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
#pragma once

#include <models/achievement_notification_model.h>
#include <models/notification_service_model.h>
#include <utils/common.h>
#include <utils/errors.h>
#include <memory>
#include <optional>
#include <vector>

class NotificationModel {
private:
std::string _id;
std::string _sender_id;
std::string _service_id;
std::string _status;
std::string _type;
std::string _created_at;
std::string _updated_at;
std::optional<NotificationServiceModel> _notification_service;
std::optional<AchievementNotificationModel> _achievement_notification;

public:
NotificationModel(std::string id, std::string sender_id, std::string service_id, std::string status, std::string created_at, std::string updated_at);
NotificationModel(std::string id, std::string type, std::string created_at, std::string updated_at, std::optional<NotificationServiceModel> notification_service, std::optional<AchievementNotificationModel> achievement_notification);

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_type() const;
std::string get_created_at() const;
std::string get_updated_at() const;
std::optional<NotificationServiceModel> get_notification_service();
std::optional<AchievementNotificationModel> get_achievement_notification();

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);
static std::unique_ptr<NotificationModel> create_notification(pqxx::connection& db, const std::string& type, bool throw_when_null = false);
static std::unique_ptr<NotificationModel> get_notification_service_by_id(pqxx::connection& db, const std::string& sender_id, const std::string& service_id, 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);
static std::vector<NotificationModel> get_notifications_self(pqxx::connection& db, const std::string& user_id, bool throw_when_null = false);
};
36 changes: 36 additions & 0 deletions include/models/notification_service_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

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

class NotificationServiceModel {
private:
std::string _id;
std::string _sender_id;
std::string _service_id;
std::string _status;
std::string _notification_id;

public:
NotificationServiceModel(std::string id, std::string sender_id, std::string service_id, std::string status, std::string notification_id);

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_notification_id() const;

static std::unique_ptr<NotificationServiceModel> create_notification_service(pqxx::connection& db, const std::string& notification_id, const std::string& sender_id, const std::string& service_id, const std::string& status = NotificationServicesStatus::PENDING, bool throw_when_null = false);

static bool is_requested(pqxx::connection& db, const std::string& sender_id, const std::string& service_id, bool throw_when_null = false);

static std::unique_ptr<NotificationServiceModel> 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, bool throw_when_null = false);

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

static std::vector<NotificationServiceModel> get_notifications_accepted_self(pqxx::connection& db, const std::string& sender_id, bool throw_when_null = false);
};
37 changes: 37 additions & 0 deletions include/models/rating_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <utils/common.h>
#include <utils/errors.h>
#include <format>
#include <iostream>
#include <memory>
#include <optional>
#include <pqxx/pqxx>
#include <string>
#include <vector>

class RatingModel {
private:
std::string _id;
std::string _service_id;
int _rating;
std::string _description;
std::optional<std::string> _sender_id;

public:
RatingModel(std::string id, std::string service_id, int rating, std::string description);

RatingModel(std::string id, std::string service_id, int rating, std::string description, std::optional<std::string> sender_id);

std::string get_id() const;
std::string get_service_id() const;
int get_rating() const;
std::string get_description() const;
std::optional<std::string> get_sender_id() const;

static std::unique_ptr<RatingModel> create_rating(pqxx::connection& db, const std::string& service_id, const int& rating, const std::string& description, bool throw_when_null = false);
static bool update_rating(pqxx::connection& db, const std::string& rating_id, const int& rating, const std::string& description, bool throw_when_null = false);
static std::vector<std::unique_ptr<RatingModel>> get_rating_by_user_id(pqxx::connection& db, const std::string& user_id, bool throw_when_null = false);
static std::unique_ptr<RatingModel> get_rating_by_service_id(pqxx::connection& db, const std::string& service_id, bool throw_when_null = false);
static std::unique_ptr<RatingModel> get_rating_by_id(pqxx::connection& db, const std::string& rating_id, bool throw_when_null = false);
};
Loading

0 comments on commit 8cf9a30

Please sign in to comment.