delete sales

This commit is contained in:
Martin Brodbeck 2018-07-28 11:52:43 +02:00
parent 9ab8ca7a4b
commit 940688edd5
6 changed files with 27 additions and 6 deletions

View file

@ -495,6 +495,12 @@ unsigned int Database::storeSales(std::vector<std::unique_ptr<Sale>>& sales)
endTransaction();
// Everything went fine, so we can now update our objects
sales.erase(
std::remove_if(sales.begin(), sales.end(),
[](const auto& sale) { return (sale->getState() == Sale::State::DELETE); }),
sales.end());
for (auto& sale : sales) {
sale->setState(Sale::State::OK);
}

View file

@ -128,3 +128,10 @@ std::string Marketplace::getBasketSumAsString()
sumStream << std::right << std::setw(12) << std::showbase << std::put_money(sumInCent, false);
return sumStream.str();
}
void Marketplace::removeSale(boost::uuids::uuid uuid)
{
sales_.erase(std::remove_if(sales_.begin(), sales_.end(),
[&uuid](const auto& a) { return a->getUuid() == uuid; }),
sales_.end());
}

View file

@ -39,6 +39,7 @@ class Marketplace
void addArticleToBasket(std::unique_ptr<Article> article);
size_t basketSize();
void finishCurrentSale();
void removeSale(boost::uuids::uuid uuid);
private:
SellersVec sellers_;

View file

@ -8,7 +8,7 @@ void Sale::addArticle(Article* articlePtr)
articles_.push_back(articlePtr);
}
std::vector<Article*>& Sale::getArticles() { return articles_; }
ArticlesVec& Sale::getArticles() { return articles_; }
void Sale::removeArticle(const Article* articlePtr)
{

View file

@ -9,7 +9,10 @@
// class Article;
namespace
{
using ArticlesVec = std::vector<Article*>;
}
class Sale : public Entity
{

View file

@ -175,14 +175,18 @@ bool SaleModel::removeRows(int row, int count, const QModelIndex& parent)
} else if (!parent.parent().isValid()) {
// remove one article from a sale
auto sale = static_cast<Sale*>(parent.internalPointer());
auto articles = sale->getArticles();
emit beginRemoveRows(parent, row, row + count -1);
auto& articles = sale->getArticles();
articles.at(row)->setState(Article::State::DELETE);
emit beginRemoveRows(parent, row, row + count - 1);
sale->removeArticle(articles.at(row));
marketplace_->storeToDb();
emit endRemoveRows();
if (articles.size() == 0) {
std::cout << "No articles left.\n";
// marketplace_->removeSale(sale->getUuid());
sale->setState(Sale::State::DELETE);
}
emit beginRemoveRows(parent.parent(), 0, 0);
marketplace_->storeToDb();
emit endRemoveRows();
}