Skip to content

Commit

Permalink
Added account storing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Duta-Sebastian committed Nov 30, 2024
1 parent 68a708b commit 34b4ec4
Show file tree
Hide file tree
Showing 23 changed files with 332 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 2 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The CI/CD pipeline is configured in GitHub Actions and includes multiple stages
- The first two stages validate code correctness and ensure no warnings are generated (using Cppcheck and Clang-Tidy).

2. Compatibility Testing Across Multiple Operating Systems:
- The next five stages ensure proper code execution on major operating systems (Linux, macOS, and Windows) and perform memory checks using MSAN, ASAN, and Valgrind to identify potential memory leaks and other memory-related issues.
- The next five stages ensure proper code execution on major operating systems (Linux, macOS, and Windows) and perform memory checks using ASAN and Valgrind to identify potential memory leaks and other memory-related issues.

3. Functional Test Execution:
- The final stage runs functional tests to verify that the application behaves as expected.
Expand Down Expand Up @@ -60,38 +60,13 @@ Verificarea calității codului:
Primele două etape validează corectitudinea codului și se asigură că nu generează warnings ( folosind `Cppcheck` si `Clang-Tidy` ).

Testarea compatibilității pe multiple sisteme de operare:
Următoarele cinci etape asigură rularea corectă a codului pe principalele sisteme de operare (Linux, macOS și Windows) și efectuează verificări de memorie folosind MSAN, ASAN și Valgrind, pentru a identifica posibile memory leaks și alte probleme legate de memorie.
Următoarele cinci etape asigură rularea corectă a codului pe principalele sisteme de operare (Linux, macOS și Windows) și efectuează verificări de memorie folosind ASAN și Valgrind, pentru a identifica posibile memory leaks și alte probleme legate de memorie.

Rularea testelor funcționale:
Ultima etapă rulează testele pentru a verifica dacă aplicația funcționează conform așteptărilor.

În toate etapele care implică rularea aplicatiei este inclus un serviciu PostgreSQL pentru a permite rularea corectă a aplicației.

## Milestone #0

- [ ] Nume proiect (poate fi schimbat ulterior)
- [ ] Scurtă descriere a temei alese, ce v-ați propus să implementați

## Milestone #1

#### Cerințe

- [ ] definirea a minim **3-4 clase** folosind compunere cu clasele definite de voi
- [ ] constructori de inițializare cu parametri
- [ ] pentru o aceeași (singură) clasă: constructor de copiere, `operator=` de copiere, destructor
- [ ] `operator<<` pentru toate clasele pentru afișare (std::ostream)
- [ ] cât mai multe `const` (unde este cazul)
- [ ] implementarea a minim 3 funcții membru publice pentru funcționalități specifice temei alese, dintre care cel puțin
1-2 funcții mai complexe
- nu doar citiri/afișări sau adăugat/șters elemente într-un/dintr-un vector
- [ ] scenariu de utilizare a claselor definite:
- preferabil sub formă de teste unitare, mai ales dacă vorbim de aplicații consolă
- crearea de obiecte și apelarea tuturor funcțiilor membru publice în main
- vor fi adăugate în fișierul `tastatura.txt` DOAR exemple de date de intrare de la tastatură (dacă există); dacă
aveți nevoie de date din fișiere, creați alte fișiere separat
- [ ] tag de `git`: de exemplu `v0.1`
- [ ] serviciu de integrare continuă (CI); exemplu: GitHub Actions

## Milestone #2

#### Cerințe
Expand Down
5 changes: 4 additions & 1 deletion infrastructure/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ ENV POSTGRES_DB=password_keeper
ENV POSTGRES_USER=user
ENV POSTGRES_PASSWORD=temp

COPY SQL_Scripts/users.sql /docker-entrypoint-initdb.d/
COPY SQL_Scripts/1_users.sql /docker-entrypoint-initdb.d/
COPY SQL_Scripts/2_bankaccounts.sql /docker-entrypoint-initdb.d/
COPY SQL_Scripts/3_emailaccounts.sql.sql /docker-entrypoint-initdb.d/
COPY SQL_Scripts/4_socialmediaaccounts.sql.sql /docker-entrypoint-initdb.d/
File renamed without changes.
8 changes: 8 additions & 0 deletions infrastructure/SQL_Scripts/2_bankaccounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS bankaccounts (
id SERIAL references users(id),
username VARCHAR(50) NOT NULL,
password varchar(50) NOT NULL,
IBAN varchar(50) NOT NULL,
bank varchar(50) NOT NULL,
constraint pk_bankaccount PRIMARY KEY (username,password,IBAN,bank)
);
8 changes: 8 additions & 0 deletions infrastructure/SQL_Scripts/3_emailaccounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS emailaddresses (
id SERIAL references users(id),
username VARCHAR(50) NOT NULL,
password varchar(50) NOT NULL,
emailAddress varchar(50) NOT NULL,
mailProvider varchar(50) NOT NULL,
constraint pk_bankaccount PRIMARY KEY (username,password,emailAddress,mailProvider)
);
8 changes: 8 additions & 0 deletions infrastructure/SQL_Scripts/4_socialmediaaccounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS socialmediaaccounts (
id SERIAL references users(id),
username VARCHAR(50) NOT NULL,
password varchar(50) NOT NULL,
platform varchar(50) NOT NULL,
profileUrl varchar(50) NOT NULL,
constraint pk_bankaccount PRIMARY KEY (username,password,platform,profileUrl)
);
12 changes: 12 additions & 0 deletions src/Accounts/Account.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Account.h"

