diff --git a/src/core/database.cpp b/src/core/database.cpp index fcc4aaa..1674d26 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -186,11 +186,9 @@ unsigned int Database::storeSellers(std::vector>& seller sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } - seller->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (seller->getState() == Seller::State::UPDATE) { - // TODO retCode = sqlite3_prepare_v2( db_, "UPDATE sellers SET" @@ -222,27 +220,58 @@ unsigned int Database::storeSellers(std::vector>& seller sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } - seller->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (seller->getState() == Seller::State::DELETE) { count += static_cast(seller->getArticles(false).size()); - // TODO + + retCode = + sqlite3_prepare_v2(db_, "DELETE FROM sellers WHERE id = :uuid", -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, + SQLITE_TRANSIENT); + + retCode = sqlite3_step(stmt); + + if (retCode != SQLITE_DONE) { + + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw std::runtime_error(errMsg); + } + ++count; + sqlite3_finalize(stmt); } if (seller->getState() != Seller::State::DELETE) { - count += storeArticles(stmt, seller->getArticles(false)); + count += storeArticles(seller->getArticles(false)); } } endTransaction(); + + // Everything went fine, so we can now update our objects + sellers.erase( + std::remove_if(begin(sellers), end(sellers), [](const std::shared_ptr& seller) { + return seller->getState() == Seller::State::DELETE; + })); + for (const auto& seller : sellers) { + seller->cleanupArticles(); + seller->setState(Seller::State::OK); + } + return count; } -unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector articles) +unsigned int Database::storeArticles(std::vector articles) { int retCode{}; int count{}; + sqlite3_stmt* stmt; for (auto& article : articles) { if (article->getState() == Article::State::NEW) { @@ -279,7 +308,6 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector a sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } - article->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (article->getState() == Article::State::UPDATE) { @@ -317,11 +345,29 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector a sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } - article->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (article->getState() == Article::State::DELETE) { - // TODO + retCode = sqlite3_prepare_v2(db_, "DELETE FROM articles WHERE id = :uuid", -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(article->getUuid()).c_str(), -1, + SQLITE_TRANSIENT); + + retCode = sqlite3_step(stmt); + + if (retCode != SQLITE_DONE) { + + std::string errMsg(sqlite3_errmsg(db_)); + sqlite3_finalize(stmt); + throw std::runtime_error(errMsg); + } + ++count; + sqlite3_finalize(stmt); } } diff --git a/src/core/database.h b/src/core/database.h index 447fdb4..82707b2 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -25,7 +25,7 @@ class Database void endTransaction(); void createNew(); int getVersion(); - unsigned int storeArticles(sqlite3_stmt* stmt, std::vector articles); + unsigned int storeArticles(std::vector articles); }; #endif // DATABASE_H \ No newline at end of file diff --git a/src/core/sale.cpp b/src/core/sale.cpp index 1da00fc..ade50b5 100644 --- a/src/core/sale.cpp +++ b/src/core/sale.cpp @@ -24,6 +24,8 @@ void Sale::removeArticle(const Article* articlePtr) [&articlePtr](auto art) { return art.get() == articlePtr; }); if (it != articles_.end()) { (*it)->setSale(nullptr); + (*it)->setState( + Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold articles_.erase(it); } } diff --git a/src/core/seller.cpp b/src/core/seller.cpp index d2dc247..94fd9d4 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -49,4 +49,16 @@ int Seller::numArticlesSold() const { return static_cast(getArticles(true). int Seller::numArticlesOffered() const { return numArticlesOffered_; } -//int Seller::numArticlesTotal() const { return static_cast(getArticles().size()); } \ No newline at end of file +void Seller::cleanupArticles() +{ + articles_.erase(std::remove_if(begin(articles_), end(articles_), + [](const std::shared_ptr
& article) { + return article->getState() == Article::State::DELETE; + })); + + for (auto& article : articles_) { + article->setState(Article::State::OK); + } +} + +// int Seller::numArticlesTotal() const { return static_cast(getArticles().size()); } \ No newline at end of file diff --git a/src/core/seller.h b/src/core/seller.h index c5f3e4a..e334fb2 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -22,13 +22,14 @@ class Seller : public Entity void setLastName(const std::string& lastName); void setNumArticlesOffered(int number); void addArticle(std::shared_ptr
article); + void cleanupArticles(); std::string getFirstName() const; std::string getLastName() const; int getSellerNo() const; int numArticlesOffered() const; int numArticlesSold() const; - //int numArticlesTotal() const; + // int numArticlesTotal() const; std::vector getArticles(bool onlySold = true) const; private: