-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add docker compose and postgresql to store users
- Loading branch information
1 parent
df4b80f
commit aabeb2d
Showing
13 changed files
with
360 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
version: '3.8' | ||
services: | ||
db: | ||
image: postgres:15 | ||
environment: | ||
POSTGRES_USER: myuser | ||
POSTGRES_PASSWORD: mypassword | ||
POSTGRES_DB: usersdb | ||
ports: | ||
- "5432:5432" | ||
volumes: | ||
- pgdata:/var/lib/postgresql/data | ||
|
||
http-server: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
ports: | ||
- "7777:7777" | ||
environment: | ||
DB_HOST: db | ||
DB_USER: myuser | ||
DB_PASSWORD: mypassword | ||
DB_NAME: usersdb | ||
depends_on: | ||
- db | ||
|
||
volumes: | ||
pgdata: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "Database.hpp" | ||
|
||
Database::Database(const std::string& connInfo) : _conn(connInfo) {} | ||
|
||
bool Database::addUser(const User& user) { | ||
try { | ||
pqxx::work txn(_conn); | ||
txn.exec_params("INSERT INTO users (userId, username, password, isAdmin) VALUES ($1, $2, $3, $4)", | ||
user.userId, user.username, user.password, user.isAdmin); | ||
txn.commit(); | ||
return true; | ||
} catch (const std::exception& e) { | ||
return false; | ||
} | ||
} | ||
|
||
bool Database::getUser(const std::string& userId, User& user) { | ||
try { | ||
pqxx::work txn(_conn); | ||
auto row = txn.exec_params("SELECT userId, username, password, isAdmin FROM users WHERE userId = $1", userId); | ||
if (row.size() == 1) { | ||
user.userId = row[0][0].as<std::string>(); | ||
user.username = row[0][1].as<std::string>(); | ||
user.password = row[0][2].as<std::string>(); | ||
user.isAdmin = row[0][3].as<bool>(); | ||
return true; | ||
} | ||
return false; | ||
} catch (const std::exception& e) { | ||
return false; | ||
} | ||
} | ||
|
||
bool Database::deleteUser(const std::string& userId) { | ||
try { | ||
pqxx::work txn(_conn); | ||
txn.exec_params("DELETE FROM users WHERE userId = $1", userId); | ||
txn.commit(); | ||
return true; | ||
} catch (const std::exception& e) { | ||
return false; | ||
} | ||
} | ||
|
||
bool Database::updateUser(const User& user) { | ||
try { | ||
pqxx::work txn(_conn); | ||
txn.exec_params("UPDATE users SET username = $2, password = $3, isAdmin = $4 WHERE userId = $1", | ||
user.userId, user.username, user.password, user.isAdmin); | ||
txn.commit(); | ||
return true; | ||
} catch (const std::exception& e) { | ||
return false; | ||
} | ||
} | ||
|
||
std::vector<User> Database::getAllUsers() { | ||
std::vector<User> users; | ||
try { | ||
pqxx::work txn(_conn); | ||
auto rows = txn.exec("SELECT userId, username, password, isAdmin FROM users"); | ||
for (const auto &row : rows) { | ||
User user; | ||
user.userId = row[0].as<std::string>(); | ||
user.username = row[1].as<std::string>(); | ||
user.password = row[2].as<std::string>(); | ||
user.isAdmin = row[3].as<bool>(); | ||
users.push_back(user); | ||
} | ||
} catch (const std::exception& e) { } | ||
return users; | ||
} | ||
|
||
bool Database::getUserByUsername(const std::string& username, User& user) { | ||
try { | ||
pqxx::work txn(_conn); | ||
auto row = txn.exec_params("SELECT userId, username, password, isAdmin FROM users WHERE username = $1", username); | ||
if (row.size() == 1) { | ||
user.userId = row[0][0].as<std::string>(); | ||
user.username = row[0][1].as<std::string>(); | ||
user.password = row[0][2].as<std::string>(); | ||
user.isAdmin = row[0][3].as<bool>(); | ||
return true; | ||
} | ||
return false; | ||
} catch (const std::exception& e) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef _DATABASE_HPP_ | ||
#define _DATABASE_HPP_ | ||
|
||
#include <pqxx/pqxx> | ||
|
||
struct User { | ||
std::string userId; | ||
std::string username; | ||
std::string password; | ||
bool isAdmin; | ||
}; | ||
|
||
class Database { | ||
public: | ||
Database(const std::string& connInfo); | ||
bool addUser(const User& user); | ||
bool getUser(const std::string& userId, User& user); | ||
bool deleteUser(const std::string& userId); | ||
bool updateUser(const User& user); | ||
std::vector<User> getAllUsers(); | ||
bool getUserByUsername(const std::string& username, User& user); | ||
|
||
private: | ||
pqxx::connection _conn; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.