diff --git a/src/core/database.cpp b/src/core/database.cpp index 1674d26..fcc4aaa 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -186,9 +186,11 @@ 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" @@ -220,58 +222,27 @@ 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()); - - 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); + // TODO } if (seller->getState() != Seller::State::DELETE) { - count += storeArticles(seller->getArticles(false)); + count += storeArticles(stmt, 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(std::vector articles) +unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector articles) { int retCode{}; int count{}; - sqlite3_stmt* stmt; for (auto& article : articles) { if (article->getState() == Article::State::NEW) { @@ -308,6 +279,7 @@ unsigned int Database::storeArticles(std::vector articles) sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } + article->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (article->getState() == Article::State::UPDATE) { @@ -345,29 +317,11 @@ unsigned int Database::storeArticles(std::vector articles) sqlite3_finalize(stmt); throw std::runtime_error(errMsg); } + article->setState(Seller::State::OK); ++count; sqlite3_finalize(stmt); } else if (article->getState() == Article::State::DELETE) { - 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); + // TODO } } diff --git a/src/core/database.h b/src/core/database.h index 82707b2..447fdb4 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(std::vector articles); + unsigned int storeArticles(sqlite3_stmt* stmt, 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 ade50b5..1da00fc 100644 --- a/src/core/sale.cpp +++ b/src/core/sale.cpp @@ -24,8 +24,6 @@ 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 94fd9d4..d2dc247 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -49,16 +49,4 @@ int Seller::numArticlesSold() const { return static_cast(getArticles(true). int Seller::numArticlesOffered() const { return numArticlesOffered_; } -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 +//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 e334fb2..c5f3e4a 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -22,14 +22,13 @@ 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: