Skip to content

Commit

Permalink
Add return Id in db manager insert function
Browse files Browse the repository at this point in the history
  • Loading branch information
wilson20010327 committed Nov 18, 2024
1 parent 51d9f71 commit 3e21e0f
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 149 deletions.
2 changes: 1 addition & 1 deletion include/DatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DatabaseManager {
const std::string& collectionName,
const std::vector<std::pair<std::string, std::string>>& keyValues,
std::vector<bsoncxx::document::value>& result);
virtual void insertResource(
virtual std::string insertResource(
const std::string& collectionName,
const std::vector<std::pair<std::string, std::string>>& keyValues);
virtual bool deleteResource(const std::string& collectionName,
Expand Down
2 changes: 1 addition & 1 deletion include/MockDatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MockDatabaseManager : public DatabaseManager {
(override));

MOCK_METHOD(
void, insertResource,
std::string, insertResource,
(const std::string &collectionName,
(const std::vector<std::pair<std::string, std::string>> &keyValues)),
(override));
Expand Down
14 changes: 8 additions & 6 deletions src/DatabaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ void DatabaseManager::printCollection(const std::string &collectionName) {
}
}

void DatabaseManager::insertResource(
std::string DatabaseManager::insertResource(
const std::string &collectionName,
const std::vector<std::pair<std::string, std::string>> &keyValues) {
auto collection = (*conn)["GitGud"][collectionName];
collection.insert_one(createDocument(keyValues).view());
auto item = collection.insert_one(createDocument(keyValues).view());
std::cout << item->inserted_id().get_oid().value.to_string() << std::endl;
return item->inserted_id().get_oid().value.to_string();
}

bool DatabaseManager::deleteResource(const std::string &collectionName,
Expand All @@ -73,11 +75,11 @@ bool DatabaseManager::deleteResource(const std::string &collectionName,

auto result = collection.delete_one(filter_builder.view());
if (result && result->deleted_count() > 0) {
std::cout << "Document deleted successfully.\n";
return 1;
std::cout << "Document deleted successfully.\n";
return 1;
} else {
std::cout << "No document found with the given _id.\n";
return 0;
std::cout << "No document found with the given _id.\n";
return 0;
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/services/Counseling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

#include "Counseling.h"

#include <bsoncxx/document/view.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>

#include <bsoncxx/document/view.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>

#include "DatabaseManager.h"

/**
Expand All @@ -26,7 +25,8 @@ Counseling::Counseling(DatabaseManager &dbManager)
* @param specialty The specialty of the counselor.
* @return A string indicating success or an error message.
*/
std::string Counseling::addCounselor(const std::string &counselorName, const std::string &specialty) {
std::string Counseling::addCounselor(const std::string &counselorName,
const std::string &specialty) {
auto content = createDBContent(counselorName, specialty);
try {
dbManager.insertResource(collection_name, content);
Expand Down Expand Up @@ -88,7 +88,9 @@ std::string Counseling::searchCounselorsAll() {
* @return A string indicating the result of the operation.
* @todo Implement counselor update logic.
*/
std::string Counseling::updateCounselor(const std::string &counselorId, const std::string &counselorName, const std::string &specialty) {
std::string Counseling::updateCounselor(const std::string &counselorId,
const std::string &counselorName,
const std::string &specialty) {
try {
auto content = createDBContent(counselorName, specialty);
dbManager.updateResource(collection_name, counselorId, content);
Expand All @@ -103,7 +105,8 @@ std::string Counseling::updateCounselor(const std::string &counselorId, const st
* @param counselor The BSON document containing the counselor's information.
* @return The ID of the counselor as a string.
*/
std::string Counseling::getCounselorID(const bsoncxx::document::view &counselor) {
std::string Counseling::getCounselorID(
const bsoncxx::document::view &counselor) {
std::string id = counselor["_id"].get_oid().value.to_string();
std::cout << id << std::endl;
return id;
Expand All @@ -114,13 +117,15 @@ std::string Counseling::getCounselorID(const bsoncxx::document::view &counselor)
* @param counselors A vector of BSON documents representing counselors.
* @return A JSON string containing all counselors' information.
*/
std::string Counseling::printCounselors(std::vector<bsoncxx::document::value> &counselors) const {
std::string Counseling::printCounselors(
std::vector<bsoncxx::document::value> &counselors) const {
std::string ret = "[";
for (const auto &counselor : counselors) {
try {
ret += bsoncxx::to_json(counselor.view()) + ",";
} catch (const std::exception &e) {
std::cerr << "Error processing counselor document: " << e.what() << std::endl;
std::cerr << "Error processing counselor document: " << e.what()
<< std::endl;
ret += "{\"error\":\"Unable to process this counselor data\"},";
}
}
Expand Down
148 changes: 78 additions & 70 deletions src/services/Food.cpp
Original file line number Diff line number Diff line change
@@ -1,111 +1,119 @@
// Copyright 2024 COMSW4156-Git-Gud

#include "Food.h"

#include <iostream>

Food::Food( DatabaseManager& db)
: db(db) {
}
Food::Food(DatabaseManager& db) : db(db) {}

/**
* @brief Adds a food resource to the database.
*
* This method takes a vector of key-value pairs representing the food resource and
* stores it in the database. The expected key-value pairs for the food resource
* include:
*
* This method takes a vector of key-value pairs representing the food resource
* and stores it in the database. The expected key-value pairs for the food
* resource include:
* - FoodType: Type of food (e.g., "Fruits")
* - Provider: The provider of the food (e.g., "LocalFarm")
* - Location: The location of the provider (e.g., "Brooklyn")
* - Quantity: The amount of food available (e.g., "100")
* - ExpirationDate: The date the food expires (e.g., "2024-12-31")
*
* @param resource A vector of key-value pairs representing the food resource to be added.
*
* @return std::string Returns "Success" if the food resource is added successfully,
* or an error message if the insertion fails.
*
* @exception std::exception Throws if an error occurs during the database insertion.
*
* @param resource A vector of key-value pairs representing the food resource to
* be added.
*
* @return std::string Returns "Success" if the food resource is added
* successfully, or an error message if the insertion fails.
*
* @exception std::exception Throws if an error occurs during the database
* insertion.
*/
std::string Food::addFood(const std::vector<std::pair<std::string, std::string>>& resource) {
try {
db.insertResource("Food", resource);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error inserting food resource: " << e.what() << std::endl;
return "Error inserting food resource: " + std::string(e.what());
}
std::string Food::addFood(
const std::vector<std::pair<std::string, std::string>>& resource) {
try {
db.insertResource("Food", resource);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error inserting food resource: " << e.what() << std::endl;
return "Error inserting food resource: " + std::string(e.what());
}
}

/**
* @brief Deletes a food resource from the database.
*
* This method takes the ID of the food resource and removes it from the database.
*
*
* This method takes the ID of the food resource and removes it from the
* database.
*
* @param id The ID of the food resource to be deleted.
*
* @return std::string Returns "Success" if the food resource is deleted successfully,
* or an error message if the deletion fails.
*
* @exception std::exception Throws if an error occurs during the database deletion.
*
* @return std::string Returns "Success" if the food resource is deleted
* successfully, or an error message if the deletion fails.
*
* @exception std::exception Throws if an error occurs during the database
* deletion.
*/
std::string Food::deleteFood(const std::string& id) {
try {
db.deleteResource("Food", id);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error deleting food resource: " << e.what() << std::endl;
return "Error deleting food resource: " + std::string(e.what());
}
try {
db.deleteResource("Food", id);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error deleting food resource: " << e.what() << std::endl;
return "Error deleting food resource: " + std::string(e.what());
}
}

/**
* @brief Retrieves all food resources from the database.
*
* This method queries the database for all food resources and returns them
* as a JSON-formatted string. If no food items are found, it returns an empty JSON array.
*
*
* This method queries the database for all food resources and returns them
* as a JSON-formatted string. If no food items are found, it returns an empty
* JSON array.
*
* @return A string containing all food resources in JSON format.
*
* @exception std::exception Throws if an error occurs during the database query or data serialization.
*/
*
* @exception std::exception Throws if an error occurs during the database query
* or data serialization.
*/
std::string Food::getAllFood() {
std::vector<bsoncxx::document::value> foodItems;
std::vector<bsoncxx::document::value> foodItems;

db.findCollection("Food", {}, foodItems);

db.findCollection("Food", {}, foodItems);

if (foodItems.empty()) {
return "[]";
}
if (foodItems.empty()) {
return "[]";
}

bsoncxx::builder::basic::array arrayBuilder;
for (const auto& doc : foodItems) {
arrayBuilder.append(doc.view());
}
bsoncxx::builder::basic::array arrayBuilder;
for (const auto& doc : foodItems) {
arrayBuilder.append(doc.view());
}

return bsoncxx::to_json(arrayBuilder.view());
return bsoncxx::to_json(arrayBuilder.view());
}

/**
* @brief Updates a food resource in the database.
*
*
* This method takes the ID of the food resource and new key-value pairs
* to update the resource in the database.
*
*
* @param id The ID of the food resource to be updated.
* @param resource A vector of key-value pairs with the updated values.
*
* @return std::string Returns "Success" if the food resource is updated successfully,
* or an error message if the update fails.
*
* @exception std::exception Throws if an error occurs during the database update.
*
* @return std::string Returns "Success" if the food resource is updated
* successfully, or an error message if the update fails.
*
* @exception std::exception Throws if an error occurs during the database
* update.
*/
std::string Food::updateFood(const std::string& id,
std::string Food::updateFood(
const std::string& id,
const std::vector<std::pair<std::string, std::string>>& resource) {
try {
db.updateResource("Food", id, resource);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error updating food resource: " << e.what() << std::endl;
return "Error updating food resource: " + std::string(e.what());
}
try {
db.updateResource("Food", id, resource);
return "Success";
} catch (const std::exception& e) {
std::cerr << "Error updating food resource: " << e.what() << std::endl;
return "Error updating food resource: " + std::string(e.what());
}
}
Loading

0 comments on commit 3e21e0f

Please sign in to comment.