From 919054c46e983f47955b0494fb131fff809528cc Mon Sep 17 00:00:00 2001 From: voooigt Date: Sat, 11 May 2024 04:20:59 +0200 Subject: [PATCH 1/2] task#138 people can get paid now yaaay --- include/controllers/notification_controller.h | 1 + src/controllers/notification_controller.cpp | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/controllers/notification_controller.h b/include/controllers/notification_controller.h index ed87351..9cba36b 100644 --- a/include/controllers/notification_controller.h +++ b/include/controllers/notification_controller.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/src/controllers/notification_controller.cpp b/src/controllers/notification_controller.cpp index 9b87d0b..0938b7d 100644 --- a/src/controllers/notification_controller.cpp +++ b/src/controllers/notification_controller.cpp @@ -112,6 +112,33 @@ void NotificationController::handle_notification(pqxx::connection& db, const cro if (action == NotificationStatus::REFUSED) { updated_notification = NotificationModel::handle_notification_status(db, NotificationStatus::REFUSED, notification_id, true); } else { + std::unique_ptr notificationCreator = UserModel::get_user_by_id(db, notification->get_sender_id()); + std::unique_ptr serviceCreator = UserModel::get_user_by_id(db, service->get_creator_id()); + + if (service->get_type() == "offered") { + if (notificationCreator->get_balance() < service->get_price()) { + handle_error(res, "Notification sender does not have enough coins to pay for the service", 400); + return; + } + else { + int new_sender_balance = notificationCreator->get_balance() - service->get_price(); + int new_creator_balance = serviceCreator->get_balance() + service->get_price(); + UserModel::update_user_admin(db, notificationCreator->get_id(), notificationCreator->get_username(), new_sender_balance); + UserModel::update_user_admin(db, serviceCreator->get_id(), serviceCreator->get_username(), new_creator_balance); + } + } + else { + if (serviceCreator->get_balance() < service->get_price()) { + handle_error(res, "You don't have the coins to pay for this request", 400); + return; + } + else { + int new_sender_balance = notificationCreator->get_balance() + service->get_price(); + int new_creator_balance = serviceCreator->get_balance() - service->get_price(); + UserModel::update_user_admin(db, notificationCreator->get_id(), notificationCreator->get_username(), new_sender_balance); + UserModel::update_user_admin(db, serviceCreator->get_id(), serviceCreator->get_username(), new_creator_balance); + } + } updated_notification = NotificationModel::handle_notification_status(db, NotificationStatus::ACCEPTED, notification_id, true); bool succes_refused = NotificationModel::refused_notifications(db, updated_notification.get()->get_service_id(), notification_id); From ef93a594fdf9b0125c380c93acfef2ed0934496b Mon Sep 17 00:00:00 2001 From: voooigt Date: Sat, 11 May 2024 19:04:19 +0200 Subject: [PATCH 2/2] task#138 solved some format errors --- src/controllers/notification_controller.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/notification_controller.cpp b/src/controllers/notification_controller.cpp index 0938b7d..8dab42b 100644 --- a/src/controllers/notification_controller.cpp +++ b/src/controllers/notification_controller.cpp @@ -115,9 +115,9 @@ void NotificationController::handle_notification(pqxx::connection& db, const cro std::unique_ptr notificationCreator = UserModel::get_user_by_id(db, notification->get_sender_id()); std::unique_ptr serviceCreator = UserModel::get_user_by_id(db, service->get_creator_id()); - if (service->get_type() == "offered") { + if (service->get_type() == ServiceType::OFFERED) { if (notificationCreator->get_balance() < service->get_price()) { - handle_error(res, "Notification sender does not have enough coins to pay for the service", 400); + handle_error(res, "notification sender does not have enough coins to pay for the service", 400); return; } else { @@ -129,7 +129,7 @@ void NotificationController::handle_notification(pqxx::connection& db, const cro } else { if (serviceCreator->get_balance() < service->get_price()) { - handle_error(res, "You don't have the coins to pay for this request", 400); + handle_error(res, "you don't have the coins to pay for this request", 400); return; } else {