Skip to content

Commit

Permalink
Shared - Add AccountRepository Class
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Apr 4, 2024
1 parent 1cb959f commit 54dc7d2
Show file tree
Hide file tree
Showing 35 changed files with 1,176 additions and 860 deletions.
2 changes: 1 addition & 1 deletion docs/po/denaro.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-04-01 21:32-0400\n"
"POT-Creation-Date: 2024-04-03 22:42-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down
1 change: 1 addition & 0 deletions libdenaro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_library(libdenaro
"src/helpers/datehelpers.cpp"
"src/models/account.cpp"
"src/models/accountmetadata.cpp"
"src/models/accountrepository.cpp"
"src/models/color.cpp"
"src/models/configuration.cpp"
"src/models/currency.cpp"
Expand Down
10 changes: 5 additions & 5 deletions libdenaro/include/models/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <tuple>
#include <unordered_map>
#include <vector>
#include <libnick/database/sqldatabase.h>
#include "accountmetadata.h"
#include "accountrepository.h"
#include "currency.h"
#include "color.h"
#include "graphtype.h"
Expand All @@ -32,8 +32,8 @@ namespace Nickvision::Money::Shared::Models
*/
Account(std::filesystem::path path);
/**
* @brief Gets the path of the account database file.
* @return The path of the account database file
* @brief Gets the path of the .nmoney file.
* @return The path of the .nmoney file
*/
const std::filesystem::path& getPath() const;
/**
Expand All @@ -42,7 +42,7 @@ namespace Nickvision::Money::Shared::Models
*/
bool isEncrypted() const;
/**
* @brief Logins to the account.
* @brief Logins in to the account.
* @brief This method also loads the account's data into memory.
* @param password The password for the account. If unencrypted, this param must be an empty string
* @return True if successful, else false
Expand Down Expand Up @@ -245,7 +245,7 @@ namespace Nickvision::Money::Shared::Models
ImportResult importFromQIF(const std::filesystem::path& path, const Color& defaultTransactionColor, const Color& defaultGroupColor);
std::filesystem::path m_path;
bool m_loggedIn;
mutable Database::SqlDatabase m_database;
AccountRepository m_repository;
AccountMetadata m_metadata;
std::unordered_map<int, Group> m_groups;
std::vector<std::string> m_tags;
Expand Down
135 changes: 135 additions & 0 deletions libdenaro/include/models/accountrepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#ifndef ACCOUNTREPOSITORY_H
#define ACCOUNTREPOSITORY_H

#include <filesystem>
#include <string>
#include <unordered_map>
#include <vector>
#include <libnick/database/sqldatabase.h>
#include "accountmetadata.h"
#include "group.h"
#include "transaction.h"

namespace Nickvision::Money::Shared::Models
{
/**
* @brief Repository (SQLite database) for the account data
*/
class AccountRepository
{
public:
/**
* @brief Constructs an AccountRepository.
* @param path The path to the repository file
*/
AccountRepository(const std::filesystem::path& path);
/**
* @brief Gets whether or not the account repository is encrypted.
* @return True if encrypted, else false
*/
bool isEncrypted() const;
/**
* @brief Logins in to the account repository.
* @param password The password to unlock the repository
* @return True if successful, else false
*/
bool login(const std::string& password);
/**
* @brief Changes the password of the account repository.
* @param password new password for the account. If removing a password from the account
* @return True if password changed successfully, else false
*/
bool changePassword(const std::string& password);
/**
* @brief Begins a transaction in the repository.
* @return True if successful, else false (false means a transaction is already in progress, run commitTransaction() first)
*/
bool beginTransaction();
/**
* @brief Commits the transaction in the repository.
* @return True if successful, else false (false means no transaction in progress, run beginTransaction() first)
*/
bool commitTransaction();
/**
* @brief Fetches the account metadata from the repository.
* @return The account metadata
*/
AccountMetadata getMetadata() const;
/**
* @brief Sets the account metadata in the repository.
* @param metadata The new account metadata
*/
void setMetadata(const AccountMetadata& metadata);
/**
* @brief Fetches the groups from the repository.
* @return The groups map
*/
std::unordered_map<int, Group> getGroups() const;
/**
* @brief Adds a group to the account.
* @param group The group to add
* @return True if successful, else false
*/
bool addGroup(const Group& group);
/**
* @brief Updates a group in the account.
* @param group The group to update
* @return True if successful, else false
*/
bool updateGroup(const Group& group);
/**
* @brief Deletes a group from the account.
* @param group The group to update
* @return True if successful, else false
*/
bool deleteGroup(const Group& group);
/**
* @brief Fetches the tags from the repository.
* @return The tags vector
*/
std::vector<std::string> getTags() const;
/**
* @brief Fetches the transactions from the repository.
* @return The transactions map
*/
std::unordered_map<int, Transaction> getTransactions() const;
/**
* @brief Fetches the upcoming transactions from the repository.
* @param threshold The date threshold for upcoming transactions
* @return The upcoming transactions
*/
std::vector<Transaction> getUpcomingTransactions(const boost::gregorian::date& threshold) const;
/**
* @brief Adds a transaction to the account
* @param transaction The transaction to add
* @return True if successful, else false
*/
bool addTransaction(const Transaction& transaction);
/**
* @brief Updates a transaction in the account
* @param transaction The transaction to update
* @param updateGenerated Whether or not to update generated transactions associated with this transaction if it is a source transaction
* @return True if successful, else false
*/
bool updateTransaction(const Transaction& transaction, bool updateGenerated = true);
/**
* @brief Deletes a transaction from the account.
* @param transaction The transaction to delete
* @param deleteGenerated Whether or not to delete generated transactions associated with this transaction if it is a source transaction
* @return True if successful, else false
*/
bool deleteTransaction(const Transaction& transaction, bool deleteGenerated = true);
/**
* @brief Deletes generated repeat transactions from the account.
* @param sourceId The id of the source transaction
* @return True if successful, else false
*/
bool deleteGeneratedTransactions(int sourceId);

private:
mutable Database::SqlDatabase m_database;
bool m_transactionInProcess;
};
}

#endif //ACCOUNTREPOSITORY_H
Loading

0 comments on commit 54dc7d2

Please sign in to comment.