Account::Account(const std::string &username, const std::string &password) :
username(username), password(password) {}

std::string Account::getUsername() const {
return this->username;
}

std::string Account::getPassword() const {
return this->password;
}
27 changes: 27 additions & 0 deletions src/Accounts/Account.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>

enum AccountType {
BankAccountType,
EmailAccountType,
SocialMediaAccountType
};

class Account {
protected:
std::string username;
std::string password;
[[nodiscard]] virtual AccountType getAccountType() const = 0;
public:
Account(const std::string&, const std::string&);
virtual void addAccount() = 0;
virtual ~Account() = default;

virtual std::string getUsername() const;
virtual std::string getPassword() const;
};



#endif //ACCOUNT_H
27 changes: 27 additions & 0 deletions src/Accounts/BankAccount.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "BankAccount.h"

#include <Database.h>

#include <utility>

BankAccount::BankAccount(const std::string &username, const std::string &password,
std::string IBAN, std::string bank)
: Account(username,password), IBAN(std::move(IBAN)), bank(std::move(bank)){}

AccountType BankAccount::getAccountType() const {
return BankAccountType;
}

void BankAccount::addAccount() {
const Database &database = Database::getDatabaseInstance();
const auto bankAccount = std::make_shared<BankAccount>(*this);
database.addUserDefinedAccount(bankAccount, this->getAccountType());
}

std::string BankAccount::getIBAN() const {
return this->IBAN;
}

std::string BankAccount::getBank() const {
return this->bank;
}
20 changes: 20 additions & 0 deletions src/Accounts/BankAccount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include "Account.h"


class BankAccount final : public Account {
std::string IBAN;
std::string bank;
[[nodiscard]] AccountType getAccountType() const override;
public:
BankAccount(const std::string &, const std::string &, std::string , std::string );
void addAccount() override;

[[nodiscard]] std::string getIBAN() const;
[[nodiscard]] std::string getBank() const;
};



#endif //BANKACCOUNT_H
25 changes: 25 additions & 0 deletions src/Accounts/EmailAccount.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "EmailAccount.h"

#include <Database.h>

AccountType EmailAccount::getAccountType() const {
return EmailAccountType;
}

EmailAccount::EmailAccount(const std::string &username, const std::string &password,
const std::string &emailAddress, const std::string &mailProvider)
: Account(username, password), emailAddress(emailAddress), mailProvider(mailProvider) {}

void EmailAccount::addAccount() {
const Database &database = Database::getDatabaseInstance();
const auto emailAccount = std::make_shared<EmailAccount>(*this);
database.addUserDefinedAccount(emailAccount, this->getAccountType());
}

std::string EmailAccount::getEmailAddress() const {
return this->emailAddress;
}

std::string EmailAccount::getMailProvider() const {
return this->mailProvider;
}
21 changes: 21 additions & 0 deletions src/Accounts/EmailAccount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef EMAILACCOUNT_H
#define EMAILACCOUNT_H
#include "Account.h"


class EmailAccount final: public Account {
std::string emailAddress;
std::string mailProvider;
[[nodiscard]] AccountType getAccountType() const override;
public:
explicit EmailAccount(const std::string&, const std::string&, const std::string&, const std::string&);

void addAccount() override;

[[nodiscard]] std::string getEmailAddress() const;
[[nodiscard]] std::string getMailProvider() const;
};



#endif //EMAILACCOUNT_H
25 changes: 25 additions & 0 deletions src/Accounts/SocialMediaAccount.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "SocialMediaAccount.h"

#include <Database.h>

AccountType SocialMediaAccount::getAccountType() const {
return SocialMediaAccountType;
}

SocialMediaAccount::SocialMediaAccount(const std::string &username, const std::string& password,
const std::string &platform, const std::string & profileUrl)
: Account(username, password), platform(platform), profileUrl(profileUrl) {}

