store and load articles correctly

This commit is contained in:
Martin Brodbeck 2018-07-23 08:57:35 +02:00
parent 4143fc2e89
commit b100fc8e51
2 changed files with 42 additions and 9 deletions

View file

@ -15,7 +15,7 @@ Database::Database()
{
namespace fs = std::filesystem;
#if defined(__linux__) || defined (__APPLE__)
#if defined(__linux__) || defined(__APPLE__)
fs::path dbpath = fs::path(std::getenv("HOME")) / ".local/share/kima2";
#elif defined(_WIN64) || defined(_WIN32)
fs::path dbpath = fs::path(std::getenv("LOCALAPPDATA") / "/kima2";
@ -435,5 +435,39 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
sqlite3_finalize(stmt);
for (auto& seller : sellers) {
retCode = sqlite3_prepare_v2(db_,
"SELECT id, source_no, article_no, description, price"
" FROM articles"
" WHERE seller_id = :seller_uuid"
" ORDER BY article_no",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":seller_uuid"),
boost::uuids::to_string(seller->getUuid()).c_str(), -1, SQLITE_TRANSIENT);
retCode = sqlite3_step(stmt);
while (retCode != SQLITE_DONE) {
++count;
auto article = std::make_unique<Article>();
article->setUuidFromString(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
article->setSeller(seller.get());
article->setSourceNo(sqlite3_column_int(stmt, 1));
article->setArticleNo(sqlite3_column_int(stmt, 2));
article->setDescription(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
article->setPrice(sqlite3_column_int(stmt, 4));
article->setState(Article::State::OK);
seller->addArticle(std::move(article));
retCode = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
return count;
}

View file

@ -36,9 +36,10 @@ int Marketplace::getNextSellerNo()
int Marketplace::getNextArticleNo()
{
auto iter = std::max_element(
sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool { return a->getMaxArticleNo() < b->getMaxArticleNo(); });
auto iter = std::max_element(sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool {
return a->getMaxArticleNo() < b->getMaxArticleNo();
});
if (iter == sellers_.end())
return 1;
return (*iter)->getMaxArticleNo() + 1;
@ -83,9 +84,7 @@ void Marketplace::finishCurrentSale()
sales_.push_back(std::move(sale));
basket_.clear();
storeToDb();
}
BasketVec& Marketplace::getBasket()
{
return basket_;
}
BasketVec& Marketplace::getBasket() { return basket_; }