diff --git a/src/core/database.cpp b/src/core/database.cpp index 086dfc9..fc1a1c8 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -41,7 +41,7 @@ void Database::createNew() "seller_no INTEGER, " "first_name TEXT, " "last_name TEXT, " - "num_offered_articles INTEGER, " + "offered_articles INTEGER, " "UNIQUE (seller_no)" ");"}; sqlStrings.push_back(sqlCreateSellers); @@ -143,53 +143,4 @@ int Database::getVersion() void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } -void Database::endTransaction() { exec("END TRANSACTION"); } - -unsigned int Database::storeSellers(std::vector& sellers) -{ - int retCode{}; - int count{}; - sqlite3_stmt* stmt; - - beginTransaction(); - - for (auto& seller : sellers) { - if (seller.getState() == Entity::State::NEW) { - retCode = sqlite3_prepare_v2( - db_, - "INSERT INTO sellers" - " (id, seller_no, first_name, last_name, num_offered_articles)" - " VALUES (:uuid, :seller_no, :first_name, :last_name, :num_offered_articles)", - -1, &stmt, nullptr); - - if (retCode != SQLITE_OK) - throw std::runtime_error(sqlite3_errmsg(db_)); - - sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"), - boost::uuids::to_string(seller.getUuid()).c_str(), -1, nullptr); - sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"), - seller.getSellerNo()); - sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"), - seller.getFirstName().c_str(), -1, nullptr); - sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"), - seller.getLastName().c_str(), -1, nullptr); - sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"), - seller.getNumberOfOfferedArticles()); - - retCode = sqlite3_step(stmt); - - if (retCode != SQLITE_DONE) { - - std::string errMsg(sqlite3_errmsg(db_)); - sqlite3_finalize(stmt); - throw std::runtime_error(errMsg); - } - seller.setState(Seller::State::CLEAN); - ++count; - sqlite3_finalize(stmt); - } - } - - endTransaction(); - return count; -} \ No newline at end of file +void Database::endTransaction() { exec("END TRANSACTION"); } \ No newline at end of file diff --git a/src/core/database.h b/src/core/database.h index 83fe27c..ff6f3d8 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -1,8 +1,6 @@ #ifndef DATABASE_H #define DATABASE_H -#include "seller.h" - #include #include @@ -16,7 +14,6 @@ public: Database& operator=(const Database&) = delete; void exec(const std::string& sql); void init(); - unsigned int storeSellers(std::vector& sellers); private: sqlite3 *db_; std::string dbname_; diff --git a/src/core/entity.cpp b/src/core/entity.cpp index 6a6e5d8..c2a0313 100644 --- a/src/core/entity.cpp +++ b/src/core/entity.cpp @@ -19,7 +19,7 @@ void Entity::createUuidFromString(const std::string& uuidString) uuid_ = generator(uuidString); } -inline Entity::State Entity::getState() const +inline Entity::State Entity::getState() { return state_; } \ No newline at end of file diff --git a/src/core/entity.h b/src/core/entity.h index 786d393..d67cc73 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -4,19 +4,17 @@ #include #include -#include class Entity { public: - enum class State { NEW, UPDATED, CLEAN }; - + enum class State { NEW, UPDATED, READ }; + // Entity(); virtual ~Entity() = 0; const boost::uuids::uuid& getUuid() const { return uuid_; }; void createUuid(); void createUuidFromString(const std::string& uuidString); - virtual State getState() const; - void setState(State state) { state_ = state; } + State getState(); private: boost::uuids::uuid uuid_{}; diff --git a/src/core/seller.cpp b/src/core/seller.cpp index ab83c55..634e585 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -12,7 +12,7 @@ Seller::Seller(const std::string& firstName, const std::string& lastName, int se numberOfOfferedArticles_ = numberOfArticles; } -void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } +inline void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; } @@ -20,15 +20,11 @@ inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastN inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; } -int Seller::getNumberOfOfferedArticles() const { return static_cast(articles_.size()); } +inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } -void Seller::addArticle(Article article) { articles_.push_back(article); } - -std::string Seller::getFirstName() const { return firstName_; } - -std::string Seller::getLastName() const { return lastName_; } - -int Seller::getSellerNo() const { return sellerNo_; } +void Seller::addArticle(Article article) { + articles_.push_back(article); +} std::vector Seller::getArticles(bool onlySold) { diff --git a/src/core/seller.h b/src/core/seller.h index eeeac29..4ac6a98 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -22,11 +22,8 @@ class Seller : public Entity void setNumberOfOfferedArticles(int number); void addArticle(Article article); - std::string getFirstName() const; - std::string getLastName() const; - int getSellerNo() const; - int getNumberOfOfferedArticles() const; std::vector getArticles(bool onlySold = false); + size_t getNumberOfOfferedArticles(); private: int sellerNo_{-1}; diff --git a/test/test_database.cpp b/test/test_database.cpp index b963d8b..de5c004 100644 --- a/test/test_database.cpp +++ b/test/test_database.cpp @@ -1,7 +1,6 @@ #define BOOST_TEST_MODULE database #include "../src/core/database.h" -#include "../src/core/seller.h" #include @@ -12,46 +11,5 @@ BOOST_AUTO_TEST_CASE(create_database) Database db(":memory:"); BOOST_CHECK_NO_THROW(db.init()); -} -BOOST_AUTO_TEST_CASE(store_seller_fail) -{ - Database db(":memory:"); - db.init(); - std::vector sellers; - sellers.push_back({}); - sellers.push_back({}); - BOOST_CHECK_THROW(db.storeSellers(sellers), std::runtime_error); -} -BOOST_AUTO_TEST_CASE(store_sellers_succ) -{ - Database db(":memory:"); - db.init(); - std::vector sellers; - Seller a{}; - a.createUuid(); - a.setSellerNo(1); - Seller b{}; - b.createUuid(); - b.setSellerNo(2); - BOOST_TEST(a.getUuid() != b.getUuid()); - sellers.push_back(a); - sellers.push_back(b); - BOOST_CHECK_NO_THROW(db.storeSellers(sellers)); -} - -BOOST_AUTO_TEST_CASE(seller_states) -{ - Database db(":memory:"); - db.init(); - std::vector sellers; - Seller a; - a.setSellerNo(3); - a.createUuid(); - sellers.push_back(a); - std::cout << "Anzahl sellers: " << sellers.size() << "\n"; - BOOST_TEST((sellers.at(0).getState() == Entity::State::NEW)); - BOOST_TEST(db.storeSellers(sellers) == 1); - BOOST_TEST((sellers.at(0).getState() == Entity::State::CLEAN)); - BOOST_TEST(db.storeSellers(sellers) == 0); } \ No newline at end of file