Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame and tests for the database #2

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build/

.vscode

*.log
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.14)
project(syncview)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(PkgConfig)
pkg_check_modules(LIBCONFIG REQUIRED IMPORTED_TARGET libconfig++)

add_subdirectory(database)

add_executable(${PROJECT_NAME} main.cpp)

add_compile_options(--coverage)
add_link_options(--coverage)

option(BUILD_TESTS "build tests" OFF)
if(BUILD_TESTS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -coverage -lgcov" )
enable_testing()
add_subdirectory(tests)
endif()

target_include_directories(${PROJECT_NAME} PUBLIC ${DATABASE_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${DATABASE_LIBRARIES})
14 changes: 14 additions & 0 deletions database/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.0.0)
project(database)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpqxx -lpq")

find_library(PQXX_LIB pqxx)
find_library(PQ_LIB pq)

add_library(${PROJECT_NAME} src/PostgreSQL.cpp src/PostgreConnection.cpp src/UsersTable.cpp src/RoomsTable.cpp src/ViewersTable.cpp src/BidsTable.cpp src/VotesTable.cpp src/FilmsTable.cpp)
target_link_libraries(${PROJECT_NAME} ${PQXX_LIB} ${PQ_LIB} PkgConfig::LIBCONFIG)
target_include_directories(${PROJECT_NAME} PUBLIC include)

set(DATABASE_LIBRARIES ${PROJECT_NAME} PARENT_SCOPE)
set(DATABASE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
25 changes: 25 additions & 0 deletions database/include/BidsTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "PostgreSQL.hpp"

class BidsTable
{
public:
BidsTable() = default;
BidsTable(std::shared_ptr<IDatabase> client);

json addBid(const json &info) const;
json deleteBid(const size_t id) const;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в объявлении обычно const у value не пишут

json updateBid(const json &info) const;
json isEnded(const size_t id) const;
json getVotesFor(const size_t id) const;
json getVotesAgainst(const size_t id) const;
json getVotesCount(const size_t id) const;
json getPointsFor(const size_t id) const;
json getPointsAgainst(const size_t id) const;
json getPointsSum(const size_t id) const;
json getWinners(const size_t id, const bool answer) const;

private:
std::shared_ptr<IDatabase> client;
};
18 changes: 18 additions & 0 deletions database/include/FilmsTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "PostgreSQL.hpp"

class FilmsTable
{
public:
FilmsTable() = default;
FilmsTable(std::shared_ptr<IDatabase> client);

json addFilm(const json &info) const;
json deleteFilm(const size_t id) const;
json updateFilm(const json &info) const;
json getFilmInfo(const size_t id) const;

private:
std::shared_ptr<IDatabase> client;
};
21 changes: 21 additions & 0 deletions database/include/IDatabase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>

#include "json.hpp"
#include "constants.hpp"

using json = nlohmann::json;

class IDatabase
{
public:
virtual json createTable(json) = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кажется немного странно конвернтировать из представления базы данных в json, чтобы потом json cконвертировать уже в классы вашей бизнес логики. возможно было бы лучше возвращать тут std::string

virtual json dropTable(std::string) = 0;
virtual json select(json) = 0;
virtual json insert(json) = 0;
virtual json remove(json) = 0;
virtual json update(json) = 0;

virtual ~IDatabase() = default;
};
30 changes: 30 additions & 0 deletions database/include/PostgreConnection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <iostream>
#include <string>
#include <map>

#include <libconfig.h++>

#include "constants.hpp"

using libconfig::Config;

class PostgreConnection
{
public:
PostgreConnection(std::string config_path);

PostgreConnection(std::string dbname, std::string user, std::string password = "postgres",
std::string host = "127.0.0.1", std::string hostaddr = "", std::string port = "5432");

std::map<std::string, std::string> reformat() const;

private:
std::string dbname;
std::string user;
std::string password;
std::string host;
std::string hostaddr;
std::string port;
};
29 changes: 29 additions & 0 deletions database/include/PostgreSQL.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <iostream>
#include <memory>
#include <pqxx/pqxx>
#include <string>

#include "IDatabase.hpp"
#include "PostgreConnection.hpp"

class PostgreSQL : public IDatabase
{
public:
PostgreSQL() = default;
PostgreSQL(std::shared_ptr<PostgreConnection> _connectionParams);

json createTable(json) override;
json dropTable(std::string) override;
json update(json) override;
json insert(json) override;
json select(json) override;
json remove(json) override;

~PostgreSQL() = default;

private:
std::shared_ptr<PostgreConnection> connectionParams;
std::shared_ptr<pqxx::connection> connection;
};
22 changes: 22 additions & 0 deletions database/include/RoomsTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "PostgreSQL.hpp"

class RoomsTable
{
public:
RoomsTable() = default;
RoomsTable(std::shared_ptr<IDatabase> client);

json addRoom(const json &info) const;
json deleteRoom(const size_t id) const;
json updateRoom(const json &info) const;
bool checkRoom(const size_t id) const;
json getRoomInfo(const size_t id) const;
json getAllRooms() const;
json getCurrentFilm(const size_t id) const;
json checkCurrentFilm(const size_t id) const;

private:
std::shared_ptr<IDatabase> client;
};
21 changes: 21 additions & 0 deletions database/include/UsersTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "PostgreSQL.hpp"

class UsersTable
{
public:
UsersTable() = default;
UsersTable(std::shared_ptr<IDatabase> client);

json addUser(const json &info) const;
json deleteUser(const size_t id) const;
json updateUser(const json &info) const;
bool checkUserByID(const size_t id) const;
bool checkUserByEmail(const std::string &email) const;
bool checkUserByUsername(const std::string &username) const;
json getUserInfo(const size_t id) const;

private:
std::shared_ptr<IDatabase> client;
};
22 changes: 22 additions & 0 deletions database/include/ViewersTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "PostgreSQL.hpp"

class ViewersTable
{
public:
ViewersTable() = default;
ViewersTable(std::shared_ptr<IDatabase> client);

json getUserRooms(const size_t id_user) const;
json getRoomUsers(const size_t id_room) const;

json addUserToRoom(const size_t id_user, const size_t id_room) const;
json getUserPointsInRoom(const size_t id_user, const size_t id_room) const;
json setUserPointsInRoom(const size_t id_user, const size_t id_room, const size_t points) const;
json getUserRoleInRoom(const size_t id_user, const size_t id_room) const;
json setUserRoleInRoom(const size_t id_user, const size_t id_room, const std::string &role) const;

private:
std::shared_ptr<IDatabase> client;
};
17 changes: 17 additions & 0 deletions database/include/VotesTable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "PostgreSQL.hpp"

class VotesTable
{
public:
VotesTable() = default;
VotesTable(std::shared_ptr<IDatabase> client);

json addVote(const json &info) const;
json deleteVote(const size_t id) const;
json updateVote(const json &info) const;

private:
std::shared_ptr<IDatabase> client;
};
8 changes: 8 additions & 0 deletions database/include/constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>

const std::string STATUS_FIELD = "status";
const std::string PARAMS_FIELD = "parameters";
const std::string ERROR_STATUS = "error";
const std::string SUCCESS_STATUS = "ok";
Loading