void SocialMediaAccount::addAccount() {
const Database &database = Database::getDatabaseInstance();
const auto emailAccount = std::make_shared<SocialMediaAccount>(*this);
database.addUserDefinedAccount(emailAccount, this->getAccountType());
}

std::string SocialMediaAccount::getPlatform() const {
return this->platform;
}

std::string SocialMediaAccount::getProfileUrl() const {
return this->profileUrl;
}
21 changes: 21 additions & 0 deletions src/Accounts/SocialMediaAccount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef SOCIALMEDIAACCOUNT_H
#define SOCIALMEDIAACCOUNT_H
#include "Account.h"


class SocialMediaAccount final : public Account {
std::string platform;
std::string profileUrl;
[[nodiscard]] AccountType getAccountType() const override;
public:
SocialMediaAccount(const std::string&, const std::string&, const std::string&, const std::string&);

void addAccount() override;

[[nodiscard]] std::string getPlatform() const;
[[nodiscard]] std::string getProfileUrl() const;
};



#endif //SOCIALMEDIAACCOUNT_H
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ add_library(${PROJECT_NAME}_lib
Database/Auth.cpp
Utils/EnvVarManager.cpp
Logger/Logger.cpp
Exceptions/DatabaseExceptions/DatabaseExceptions.cpp
Accounts/Account.cpp
Accounts/BankAccount.cpp
Accounts/EmailAccount.cpp
Accounts/SocialMediaAccount.cpp
)

target_include_directories(${PROJECT_NAME}_lib SYSTEM PRIVATE
Expand All @@ -27,6 +32,9 @@ target_include_directories(${PROJECT_NAME}_lib PUBLIC
User
Utils
Logger
Exceptions
Exceptions/DatabaseExceptions
Accounts
)

target_link_directories(${PROJECT_NAME}_lib PUBLIC ${pqxx_SOURCE_DIR}/lib)
Expand Down
53 changes: 53 additions & 0 deletions src/Database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <iostream>

#include "../Accounts/BankAccount.h"
#include "../Accounts/EmailAccount.h"
#include "../Accounts/SocialMediaAccount.h"

std::string Database::connString;

Database::Database() {
Expand Down Expand Up @@ -113,3 +117,52 @@ User Database::getUserByUsername(std::string &username) const {
throw;
}
}

void Database::addUserDefinedAccount(const std::shared_ptr<Account>& account,const AccountType &accountType) const {
pqxx::work work(*connection);
std::string query;
switch (accountType) {
case BankAccountType: {
const auto bankAccount = dynamic_pointer_cast<BankAccount>(account);
query = "INSERT INTO bankaccounts VALUES (" +
work.quote(User::getCurrentUserId()) + ", " +
work.quote(bankAccount->getUsername()) + ", " +
work.quote(bankAccount->getPassword()) + ", " +
work.quote(bankAccount->getIBAN()) + ", " +
work.quote(bankAccount->getBank()) + ");";
break;
}
case EmailAccountType: {
const auto emailAccount = dynamic_pointer_cast<EmailAccount>(account);
query = "INSERT INTO bankaccounts VALUES (" +
work.quote(User::getCurrentUserId()) + ", " +
work.quote(emailAccount->getUsername()) + ", " +
work.quote(emailAccount->getPassword()) + ", " +
work.quote(emailAccount->getEmailAddress()) + ", " +
work.quote(emailAccount->getMailProvider()) + ");";
break;
}
case SocialMediaAccountType: {
const auto socialMediaAccount = dynamic_pointer_cast<SocialMediaAccount>(account);
query = "INSERT INTO bankaccounts VALUES (" +
work.quote(User::getCurrentUserId()) + ", " +
work.quote(socialMediaAccount->getUsername()) + ", " +
work.quote(socialMediaAccount->getPassword()) + ", " +
work.quote(socialMediaAccount->getPlatform()) + ", " +
work.quote(socialMediaAccount->getProfileUrl()) + ");";
break;
}
default: {
work.abort();
throw std::runtime_error("Unknown account type");
}
}
try {
work.exec_params(query);
work.commit();
}
catch (const pqxx::data_exception &e) {
throw std::runtime_error("Data error");
}

}
4 changes: 4 additions & 0 deletions src/Database/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define DATABASE_H

#include <pqxx/pqxx>

#include "../Accounts/Account.h"
#include "../User/User.h"

class Database {
Expand Down Expand Up @@ -34,6 +36,8 @@ class Database {
[[nodiscard]] int getNumberOfUsers() const;

User getUserByUsername(std::string &username) const;

void addUserDefinedAccount(const std::shared_ptr<Account> &account, const AccountType &) const;
};


Expand Down
1 change: 1 addition & 0 deletions src/Exceptions/DatabaseExceptions/DatabaseExceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "DatabaseExceptions.h"
Loading

0 comments on commit 34b4ec4

Please sign in to comment.