Compare commits
No commits in common. "2d93da5cc2468e6c629c3d360fbaa35210aeb684" and "ba86af4064fcaa1e7a865546f9bf8aa70ff13939" have entirely different histories.
2d93da5cc2
...
ba86af4064
5 changed files with 12 additions and 73 deletions
|
@ -186,9 +186,11 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& 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<std::shared_ptr<Seller>>& 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<int>(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>& 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<Article*> articles)
|
||||
unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> 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<Article*> 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<Article*> 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class Database
|
|||
void endTransaction();
|
||||
void createNew();
|
||||
int getVersion();
|
||||
unsigned int storeArticles(std::vector<Article*> articles);
|
||||
unsigned int storeArticles(sqlite3_stmt* stmt, std::vector<Article*> articles);
|
||||
};
|
||||
|
||||
#endif // DATABASE_H
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,16 +49,4 @@ int Seller::numArticlesSold() const { return static_cast<int>(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>& 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()); }
|
|
@ -22,7 +22,6 @@ class Seller : public Entity
|
|||
void setLastName(const std::string& lastName);
|
||||
void setNumArticlesOffered(int number);
|
||||
void addArticle(std::shared_ptr<Article> article);
|
||||
void cleanupArticles();
|
||||
|
||||
std::string getFirstName() const;
|
||||
std::string getLastName() const;
|
||||
|
|
Loading…
Reference in a new issue