From 8d58da4618653e9e7250e37b21a57f9e7b5462a3 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 08:33:16 +0200 Subject: [PATCH 01/13] new test case --- test/test_seller.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_seller.cpp b/test/test_seller.cpp index d193340..0a3474f 100644 --- a/test/test_seller.cpp +++ b/test/test_seller.cpp @@ -2,6 +2,8 @@ #include "../src/core/seller.h" +#include + #include //using namespace boost::unit_test; @@ -15,4 +17,13 @@ BOOST_AUTO_TEST_CASE(create_uuid) { Seller seller{}; seller.createUuid(); BOOST_TEST(seller.getUuid().is_nil() == false); +} + +BOOST_AUTO_TEST_CASE(create_many) { + constexpr unsigned int QUANTITY{10000}; + std::array sellers; + for (unsigned i = 0; i < sellers.size(); i++) { + sellers[i] = Seller(); + sellers[i].createUuid(); + } } \ No newline at end of file From 0f3e7ba7992b4087c2d1af7ed7264ea356953a5b Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 08:33:40 +0200 Subject: [PATCH 02/13] small changes --- src/core/database.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 42603ad..a80cf0d 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -9,16 +9,20 @@ Database::Database(const std::string& dbname) : db_(nullptr) if (errCode) { throw std::runtime_error("Could not open database file."); } - exec("PRAGMA foreign_key = 1"); + sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY); } Database::~Database() { sqlite3_close(db_); } void Database::exec(const std::string& sql) { - const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, nullptr); + char* errMsg; + const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg); if (errCode) { - throw std::runtime_error("Error in SQL execution."); + std::string errMsgString(errMsg); // Make a C++ string of the errMsg, so that we can call + // sqlite3_free() before throwing the exception + sqlite3_free(errMsg); + throw std::runtime_error("Error in SQL execution: " + errMsgString); } } From 779f085cca8425dd1da3b5fd493d54a79f304ac1 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 08:34:23 +0200 Subject: [PATCH 03/13] beautify code --- test/test_seller.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/test_seller.cpp b/test/test_seller.cpp index 0a3474f..e9254c7 100644 --- a/test/test_seller.cpp +++ b/test/test_seller.cpp @@ -6,20 +6,23 @@ #include -//using namespace boost::unit_test; +// using namespace boost::unit_test; -BOOST_AUTO_TEST_CASE( create_uuid_nil ) { +BOOST_AUTO_TEST_CASE(create_uuid_nil) +{ Seller seller{}; BOOST_TEST(seller.getUuid().is_nil() == true); } -BOOST_AUTO_TEST_CASE(create_uuid) { +BOOST_AUTO_TEST_CASE(create_uuid) +{ Seller seller{}; seller.createUuid(); BOOST_TEST(seller.getUuid().is_nil() == false); } -BOOST_AUTO_TEST_CASE(create_many) { +BOOST_AUTO_TEST_CASE(create_many) +{ constexpr unsigned int QUANTITY{10000}; std::array sellers; for (unsigned i = 0; i < sellers.size(); i++) { From b422664f81d2e3a54b28dd89d12afb2f992a2786 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 09:43:40 +0200 Subject: [PATCH 04/13] handle db initialization --- src/core/database.cpp | 71 +++++++++++++++++++++++++++++++++++++++++-- src/core/database.h | 3 ++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index a80cf0d..4a97e48 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -1,11 +1,13 @@ #include "database.h" +#include #include #include Database::Database(const std::string& dbname) : db_(nullptr) { - const int errCode = sqlite3_open(dbname.c_str(), &db_); + dbname_ = dbname; + const int errCode = sqlite3_open(dbname_.c_str(), &db_); if (errCode) { throw std::runtime_error("Could not open database file."); } @@ -26,8 +28,11 @@ void Database::exec(const std::string& sql) } } -void Database::init() +void Database::createNew() { + std::string sqlCreateKima2{"CREATE TABLE IF NOT EXISTS kima2 (" + "version INTEGER NOT NULL);" + "INSERT INTO kima2 (version) VALUES (1);"}; std::string sqlCreateSellers{"CREATE TABLE IF NOT EXISTS sellers (" "id TEXT PRIMARY KEY NOT NULL, " "seller_no INTEGER, " @@ -51,11 +56,73 @@ void Database::init() ");"}; beginTransaction(); + exec(sqlCreateKima2); exec(sqlCreateSellers); exec(sqlCreateArticles); endTransaction(); } +void Database::init() +{ + int version = getVersion(); + + switch (version) { + case 0: + createNew(); + break; + // perhaps handle upgrades for db schema here... + default: + // Do nothing because we are up-to-date. + break; + } +} + +int Database::getVersion() +{ + int retCode{}; + sqlite3_stmt* stmt; + + // Check if there's already a kima2 table available. + // If not, return version == 0. + retCode = sqlite3_prepare_v2( + db_, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='kima2';", -1, &stmt, + nullptr); + if (retCode != SQLITE_OK) + throw std::string(sqlite3_errmsg(db_)); + retCode = sqlite3_step(stmt); + if (retCode != SQLITE_ROW && retCode != SQLITE_DONE) { + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw errMsg; + } else if (retCode != SQLITE_DONE) { + int count = sqlite3_column_int(stmt, 0); + sqlite3_finalize(stmt); + if (count == 0) + return 0; // no kima2 table, so version is 0 + } + + // Now that we know that the kima2 table is present, read and return the schema version. + retCode = sqlite3_prepare_v2(db_, "SELECT version FROM kima2", -1, &stmt, nullptr); + if (retCode != SQLITE_OK) + throw std::string(sqlite3_errmsg(db_)); + + retCode = sqlite3_step(stmt); + + if (retCode != SQLITE_ROW && retCode != SQLITE_DONE) { + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw errMsg; + } else if (retCode == SQLITE_DONE) { + sqlite3_finalize(stmt); + return 0; // no version entry, so version is 0 + } + + int version = sqlite3_column_int(stmt, 0); + sqlite3_finalize(stmt); + + return version; +} + void Database::beginTransaction() { exec("BEGIN TRANSACTION"); } 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 c0b4de4..ff6f3d8 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -16,8 +16,11 @@ public: void init(); private: sqlite3 *db_; + std::string dbname_; void beginTransaction(); void endTransaction(); + void createNew(); + int getVersion(); }; #endif // DATABASE_H \ No newline at end of file From eeba4b11e01bc107b9fa0cd2bcea0097e169524a Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 11:52:22 +0200 Subject: [PATCH 05/13] include ctest --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d9f0a1..c53101c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,13 +10,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) if(MSVC) add_compile_options(/W4 /WX) else() - add_compile_options(-W -Wall -Werror) + add_compile_options(-Wall -Wpedantic) endif() set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +include(CTest) enable_testing() add_subdirectory(src) From 1778f48bc613193c7129dafe5ec37f72f07258fa Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 11:53:06 +0200 Subject: [PATCH 06/13] create all needed tables --- src/core/database.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index 4a97e48..fc1a1c8 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -1,8 +1,8 @@ #include "database.h" -#include #include #include +#include Database::Database(const std::string& dbname) : db_(nullptr) { @@ -30,9 +30,12 @@ void Database::exec(const std::string& sql) void Database::createNew() { + std::vector sqlStrings{}; + std::string sqlCreateKima2{"CREATE TABLE IF NOT EXISTS kima2 (" "version INTEGER NOT NULL);" "INSERT INTO kima2 (version) VALUES (1);"}; + sqlStrings.push_back(sqlCreateKima2); std::string sqlCreateSellers{"CREATE TABLE IF NOT EXISTS sellers (" "id TEXT PRIMARY KEY NOT NULL, " "seller_no INTEGER, " @@ -41,7 +44,7 @@ void Database::createNew() "offered_articles INTEGER, " "UNIQUE (seller_no)" ");"}; - + sqlStrings.push_back(sqlCreateSellers); std::string sqlCreateArticles{ "CREATE TABLE IF NOT EXISTS articles (" "id TEXT PRIMARY KEY NOT NULL, " @@ -54,11 +57,26 @@ void Database::createNew() "FOREIGN KEY (seller_id) REFERENCES sellers(id) ON DELETE CASCADE, " "CHECK (article_no BETWEEN 0 AND 99999)" ");"}; + sqlStrings.push_back(sqlCreateArticles); + std::string sqlCreateSales{"CREATE TABLE IF NOT EXISTS sales (" + " id TEXT PRIMARY KEY NOT NULL," + " source_no INTEGER NOT NULL," + " sold_at TEXT" + ");"}; + sqlStrings.push_back(sqlCreateSales); + std::string sqlCreateSalesItems{ + "CREATE TABLE IF NOT EXISTS sales_items(" + " sale_id TEXT NOT NULL," + " article_id TEXT NOT NULL," + " FOREIGN KEY (sale_id) REFERENCES sales(id) ON DELETE CASCADE," + " FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE" + ");"}; + sqlStrings.push_back(sqlCreateSalesItems); beginTransaction(); - exec(sqlCreateKima2); - exec(sqlCreateSellers); - exec(sqlCreateArticles); + for (const auto& sql : sqlStrings) { + exec(sql); + } endTransaction(); } From 59e7634712e5d0e7950488c769e962fe77e3559d Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 11:53:15 +0200 Subject: [PATCH 07/13] simplify test use case --- test/test_database.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/test_database.cpp b/test/test_database.cpp index 28d47dc..de5c004 100644 --- a/test/test_database.cpp +++ b/test/test_database.cpp @@ -8,18 +8,8 @@ BOOST_AUTO_TEST_CASE(create_database) { - std::filesystem::path testPath = std::filesystem::temp_directory_path() / "kima2test.db"; - if (std::filesystem::exists(testPath)) { - std::filesystem::remove(testPath); - } - std::string filename = testPath.generic_string(); + Database db(":memory:"); + BOOST_CHECK_NO_THROW(db.init()); - Database db(filename); - db.init(); - - BOOST_TEST(std::filesystem::exists(testPath) == true); - if (std::filesystem::exists(testPath)) { - std::filesystem::remove(testPath); - } } \ No newline at end of file From 4d8396fac19faa1a4ade29872354100bf39851e6 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 11:53:46 +0200 Subject: [PATCH 08/13] code beautifying --- src/core/seller.cpp | 31 ++++++++++++++----------------- src/core/seller.h | 5 ++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core/seller.cpp b/src/core/seller.cpp index e4de084..be247db 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -1,26 +1,23 @@ #include "seller.h" -void Seller::setSellerNo(int seller_no) -{ - sellerNo_ = seller_no; -} +Seller::Seller() : Entity() {} -void Seller::setFirstName(const std::string& firstName) +Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo, + int numberOfArticles) + : Entity() { firstName_ = firstName; -} - -void Seller::setLastName(const std::string& lastName) -{ lastName_ = lastName; + sellerNo_ = sellerNo; + numberOfOfferedArticles_ = numberOfArticles; } -void Seller::setNumberOfOfferedArticles(int number) -{ - numberOfOfferedArticles_ = number; -} +inline void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; } -size_t Seller::getNumberOfOfferedArticles() -{ - return articles_.size(); -} \ No newline at end of file +inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; } + +inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; } + +inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; } + +inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index 2848579..d5dfc18 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -1,8 +1,8 @@ #ifndef SELLER_H #define SELLER_H -#include "entity.h" #include "article.h" +#include "entity.h" #include #include @@ -12,6 +12,9 @@ class Article; class Seller : public Entity { public: + Seller(); + Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0, + int numberOfArticles = 0); void setSellerNo(int sellerNo); void setFirstName(const std::string& firstName); void setLastName(const std::string& lastName); From 8b08fad47a32ecab8b0219f3bc5b9d8d06833b41 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 12:52:57 +0200 Subject: [PATCH 09/13] more on articles --- src/core/article.cpp | 27 +++++++++------------------ src/core/article.h | 13 +++++++++---- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/core/article.cpp b/src/core/article.cpp index 4ba0b33..5d046ff 100644 --- a/src/core/article.cpp +++ b/src/core/article.cpp @@ -1,24 +1,15 @@ #include "article.h" -Article::Article() : Entity() -{} +Article::Article() : Entity() {} -Article::Article(const std::shared_ptr sellerPtr) : Entity() -{ - this->sellerPtr = sellerPtr; -} +Article::Article(std::shared_ptr sellerPtr) : Entity() { sellerPtr_ = sellerPtr; } -void Article::setArticleNo(int articleNo) -{ - this->articleNo = articleNo; -} +void Article::setArticleNo(int articleNo) { articleNo_ = articleNo; } -void Article::setPrice(int price) -{ - this->price = price; -} +void Article::setPrice(int price) { price_ = price; } -void Article::setDescription(const std::string& description) -{ - this->description = description; -} \ No newline at end of file +void Article::setDescription(const std::string& description) { description_ = description; } + +void Article::setSale(const std::shared_ptr salePtr) { salePtr_ = salePtr; } + +bool Article::isSold() { return salePtr_ ? true : false; } \ No newline at end of file diff --git a/src/core/article.h b/src/core/article.h index cc25ffa..dd93e89 100644 --- a/src/core/article.h +++ b/src/core/article.h @@ -3,6 +3,7 @@ #include "entity.h" #include "seller.h" +#include "sale.h" #include #include @@ -17,11 +18,15 @@ public: void setArticleNo(int articleNo); void setPrice(int price); void setDescription(const std::string& description); + bool isSold(); + void setSale(const std::shared_ptr salePtr); + void setSeller(std::shared_ptr sellerPtr); private: - std::shared_ptr sellerPtr{}; - int articleNo{}; - int price{}; - std::string description{}; + std::shared_ptr sellerPtr_{}; + std::shared_ptr salePtr_{}; + int articleNo_{}; + int price_{}; + std::string description_{}; }; #endif \ No newline at end of file From 37ae599c8083c2d87025cd9b19913adbce7fb173 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 12:53:18 +0200 Subject: [PATCH 10/13] new class introduced --- src/core/sale.h | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/core/sale.h diff --git a/src/core/sale.h b/src/core/sale.h new file mode 100644 index 0000000..06f350d --- /dev/null +++ b/src/core/sale.h @@ -0,0 +1,8 @@ +#ifndef SALE_H +#define SALE_H + +class Sale : public Entity { + +}; + +#endif \ No newline at end of file From 94edacc6e6790002e5578e5ee0bf9a05c8dfeb71 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 12:54:10 +0200 Subject: [PATCH 11/13] more on sellers --- src/core/seller.cpp | 16 +++++++++++++++- src/core/seller.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/seller.cpp b/src/core/seller.cpp index be247db..1a205d0 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -20,4 +20,18 @@ inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastN inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; } -inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } \ No newline at end of file +inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } + +std::vector
Seller::getArticles(bool onlySold) { + if (onlySold) { + std::vector
soldArticles; + for (auto article: articles_) { + if (article.isSold()) { + soldArticles.push_back(article); + } + } + return soldArticles; + } else { + return articles_; + } +} \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index d5dfc18..b5ccb54 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -19,6 +19,7 @@ class Seller : public Entity void setFirstName(const std::string& firstName); void setLastName(const std::string& lastName); void setNumberOfOfferedArticles(int number); + std::vector
getArticles(bool onlySold = false); size_t getNumberOfOfferedArticles(); From c1c7e322913ba2c3f0135a977d9e081405cd474e Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 12:54:33 +0200 Subject: [PATCH 12/13] new test cases for article --- test/CMakeLists.txt | 4 ++++ test/test_article.cpp | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 test/test_article.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2c68a6d..382580f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,6 +4,10 @@ add_executable(test_seller test_seller.cpp) target_link_libraries(test_seller core Boost::unit_test_framework) add_test(NAME Seller COMMAND ${CMAKE_BINARY_DIR}/bin/test_seller) +add_executable(test_article test_article.cpp) +target_link_libraries(test_article core Boost::unit_test_framework) +add_test(NAME Article COMMAND ${CMAKE_BINARY_DIR}/bin/test_article) + add_executable(test_database test_database.cpp) target_link_libraries(test_database core Boost::unit_test_framework stdc++fs) add_test(NAME Database COMMAND ${CMAKE_BINARY_DIR}/bin/test_database) diff --git a/test/test_article.cpp b/test/test_article.cpp new file mode 100644 index 0000000..e0792ad --- /dev/null +++ b/test/test_article.cpp @@ -0,0 +1,21 @@ +#define BOOST_TEST_MODULE article + +#include "../src/core/article.h" + +#include + +BOOST_AUTO_TEST_CASE(create_article) +{ + Article article{}; + BOOST_TEST(article.getUuid().is_nil() == true); +} + +BOOST_AUTO_TEST_CASE(check_is_sold) +{ + Article article{}; + BOOST_TEST(article.isSold() == false); + + auto salePtr = std::make_shared(); + article.setSale(salePtr); + BOOST_TEST(article.isSold() == true); +} \ No newline at end of file From f42855dc501e28ac681c0e1408c59b175dfca97a Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 11 Jul 2018 13:25:39 +0200 Subject: [PATCH 13/13] more on articles --- src/core/article.cpp | 4 +++- src/core/article.h | 12 +++++++----- src/core/seller.cpp | 23 +++++++++++++---------- src/core/seller.h | 4 +++- test/test_seller.cpp | 10 ++++++++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/core/article.cpp b/src/core/article.cpp index 5d046ff..3270da7 100644 --- a/src/core/article.cpp +++ b/src/core/article.cpp @@ -10,6 +10,8 @@ void Article::setPrice(int price) { price_ = price; } void Article::setDescription(const std::string& description) { description_ = description; } -void Article::setSale(const std::shared_ptr salePtr) { salePtr_ = salePtr; } +std::string Article::getDescription() { return description_; } + +void Article::setSale(std::shared_ptr salePtr) { salePtr_ = salePtr; } bool Article::isSold() { return salePtr_ ? true : false; } \ No newline at end of file diff --git a/src/core/article.h b/src/core/article.h index dd93e89..1d0487b 100644 --- a/src/core/article.h +++ b/src/core/article.h @@ -2,26 +2,28 @@ #define ARTICLE_H #include "entity.h" -#include "seller.h" #include "sale.h" +#include "seller.h" -#include #include +#include class Seller; class Article : public Entity { -public: + public: Article(); Article(std::shared_ptr sellerPtr); void setArticleNo(int articleNo); void setPrice(int price); void setDescription(const std::string& description); + std::string getDescription(); bool isSold(); - void setSale(const std::shared_ptr salePtr); + void setSale(std::shared_ptr salePtr); void setSeller(std::shared_ptr sellerPtr); -private: + + private: std::shared_ptr sellerPtr_{}; std::shared_ptr salePtr_{}; int articleNo_{}; diff --git a/src/core/seller.cpp b/src/core/seller.cpp index 1a205d0..634e585 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -22,16 +22,19 @@ inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArti inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); } -std::vector
Seller::getArticles(bool onlySold) { - if (onlySold) { - std::vector
soldArticles; - for (auto article: articles_) { - if (article.isSold()) { - soldArticles.push_back(article); - } +void Seller::addArticle(Article article) { + articles_.push_back(article); +} + +std::vector Seller::getArticles(bool onlySold) +{ + std::vector articles; + for (auto& article : articles_) { + if (onlySold && article.isSold()) { + articles.push_back(&article); + } else if (!onlySold) { + articles.push_back(&article); } - return soldArticles; - } else { - return articles_; } + return articles; } \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index b5ccb54..4ac6a98 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -15,12 +15,14 @@ class Seller : public Entity Seller(); Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0, int numberOfArticles = 0); + void setSellerNo(int sellerNo); void setFirstName(const std::string& firstName); void setLastName(const std::string& lastName); void setNumberOfOfferedArticles(int number); - std::vector
getArticles(bool onlySold = false); + void addArticle(Article article); + std::vector getArticles(bool onlySold = false); size_t getNumberOfOfferedArticles(); private: diff --git a/test/test_seller.cpp b/test/test_seller.cpp index e9254c7..5de2ed2 100644 --- a/test/test_seller.cpp +++ b/test/test_seller.cpp @@ -6,8 +6,6 @@ #include -// using namespace boost::unit_test; - BOOST_AUTO_TEST_CASE(create_uuid_nil) { Seller seller{}; @@ -29,4 +27,12 @@ BOOST_AUTO_TEST_CASE(create_many) sellers[i] = Seller(); sellers[i].createUuid(); } +} + +BOOST_AUTO_TEST_CASE(with_article) { + Seller seller("Max", "Mustermann"); + Article article{}; + article.setDescription("Test article"); + seller.addArticle(article); + BOOST_TEST(seller.getArticles().at(0)->getDescription() == "Test article"); } \ No newline at end of file