Compare commits

...

2 commits

Author SHA1 Message Date
e06458b419 more on seller dialog 2018-07-17 11:40:09 +02:00
90e132bcc8 load sellers 2018-07-17 11:09:35 +02:00
7 changed files with 59 additions and 7 deletions

View file

@ -399,3 +399,39 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
return count; return count;
} }
unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers)
{
int retCode{};
int count{};
sqlite3_stmt* stmt;
retCode = sqlite3_prepare_v2(db_,
"SELECT id, seller_no, first_name, last_name, "
"num_offered_articles FROM sellers ORDER BY seller_no",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
retCode = sqlite3_step(stmt);
sellers.clear();
while (retCode != SQLITE_DONE) {
++count;
auto seller = std::make_unique<Seller>();
seller->setUuidFromString(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
seller->setSellerNo(sqlite3_column_int(stmt, 1));
seller->setFirstName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
seller->setLastName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
seller->setNumArticlesOffered(sqlite3_column_int(stmt, 4));
seller->setState(Seller::State::OK);
sellers.push_back(std::move(seller));
retCode = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
return count;
}

View file

@ -17,6 +17,7 @@ class Database
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
void exec(const std::string& sql); void exec(const std::string& sql);
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers); unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers);
unsigned int loadSellers(std::vector<std::unique_ptr<Seller>>& sellers);
private: private:
sqlite3* db_{nullptr}; sqlite3* db_{nullptr};

View file

@ -12,13 +12,15 @@ Marketplace::Marketplace()
void Marketplace::storeToDb() void Marketplace::storeToDb()
{ {
//const std::string DB_PATH{"/tmp/kima2.db"};
Database db; Database db;
db.storeSellers(sellers_); db.storeSellers(sellers_);
} }
void Marketplace::loadFromDb() {} void Marketplace::loadFromDb()
{
Database db;
db.loadSellers(sellers_);
}
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; } std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }

View file

@ -27,7 +27,7 @@ void MainWindow::on_actionEditSeller_triggered()
STATUSBAR_TIMEOUT); STATUSBAR_TIMEOUT);
} else { } else {
marketplace_->loadFromDb(); marketplace_->loadFromDb();
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten <b>verworfen</b>.", statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten verworfen!",
STATUSBAR_TIMEOUT); STATUSBAR_TIMEOUT);
} }
} }

View file

@ -18,6 +18,7 @@ void SellerDialog::on_newButton_clicked()
{ {
ui_.tableView->model()->insertRows(ui_.tableView->model()->rowCount(), 1); ui_.tableView->model()->insertRows(ui_.tableView->model()->rowCount(), 1);
ui_.tableView->scrollToBottom(); ui_.tableView->scrollToBottom();
ui_.tableView->selectRow(ui_.tableView->model()->rowCount() - 1);
} }
void SellerDialog::accept() void SellerDialog::accept()

View file

@ -5,6 +5,14 @@
<property name="windowModality"> <property name="windowModality">
<enum>Qt::NonModal</enum> <enum>Qt::NonModal</enum>
</property> </property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -38,7 +46,7 @@
<item> <item>
<widget class="QPushButton" name="newButton"> <widget class="QPushButton" name="newButton">
<property name="text"> <property name="text">
<string>Neu</string> <string>&amp;Neu</string>
</property> </property>
</widget> </widget>
</item> </item>

View file

@ -22,6 +22,10 @@ QVariant SellerModel::data(const QModelIndex& index, int role) const
return QVariant(); return QVariant();
Seller* seller = marketplace_->getSellers().at(index.row()).get(); Seller* seller = marketplace_->getSellers().at(index.row()).get();
if (seller->getState() == Seller::State::DELETE)
return QVariant::Invalid;
switch (index.column()) { switch (index.column()) {
case 0: case 0:
return seller->getUuidAsString().c_str(); return seller->getUuidAsString().c_str();
@ -117,12 +121,12 @@ bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int r
bool SellerModel::insertRows(int row, int count, const QModelIndex& parent) bool SellerModel::insertRows(int row, int count, const QModelIndex& parent)
{ {
beginInsertRows(parent, row, row + count - 1); emit beginInsertRows(parent, row, row + count - 1);
auto seller = std::make_unique<Seller>(); auto seller = std::make_unique<Seller>();
seller->createUuid(); seller->createUuid();
seller->setSellerNo(marketplace_->getNextSellerNo()); seller->setSellerNo(marketplace_->getNextSellerNo());
marketplace_->getSellers().push_back(std::move(seller)); marketplace_->getSellers().push_back(std::move(seller));
endInsertRows(); emit endInsertRows();
return true; return true;
} }