more on sellers

This commit is contained in:
Martin Brodbeck 2018-07-16 15:30:24 +02:00
parent 207edb27ba
commit a2ad2eb57d
8 changed files with 74 additions and 38 deletions

View file

@ -145,7 +145,7 @@ void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); }
unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& sellers)
unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& sellers)
{
int retCode{};
int count{};
@ -165,10 +165,9 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
int test = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"),
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"),
@ -255,11 +254,13 @@ unsigned int Database::storeSellers(std::vector<std::shared_ptr<Seller>>& seller
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) {
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) {
seller->cleanupArticles();
seller->setState(Seller::State::OK);
}

View file

@ -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::shared_ptr<Seller>>& sellers);
unsigned int storeSellers(std::vector<std::unique_ptr<Seller>>& sellers);
private:
sqlite3* db_;

View file

@ -3,8 +3,8 @@
Marketplace::Marketplace()
{
auto seller = std::make_shared<Seller>("Max", "Mustermann");
sellers_.push_back(seller);
auto seller = std::make_unique<Seller>("Max", "Mustermann");
sellers_.push_back(std::move(seller));
}
void Marketplace::storeToDb()
@ -15,4 +15,4 @@ void Marketplace::storeToDb()
db.storeSellers(sellers_);
}
std::vector<std::shared_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }

View file

@ -13,11 +13,11 @@ class Marketplace
Marketplace();
void storeToDb();
void loadFromDb();
std::vector<std::shared_ptr<Seller>>& getSellers();
std::vector<std::unique_ptr<Seller>>& getSellers();
private:
std::vector<std::shared_ptr<Seller>> sellers_;
std::vector<std::shared_ptr<Sale>> sales_;
std::vector<std::unique_ptr<Seller>> sellers_;
std::vector<std::unique_ptr<Sale>> sales_;
};
#endif

View file

@ -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; }
inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
inline void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
void Seller::addArticle(std::shared_ptr<Article> article)
{
@ -51,10 +51,11 @@ int Seller::numArticlesOffered() const { return numArticlesOffered_; }
void Seller::cleanupArticles()
{
articles_.erase(std::remove_if(begin(articles_), end(articles_),
articles_.erase(std::remove_if(articles_.begin(), articles_.end(),
[](const std::shared_ptr<Article>& article) {
return article->getState() == Article::State::DELETE;
}));
}),
articles_.end());
for (auto& article : articles_) {
article->setState(Article::State::OK);

View file

@ -1,6 +1,6 @@
#include "sellermodel.h"
SellerModel::SellerModel(std::vector<std::shared_ptr<Seller>>& sellers, QObject* parent)
SellerModel::SellerModel(std::vector<std::unique_ptr<Seller>>& sellers, QObject* parent)
: QAbstractTableModel(parent), sellers_(&sellers)
{
}
@ -56,3 +56,36 @@ 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 = sellers_->at(index.row()).get();
switch (index.column()) {
case 0:
seller->setUuidFromString(value.toString().toStdString());
break;
case 1:
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;
}
dataChanged(index, index);
return true;
}

View file

@ -8,14 +8,16 @@
class SellerModel : public QAbstractTableModel
{
public:
explicit SellerModel(std::vector<std::shared_ptr<Seller>>& sellers, QObject* parent = nullptr);
explicit SellerModel(std::vector<std::unique_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);
private:
std::vector<std::shared_ptr<Seller>>* sellers_;
std::vector<std::unique_ptr<Seller>>* sellers_;
};
#endif

View file

@ -17,20 +17,20 @@ BOOST_AUTO_TEST_CASE(store_seller_fail)
{
Database db(":memory:");
db.init();
std::vector<std::shared_ptr<Seller>> sellers;
sellers.push_back(std::make_shared<Seller>());
sellers.push_back(std::make_shared<Seller>());
std::vector<std::unique_ptr<Seller>> sellers;
sellers.push_back(std::make_unique<Seller>());
sellers.push_back(std::make_unique<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::shared_ptr<Seller>> sellers;
auto a = std::make_shared<Seller>();
std::vector<std::unique_ptr<Seller>> sellers;
auto a = std::make_unique<Seller>();
a->createUuid();
a->setSellerNo(1);
auto b = std::make_shared<Seller>("Max", "Mustermann");
auto b = std::make_unique<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(a);
sellers.push_back(b);
sellers.push_back(std::move(a));
sellers.push_back(std::move(b));
BOOST_CHECK_NO_THROW(db.storeSellers(sellers));
}
@ -48,12 +48,11 @@ BOOST_AUTO_TEST_CASE(seller_states)
{
Database db(":memory:");
db.init();
std::vector<std::shared_ptr<Seller>> sellers;
auto a = std::make_shared<Seller>();
std::vector<std::unique_ptr<Seller>> sellers;
auto a = std::make_unique<Seller>();
a->setSellerNo(3);
a->createUuid();
sellers.push_back(a);
std::cout << "Anzahl sellers: " << sellers.size() << "\n";
sellers.push_back(std::move(a));
BOOST_TEST((sellers.at(0)->getState() == Entity::State::NEW));
BOOST_TEST(db.storeSellers(sellers) == 1);
BOOST_TEST((sellers.at(0)->getState() == Entity::State::OK));