Data can now delete their associated file through deleteObject

This commit is contained in:
quentin 2022-09-06 17:02:15 -05:00
parent cd45dd848f
commit 53ba7c9cc9
4 changed files with 15 additions and 3 deletions

View File

@ -18,6 +18,8 @@ public:
explicit AccountData(const std::string &file, const std::string &name);
Account *getAccount();
using Data<Account>::deleteObject;
private:
Account account;

View File

@ -24,10 +24,13 @@ public:
std::string getFilePath();
void flushToFile();
void deleteObject();
private:
const std::string fileName;
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
std::string fileDirectory;
bool flush = true;
virtual bool isJsonCorrect() = 0;
};

View File

@ -52,5 +52,13 @@ void Data<T>::flushToFile() {
template<class T>
Data<T>::~Data() {
flushToFile();
if (flush) {
flushToFile();
}
}
template<class T>
void Data<T>::deleteObject() {
remove(getFilePath().c_str());
flush = false;
}

View File

@ -12,7 +12,6 @@
#include <string>
#include <filesystem>
#include <list>
#include <iostream>
using namespace Budget;
@ -56,7 +55,7 @@ int main(int argc, char *argv[]) {
accountOptHandler.help();
}
if (accountOptHandler.getSetOpts()->create) {
AccountData account(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json", accountOptHandler.getSetOpts()->createAccount);
accounts.emplace_back(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json", accountOptHandler.getSetOpts()->createAccount);
}
}
return 0;