Compare commits
4 commits
ba86af4064
...
2d93da5cc2
Author | SHA1 | Date | |
---|---|---|---|
2d93da5cc2 | |||
372937e096 | |||
78e7dc772b | |||
9ec6ac383f |
5 changed files with 73 additions and 12 deletions
|
@ -186,11 +186,9 @@ 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"
|
||||||
|
@ -222,27 +220,58 @@ 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(stmt, seller->getArticles(false));
|
count += storeArticles(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(sqlite3_stmt* stmt, std::vector<Article*> articles)
|
unsigned int Database::storeArticles(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) {
|
||||||
|
@ -279,7 +308,6 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
|
||||||
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) {
|
||||||
|
@ -317,11 +345,29 @@ unsigned int Database::storeArticles(sqlite3_stmt* stmt, std::vector<Article*> a
|
||||||
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) {
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ class Database
|
||||||
void endTransaction();
|
void endTransaction();
|
||||||
void createNew();
|
void createNew();
|
||||||
int getVersion();
|
int getVersion();
|
||||||
unsigned int storeArticles(sqlite3_stmt* stmt, std::vector<Article*> articles);
|
unsigned int storeArticles(std::vector<Article*> articles);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DATABASE_H
|
#endif // DATABASE_H
|
|
@ -24,6 +24,8 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,4 +49,16 @@ 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()
|
||||||
|
{
|
||||||
|
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()); }
|
// int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }
|
|
@ -22,6 +22,7 @@ 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;
|
||||||
|
|
Loading…
Reference in a new issue