Compare commits
No commits in common. "2812f582a1a28370e73457a6618702ac93088902" and "207edb27ba7ae598c8b29fdc1b16c3837c4cb576" have entirely different histories.
2812f582a1
...
207edb27ba
11 changed files with 44 additions and 165 deletions
|
@ -145,7 +145,7 @@ void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
|
|||
|
||||
void Database::endTransaction() { exec("END TRANSACTION"); }
|
||||
|
||||
unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& sellers)
|
||||
unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& sellers)
|
||||
{
|
||||
int retCode{};
|
||||
int count{};
|
||||
|
@ -165,9 +165,10 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
|
|||
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);
|
||||
int test = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"),
|
||||
boost::uuids::to_string(seller->getUuid()).c_str(), -1,
|
||||
SQLITE_TRANSIENT);
|
||||
std::cout << "!!! TEST: " << test << "\n";
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"),
|
||||
seller->getSellerNo());
|
||||
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"),
|
||||
|
@ -253,14 +254,12 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
|
|||
|
||||
endTransaction();
|
||||
|
||||
// Everything went fine, so we can now update our objects
|
||||
sellers.erase(std::remove_if(sellers.begin(), sellers.end(),
|
||||
[](const std::unique_ptr<Seller>& seller) {
|
||||
return (seller->getState() == Seller::State::DELETE);
|
||||
}),
|
||||
sellers.end());
|
||||
|
||||
for (auto& seller : sellers) {
|
||||
// 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);
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ class Database
|
|||
Database& operator=(const Database&) = delete;
|
||||
void exec(const std::string& sql);
|
||||
void init();
|
||||
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers);
|
||||
unsigned int storeSellers(std::vector<std::shared_ptr<Seller>>& sellers);
|
||||
|
||||
private:
|
||||
sqlite3* db_;
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
#include "marketplace.h"
|
||||
#include "database.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
Marketplace::Marketplace()
|
||||
{
|
||||
auto seller = std::make_unique<Seller>("Max", "Mustermann");
|
||||
seller->createUuid();
|
||||
sellers_.push_back(std::move(seller));
|
||||
auto seller = std::make_shared<Seller>("Max", "Mustermann");
|
||||
sellers_.push_back(seller);
|
||||
}
|
||||
|
||||
void Marketplace::storeToDb()
|
||||
|
@ -15,20 +12,7 @@ void Marketplace::storeToDb()
|
|||
const std::string DB_PATH{"/tmp/kima2.db"};
|
||||
|
||||
Database db(DB_PATH);
|
||||
db.init();
|
||||
db.storeSellers(sellers_);
|
||||
}
|
||||
|
||||
void Marketplace::loadFromDb() {}
|
||||
|
||||
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }
|
||||
|
||||
int Marketplace::getNextSellerNo()
|
||||
{
|
||||
auto iter = std::max_element(
|
||||
sellers_.begin(), sellers_.end(),
|
||||
[](const std::unique_ptr<Seller>& a, const std::unique_ptr<Seller>& b) -> bool {
|
||||
return a->getSellerNo() < b->getSellerNo();
|
||||
});
|
||||
return (*iter)->getSellerNo() + 1;
|
||||
}
|
||||
std::vector<std::shared_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }
|
|
@ -13,12 +13,11 @@ class Marketplace
|
|||
Marketplace();
|
||||
void storeToDb();
|
||||
void loadFromDb();
|
||||
std::vector<std::unique_ptr<Seller>>& getSellers();
|
||||
int getNextSellerNo();
|
||||
std::vector<std::shared_ptr<Seller>>& getSellers();
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Seller>> sellers_;
|
||||
std::vector<std::unique_ptr<Sale>> sales_;
|
||||
std::vector<std::shared_ptr<Seller>> sellers_;
|
||||
std::vector<std::shared_ptr<Sale>> sales_;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -14,11 +14,11 @@ Seller::Seller(const std::string& firstName, const std::string& lastName, int se
|
|||
|
||||
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
|
||||
|
||||
void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
|
||||
inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
|
||||
|
||||
void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
|
||||
inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
|
||||
|
||||
void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
|
||||
inline void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
|
||||
|
||||
void Seller::addArticle(std::shared_ptr<Article> article)
|
||||
{
|
||||
|
@ -51,11 +51,10 @@ int Seller::numArticlesOffered() const { return numArticlesOffered_; }
|
|||
|
||||
void Seller::cleanupArticles()
|
||||
{
|
||||
articles_.erase(std::remove_if(articles_.begin(), articles_.end(),
|
||||
articles_.erase(std::remove_if(begin(articles_), end(articles_),
|
||||
[](const std::shared_ptr<Article>& article) {
|
||||
return article->getState() == Article::State::DELETE;
|
||||
}),
|
||||
articles_.end());
|
||||
}));
|
||||
|
||||
for (auto& article : articles_) {
|
||||
article->setState(Article::State::OK);
|
||||
|
|
|
@ -15,10 +15,5 @@ MainWindow::MainWindow()
|
|||
void MainWindow::on_actionEditSeller_triggered()
|
||||
{
|
||||
auto dialog = std::make_unique<SellerDialog>(this);
|
||||
int retCode = dialog->exec();
|
||||
if (retCode == QDialog::Accepted) {
|
||||
marketplace_->storeToDb();
|
||||
} else {
|
||||
marketplace_->loadFromDb();
|
||||
}
|
||||
dialog->exec();
|
||||
}
|
|
@ -2,34 +2,11 @@
|
|||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
|
||||
SellerDialog::SellerDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
|
||||
{
|
||||
ui_.setupUi(this);
|
||||
SellerModel* model =
|
||||
new SellerModel(dynamic_cast<MainWindow*>(parent)->getMarketplace(), ui_.tableView);
|
||||
SellerModel* model = new SellerModel(
|
||||
dynamic_cast<MainWindow*>(parent)->getMarketplace()->getSellers(), ui_.tableView);
|
||||
ui_.tableView->setModel(model);
|
||||
ui_.tableView->setColumnHidden(0, true); // hide the uuid
|
||||
connect(ui_.newButton, &QPushButton::clicked, this, &SellerDialog::on_newButton_clicked);
|
||||
}
|
||||
|
||||
void SellerDialog::on_newButton_clicked()
|
||||
{
|
||||
ui_.tableView->model()->insertRows(ui_.tableView->model()->rowCount(), 1);
|
||||
ui_.tableView->scrollToBottom();
|
||||
}
|
||||
|
||||
void SellerDialog::accept()
|
||||
{
|
||||
Marketplace* market = dynamic_cast<MainWindow*>(parentWidget())->getMarketplace();
|
||||
for (const auto& seller : market->getSellers()) {
|
||||
if (seller->getFirstName().empty() || seller->getLastName().empty()) {
|
||||
QMessageBox(QMessageBox::Icon::Critical, "Fehler",
|
||||
"Bitte geben Sie bei jedem Verkäufer Vorname und Nachname an.",
|
||||
QMessageBox::StandardButton::Ok, this).exec();
|
||||
return;
|
||||
}
|
||||
}
|
||||
QDialog::accept();
|
||||
}
|
|
@ -16,8 +16,6 @@ class SellerDialog : public QDialog
|
|||
Qt::WindowFlags f = Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
|
||||
|
||||
private:
|
||||
void on_newButton_clicked();
|
||||
virtual void accept() override;
|
||||
Ui::SellerDialog ui_;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,18 +1,11 @@
|
|||
#include "sellermodel.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <QMessageBox>
|
||||
|
||||
SellerModel::SellerModel(Marketplace* market, QObject* parent)
|
||||
: QAbstractTableModel(parent), marketplace_(market)
|
||||
SellerModel::SellerModel(std::vector<std::shared_ptr<Seller>>& sellers, QObject* parent)
|
||||
: QAbstractTableModel(parent), sellers_(&sellers)
|
||||
{
|
||||
}
|
||||
|
||||
int SellerModel::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
return marketplace_->getSellers().size();
|
||||
}
|
||||
int SellerModel::rowCount(const QModelIndex& parent) const { return sellers_->size(); }
|
||||
|
||||
int SellerModel::columnCount(const QModelIndex& parent) const { return 5; }
|
||||
|
||||
|
@ -21,7 +14,7 @@ QVariant SellerModel::data(const QModelIndex& index, int role) const
|
|||
if (role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
|
||||
Seller* seller = marketplace_->getSellers().at(index.row()).get();
|
||||
Seller* seller = sellers_->at(index.row()).get();
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
return seller->getUuidAsString().c_str();
|
||||
|
@ -63,66 +56,3 @@ QVariant SellerModel::headerData(int section, Qt::Orientation orientation, int r
|
|||
} else
|
||||
return "";
|
||||
}
|
||||
|
||||
Qt::ItemFlags SellerModel::flags(const QModelIndex& index) const
|
||||
{
|
||||
return QAbstractTableModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
|
||||
}
|
||||
|
||||
bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||
{
|
||||
if (role != Qt::EditRole)
|
||||
return false;
|
||||
|
||||
Seller* seller = marketplace_->getSellers().at(index.row()).get();
|
||||
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
seller->setUuidFromString(value.toString().toStdString());
|
||||
break;
|
||||
case 1: {
|
||||
if (value.toInt() < 0)
|
||||
return false;
|
||||
auto iter =
|
||||
std::find_if(marketplace_->getSellers().begin(), marketplace_->getSellers().end(),
|
||||
[&value](const std::unique_ptr<Seller>& seller) {
|
||||
return value.toInt() == seller->getSellerNo();
|
||||
});
|
||||
if (iter != marketplace_->getSellers().end()) {
|
||||
/* QMessageBox(QMessageBox::Icon::Warning, "Fehler",
|
||||
"Die Verkäufernummer muss eindeutig sein.", QMessageBox::StandardButton::Ok)
|
||||
.exec(); */
|
||||
return false;
|
||||
}
|
||||
seller->setSellerNo(value.toInt());
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
seller->setFirstName(value.toString().toStdString());
|
||||
break;
|
||||
case 3:
|
||||
seller->setLastName(value.toString().toStdString());
|
||||
break;
|
||||
case 4:
|
||||
seller->setNumArticlesOffered(value.toInt());
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (seller->getState() == Seller::State::OK)
|
||||
seller->setState(Seller::State::UPDATE);
|
||||
dataChanged(index, index);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SellerModel::insertRows(int row, int count, const QModelIndex& parent)
|
||||
{
|
||||
beginInsertRows(parent, row, row + count - 1);
|
||||
auto seller = std::make_unique<Seller>();
|
||||
seller->createUuid();
|
||||
seller->setSellerNo(marketplace_->getNextSellerNo());
|
||||
marketplace_->getSellers().push_back(std::move(seller));
|
||||
endInsertRows();
|
||||
|
||||
return true;
|
||||
}
|
|
@ -8,17 +8,14 @@
|
|||
class SellerModel : public QAbstractTableModel
|
||||
{
|
||||
public:
|
||||
explicit SellerModel(Marketplace* market, QObject* parent = nullptr);
|
||||
explicit SellerModel(std::vector<std::shared_ptr<Seller>>& sellers, QObject* parent = nullptr);
|
||||
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
||||
virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
|
||||
|
||||
private:
|
||||
Marketplace* marketplace_;
|
||||
std::vector<std::shared_ptr<Seller>>* sellers_;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -17,20 +17,20 @@ BOOST_AUTO_TEST_CASE(store_seller_fail)
|
|||
{
|
||||
Database db(":memory:");
|
||||
db.init();
|
||||
std::vector<std::unique_ptr<Seller>> sellers;
|
||||
sellers.push_back(std::make_unique<Seller>());
|
||||
sellers.push_back(std::make_unique<Seller>());
|
||||
std::vector<std::shared_ptr<Seller>> sellers;
|
||||
sellers.push_back(std::make_shared<Seller>());
|
||||
sellers.push_back(std::make_shared<Seller>());
|
||||
BOOST_CHECK_THROW(db.storeSellers(sellers), std::runtime_error);
|
||||
}
|
||||
BOOST_AUTO_TEST_CASE(store_sellers_succ)
|
||||
{
|
||||
Database db(":memory:");
|
||||
db.init();
|
||||
std::vector<std::unique_ptr<Seller>> sellers;
|
||||
auto a = std::make_unique<Seller>();
|
||||
std::vector<std::shared_ptr<Seller>> sellers;
|
||||
auto a = std::make_shared<Seller>();
|
||||
a->createUuid();
|
||||
a->setSellerNo(1);
|
||||
auto b = std::make_unique<Seller>("Max", "Mustermann");
|
||||
auto b = std::make_shared<Seller>("Max", "Mustermann");
|
||||
b->createUuid();
|
||||
b->setSellerNo(2);
|
||||
auto c = std::make_shared<Article>();
|
||||
|
@ -39,8 +39,8 @@ BOOST_AUTO_TEST_CASE(store_sellers_succ)
|
|||
c->setDescription("Test");
|
||||
b->addArticle(c);
|
||||
BOOST_TEST(a->getUuid() != b->getUuid());
|
||||
sellers.push_back(std::move(a));
|
||||
sellers.push_back(std::move(b));
|
||||
sellers.push_back(a);
|
||||
sellers.push_back(b);
|
||||
BOOST_CHECK_NO_THROW(db.storeSellers(sellers));
|
||||
}
|
||||
|
||||
|
@ -48,11 +48,12 @@ BOOST_AUTO_TEST_CASE(seller_states)
|
|||
{
|
||||
Database db(":memory:");
|
||||
db.init();
|
||||
std::vector<std::unique_ptr<Seller>> sellers;
|
||||
auto a = std::make_unique<Seller>();
|
||||
std::vector<std::shared_ptr<Seller>> sellers;
|
||||
auto a = std::make_shared<Seller>();
|
||||
a->setSellerNo(3);
|
||||
a->createUuid();
|
||||
sellers.push_back(std::move(a));
|
||||
sellers.push_back(a);
|
||||
std::cout << "Anzahl sellers: " << sellers.size() << "\n";
|
||||
BOOST_TEST((sellers.at(0)->getState() == Entity::State::NEW));
|
||||
BOOST_TEST(db.storeSellers(sellers) == 1);
|
||||
BOOST_TEST((sellers.at(0)->getState() == Entity::State::OK));
|
||||
|
|
Loading…
Reference in a new issue