Skip to content

Commit

Permalink
Small fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Duta-Sebastian committed Dec 2, 2024
1 parent 1915507 commit da6a1cf
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Accounts/Account.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>
#include <AccountType.cpp>
#include <AccountType.h>

class Account {
protected:
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/AccountFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <AccountFactory.h>
#include <AccountType.h>

#include <BankAccount.h>
#include <EmailAccount.h>
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/AccountFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>

#include <Account.h>
#include <AccountType.h>

class AccountFactory {
public:
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/BankAccount.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <AccountType.h>
#include <BankAccount.h>

#include <Database.h>
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/BankAccount.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <Account.h>

#include <AccountType.h>

class BankAccount final : public Account {
std::string IBAN;
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/EmailAccount.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <AccountType.h>
#include <EmailAccount.h>

#include <Database.h>
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/EmailAccount.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef EMAILACCOUNT_H
#define EMAILACCOUNT_H
#include <Account.h>

#include <AccountType.h>

class EmailAccount final : public Account {
std::string emailAddress;
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/SocialMediaAccount.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <AccountType.h>
#include <SocialMediaAccount.h>

#include <Database.h>
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/SocialMediaAccount.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SOCIALMEDIAACCOUNT_H
#define SOCIALMEDIAACCOUNT_H
#include <Account.h>

#include <AccountType.h>
class SocialMediaAccount final : public Account {
std::string platform;
std::string profileUrl;
Expand Down
3 changes: 0 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ add_library(${PROJECT_NAME}_lib
Accounts/SocialMediaAccount.cpp
Accounts/AccountFactory.cpp
Accounts/AccountFactory.h
Utils/VectorPlusTemplate.cpp
Utils/AccountType.cpp
Utils/AddAccountCommandTemplate.cpp
Utils/ShowAccountsCommand.cpp
)

target_include_directories(${PROJECT_NAME}_lib SYSTEM PRIVATE
Expand Down
7 changes: 4 additions & 3 deletions src/Database/Database.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "Database.h"
#include <Database.h>
#include <iostream>

#include <DatabaseExceptions.h>
#include <AccountExceptions.h>
#include <AccountFactory.h>
#include <AccountType.h>
#include <BankAccount.h>
#include <DatabaseExceptions.h>
#include <EmailAccount.h>
#include <SocialMediaAccount.h>
#include "../Utils/VectorPlusTemplate.cpp"
#include <VectorUtils.h>

std::string Database::connString;

Expand Down
9 changes: 7 additions & 2 deletions src/Utils/AccountType.cpp → src/Utils/AccountType.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <string>
#ifndef ACCOUNTTYPE_H
#define ACCOUNTTYPE_H

#include <string_view>

enum class AccountType {
BankAccountType,
EmailAccountType,
SocialMediaAccountType
};

constexpr std::string getAccountTypeString(const AccountType &accountType) {
constexpr std::string_view getAccountTypeString(const AccountType& accountType) {
switch (accountType) {
case AccountType::BankAccountType:
return "Bank Account";
Expand All @@ -18,3 +21,5 @@ constexpr std::string getAccountTypeString(const AccountType &accountType) {
return "Unknown Account Type";
}
}

#endif //ACCOUNTTYPE_H
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#ifndef SHOWACCOUNTSCOMMAND_H
#define SHOWACCOUNTSCOMMAND_H

#include <AccountType.h>
#include <Database.h>
#include <iostream>

namespace ShowAccountsCommands {
template<AccountType T>
void showAccountsCommand() {
for (const auto &database = Database::getDatabaseInstance();
const auto &account: database.getAccountsByType(T)) {
account->show();
}
}
std::cout << "---------------------------------\n";
}
} ;

void showAllAccounts() {
inline void showAllAccounts() {
showAccountsCommand<AccountType::BankAccountType>();
showAccountsCommand<AccountType::EmailAccountType>();
showAccountsCommand<AccountType::SocialMediaAccountType>();
}
}

#endif //SHOWACCOUNTSCOMMAND_H
7 changes: 6 additions & 1 deletion src/Utils/VectorPlusTemplate.cpp → src/Utils/VectorUtils.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#ifndef VECTORUTILS_H
#define VECTORUTILS_H

#include <vector>

template<typename T>
std::vector<T> operator+(const std::vector<T> &lhs, const std::vector<T> &rhs) {
std::vector<T> operator+(const std::vector<T> &lhs, const std::vector<T> &rhs){
std::vector<T> result;
result.reserve(lhs.size() + rhs.size());
result.insert(result.end(), lhs.begin(), lhs.end());
result.insert(result.end(), rhs.begin(), rhs.end());
return result;
}

#endif //VECTORUTILS_H
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#include <iostream>

#include <AccountExceptions.h>
#include "AddAccountCommandTemplate.h"
#include "ShowAccountsCommand.cpp"
#include "Database/Database.h"
#include <AddAccountCommandTemplate.h>
#include <ShowAccountsCommand.h>
#include <Database.h>

#include "Database/Auth.h"
#include "EnvironmentReader/EnvironmentReader.h"
#include "Logger/Logger.h"
#include <Auth.h>
#include <EnvironmentReader.h>
#include <Logger.h>

void initializeDatabase() {
auto &logger = Logger::getInstance();
Expand Down
15 changes: 12 additions & 3 deletions tests/AccountTestNotLoggedIn.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include <gtest/gtest.h>
#include <AccountFactory.h>
#include <AccountType.h>
#include <User.h>

TEST(AccountTest, noBankAccountCreatedWhenNotLoggedIn) {
class AccountTestNotLoggedIn : public ::testing::Test {
protected:
static void SetUpTestSuite() {
User::setCurrentUserId(-1);
}
};

TEST_F(AccountTestNotLoggedIn, noBankAccountCreatedWhenNotLoggedIn) {
const auto bankAccountFactory =
AccountFactory::accountFactory(AccountType::BankAccountType,
{
Expand All @@ -11,15 +20,15 @@ TEST(AccountTest, noBankAccountCreatedWhenNotLoggedIn) {
ASSERT_THROW(bankAccountFactory->addAccount(), std::exception);
}

TEST(AccountTest, noEmailAccountCreatedWhenNotLoggedIn) {
TEST_F(AccountTestNotLoggedIn, noEmailAccountCreatedWhenNotLoggedIn) {
const auto emailAccountFactory = AccountFactory::accountFactory(AccountType::EmailAccountType,
{{"username", "sebi1"},{"password","1234"},
{"emailAddress", "123412412"}, {"mailProvider", "bt"}
});
ASSERT_THROW(emailAccountFactory->addAccount(), std::exception);
}

TEST(AccountTest, noSocialMediaAccountCreatedWhenNotLoggedIn) {
TEST_F(AccountTestNotLoggedIn, noSocialMediaAccountCreatedWhenNotLoggedIn) {
const auto socialMediaAccountFactory =
AccountFactory::accountFactory(AccountType::SocialMediaAccountType,
{{"username", "sebi1"},{"password","1234"},
Expand Down
6 changes: 3 additions & 3 deletions tests/VectorPlusTest.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <gtest/gtest.h>
#include <VectorPlusTemplate.cpp>
#include <Account.h>
#include <VectorUtils.h>
#include <memory>

#include "AccountFactory.h"
#include <AccountFactory.h>
#include <AccountType.h>

TEST(VectorPlusTest, IntTest) {
const std::vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Expand Down

0 comments on commit da6a1cf

Please sign in to comment.