Compare commits

..

No commits in common. "2d93da5cc2468e6c629c3d360fbaa35210aeb684" and "ba86af4064fcaa1e7a865546f9bf8aa70ff13939" have entirely different histories.

5 changed files with 12 additions and 73 deletions

View file

@ -186,9 +186,11 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
seller->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (seller->getState() == Seller::State::UPDATE) { } else if (seller->getState() == Seller::State::UPDATE) {
// TODO
retCode = sqlite3_prepare_v2( retCode = sqlite3_prepare_v2(
db_, db_,
"UPDATE sellers SET" "UPDATE sellers SET"
@ -220,58 +222,27 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
seller->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (seller->getState() == Seller::State::DELETE) { } else if (seller->getState() == Seller::State::DELETE) {
count += static_cast<int>(seller->getArticles(false).size()); count += static_cast<int>(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) { if (seller->getState() != Seller::State::DELETE) {
count += storeArticles(seller->getArticles(false)); count += storeArticles(stmt, seller->getArticles(false));
} }
} }
endTransaction(); 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>& seller) {
return seller->getState() == Seller::State::DELETE;
}));
for (const auto& seller : sellers) {
seller->cleanupArticles();
seller->setState(Seller::State::OK);
}
return count; return count;
} }
unsigned int Database::storeArticles(std::vector<Article*> articles) unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> articles)
{ {
int retCode{}; int retCode{};
int count{}; int count{};
sqlite3_stmt* stmt;
for (auto& article : articles) { for (auto& article : articles) {
if (article->getState() == Article::State::NEW) { if (article->getState() == Article::State::NEW) {
@ -308,6 +279,7 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
article->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (article->getState() == Article::State::UPDATE) { } else if (article->getState() == Article::State::UPDATE) {
@ -345,29 +317,11 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
throw std::runtime_error(errMsg); throw std::runtime_error(errMsg);
} }
article->setState(Seller::State::OK);
++count; ++count;
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
} else if (article->getState() == Article::State::DELETE) { } else if (article->getState() == Article::State::DELETE) {
retCode = sqlite3_prepare_v2(db_, "DELETE FROM articles WHERE id = :uuid", -1, &stmt, // TODO
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);
} }
} }

View file

@ -25,7 +25,7 @@ class Database
void endTransaction(); void endTransaction();
void createNew(); void createNew();
int getVersion(); int getVersion();
unsigned int storeArticles(std::vector<Article*> articles); unsigned int storeArticles(sqlite3_stmt* stmt, std::vector<Article*> articles);
}; };
#endif // DATABASE_H #endif // DATABASE_H

View file

@ -24,8 +24,6 @@ void Sale::removeArticle(const Article* articlePtr)
[&articlePtr](auto art) { return art.get() == articlePtr; }); [&articlePtr](auto art) { return art.get() == articlePtr; });
if (it != articles_.end()) { if (it != articles_.end()) {
(*it)->setSale(nullptr); (*it)->setSale(nullptr);
(*it)->setState(
Article::State::DELETE); // since we only have ad-hoc articles, that have all been sold
articles_.erase(it); articles_.erase(it);
} }
} }

View file

@ -49,16 +49,4 @@ int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).
int Seller::numArticlesOffered() const { return numArticlesOffered_; } int Seller::numArticlesOffered() const { return numArticlesOffered_; }
void Seller::cleanupArticles() //int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }
{
articles_.erase(std::remove_if(begin(articles_), end(articles_),
[](const std::shared_ptr<Article>& article) {
return article->getState() == Article::State::DELETE;
}));
for (auto& article : articles_) {
article->setState(Article::State::OK);
}
}
// int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }

View file

@ -22,14 +22,13 @@ class Seller : public Entity
void setLastName(const std::string& lastName); void setLastName(const std::string& lastName);
void setNumArticlesOffered(int number); void setNumArticlesOffered(int number);
void addArticle(std::shared_ptr<Article> article); void addArticle(std::shared_ptr<Article> article);
void cleanupArticles();
std::string getFirstName() const; std::string getFirstName() const;
std::string getLastName() const; std::string getLastName() const;
int getSellerNo() const; int getSellerNo() const;
int numArticlesOffered() const; int numArticlesOffered() const;
int numArticlesSold() const; int numArticlesSold() const;
// int numArticlesTotal() const; //int numArticlesTotal() const;
std::vector<Article*> getArticles(bool onlySold = true) const; std::vector<Article*> getArticles(bool onlySold = true) const;
private: private: