import / export sellers completed

This commit is contained in:
Martin Brodbeck 2018-08-02 12:34:56 +02:00
parent 045bc8dd20
commit 61da9adce5
5 changed files with 66 additions and 10 deletions

View file

@ -1,4 +1,5 @@
#include "jsonutil.h"
#include "database.h"
#include <json/json.h>
@ -15,17 +16,39 @@ void JsonUtil::exportSellers(const std::string& filename, Marketplace* market)
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
root["encoding"] = "UTF-8";
for (const auto& seller : market->getSellers()) {
Json::Value newEntry;
newEntry["uuid"] = seller->getUuidAsString();
newEntry["seller_no"] = seller->getSellerNo();
newEntry["last_name"] = seller->getLastName();
newEntry["first_name"] = seller->getFirstName();
newEntry["num_offered_articles"] = seller->getFirstName();
newEntry["num_offered_articles"] = seller->numArticlesOffered();
root["sellers"].append(newEntry);
}
writer->write(root, &file);
}
void JsonUtil::importSellers(const std::string& filename, Marketplace* market)
{
for (auto& seller : market->getSellers())
{
seller->setState(Seller::State::DELETE);
}
market->storeToDb(true);
Json::Value jsonValues;
std::ifstream file(filename);
file >> jsonValues;
for(auto val : jsonValues["sellers"]) {
auto seller = std::make_unique<Seller>();
seller->setUuidFromString(val["uuid"].asString());
seller->setSellerNo(val["seller_no"].asInt());
seller->setLastName(val["last_name"].asString());
seller->setFirstName(val["first_name"].asString());
seller->setNumArticlesOffered(val["num_offered_articles"].asInt());
market->getSellers().push_back(std::move(seller));
}
market->storeToDb();
}

View file

@ -9,6 +9,7 @@ class JsonUtil
{
public:
static void exportSellers(const std::string& filename, Marketplace* market);
static void importSellers(const std::string& filename, Marketplace* market);
};
#endif