5 Commits

43 changed files with 1143 additions and 188 deletions

113
.gitignore vendored
View File

@@ -1,38 +1,87 @@
# user ignore # This file is used to ignore files which are generated
build # ----------------------------------------------------------------------------
opds.db
# ---> C++ *~
# Prerequisites *.autosave
*.d *.a
*.core
# Compiled Object files *.moc
*.slo
*.lo
*.o *.o
*.obj *.obj
*.orig
# Precompiled Headers *.rej
*.gch
*.pch
# Compiled Dynamic libraries
*.so *.so
*.dylib *.so.*
*.dll *_pch.h.cpp
*_resource.rc
# Fortran module files *.qm
*.mod .#*
*.smod *.*#
core
# Compiled Static libraries !core/
*.lai tags
*.la .DS_Store
*.a .directory
*.lib *.debug
Makefile*
# Executables *.prl
*.exe
*.out
*.app *.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
*.qbs.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
# Directories with generated files
.moc/
.obj/
.pch/
.rcc/
.uic/
/build*/
ccache.log
*-odb.*xx

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "redkbuild"]
path = redkbuild
url = git@gitea.redkit-lab.work:redkit-lab/redkbuild.git

View File

@@ -1,58 +0,0 @@
cmake_minimum_required(VERSION 3.16)
project(cpp-opds)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ODB_INCLUDE_DIRS /usr/include)
set(ODB_LIBRARY_DIRS /usr/lib/x86_64-linux-gnu)
include_directories(${ODB_INCLUDE_DIRS})
link_directories(${ODB_LIBRARY_DIRS})
find_package(Qt6 REQUIRED COMPONENTS Core Sql Network)
# find_package(ODB REQUIRED)
# find_package(SQLite3 REQUIRED)
find_library(ODB_LIBRARIES NAMES odb HINTS ${ODB_LIBRARY_DIRS})
find_library(ODB_SQLITE_LIBRARIES NAMES odb-sqlite HINTS ${ODB_LIBRARY_DIRS})
set(CMAKE_AUTOMOC ON) # <-- Включаем автоматическую генерацию MOC-файлов
# include_directories(${ODB_INCLUDE_DIRS} ${SQLite3_INCLUDE_DIRS})
# link_directories(${ODB_LIBRARY_DIRS} ${SQLite3_LIBRARY_DIRS})
# add_executable(cpp-opds
# src/main.cpp
# include/backend.cpp
# )
set(SOURCE_FILES
src/main.cpp
# src/database.cpp
# src/book.cpp
# src/author.hpp
)
set(ODB_SOURCES
src/book
src/author
)
# Генерация ODB файлов
foreach(file ${ODB_SOURCES})
add_custom_command(
OUTPUT ${file}.odb.cpp ${file}.odb.hpp
COMMAND odb --generate-query --generate-schema --database sqlite ${file}.hpp
DEPENDS ${file}.hpp
COMMENT "Running ODB on ${file}.hpp"
)
list(APPEND SOURCE_FILES ${file}.odb.cpp)
endforeach()
add_executable(cpp-opds ${SOURCE_FILES})
target_include_directories(cpp-opds PRIVATE include)
# target_link_libraries(cpp-opds Qt6::Core Qt6::Sql Qt6::Network ${ODB_LIBRARIES} ${SQLite3_LIBRARIES})
target_link_libraries(cpp-opds Qt6::Core Qt6::Sql Qt6::Network ${ODB_LIBRARIES} ${ODB_SQLITE_LIBRARIES})

33
Dockerfile Normal file
View File

@@ -0,0 +1,33 @@
# Используем образ с поддержкой Qt и инструментов сборки
FROM nexus.redkit-lab.ru:8084/rkl/dev-scada-debian-10:v1.1
RUN apt update && apt upgrade -y && apt-get clean -y && apt-get autoremove --purge -y
# Создаем пользователя, дабы выходящие файлы не были под root
# ENV USER_NAME=ubuilder
# RUN adduser $USER_NAME; \
# usermod -aG sudo $USER_NAME; \
# echo "$USER_NAME:123" | chpasswd
# USER $USER_NAME
# ENV HOME=/home/$USER_NAME
# WORKDIR $HOME
RUN qbs config defaultProfile qt_clang19_51513
# Создаем рабочую директорию
WORKDIR /app
# Копируем локальный код в контейнер
COPY . .
# Собираем проект
# RUN qbs setup-toolchains --type gcc /usr/bin/g++ gcc && \
# qbs setup-qt /usr/lib/qt5/bin/qmake qt5 && \
# qbs resolve && \
# qbs build --jobs $(nproc)
# Указываем команду для запуска приложения
# CMD ["./dockertools/run.sh"]
# CMD ["/bin/bash"]

16
docker-compose.yml Normal file
View File

@@ -0,0 +1,16 @@
version: '3.8'
services:
cpp_opds:
build:
context: .
dockerfile: Dockerfile
image: cpp_opds:latest
container_name: cpp_opds
ports:
- "8000:80"
volumes:
- .:/app
environment:
- ENV_VAR=value
restart: unless-stopped

View File

@@ -1,25 +0,0 @@
#include "backend.h"
#include <QtCore/QDebug>
Backend::Backend(QObject* parent) :
QObject(parent)
{
// Используем SQLite по умолчанию, для PostgreSQL потребуется изменить параметры подключения
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("opds.db");
if (!db.open())
{
qDebug() << "Error: Unable to open database";
}
}
Backend::~Backend()
{
qDebug() << "Backend stoped.";
}
void Backend::start()
{
qDebug() << "Backend started.";
}

View File

@@ -1,21 +0,0 @@
#ifndef BACKEND_H
#define BACKEND_H
#include <QObject>
#include <QtSql>
class Backend : public QObject
{
Q_OBJECT
public:
explicit Backend(QObject* parent = nullptr);
~Backend() override;
void start();
private:
QSqlDatabase db;
};
#endif // BACKEND_H

18
project.qbs Normal file
View File

@@ -0,0 +1,18 @@
import qbs
Project {
name: "opds++"
qbsSearchPaths: [
"redkbuild"
]
references: [
"src/cpp-opds.qbs",
"src/database/database.qbs",
"src/restapi/restapi.qbs",
"src/model/model.qbs",
"src/repositories/repositories.qbs",
"src/services/services.qbs",
"src/sql_builder/sql_builder.qbs",
]
}

1
redkbuild Submodule

Submodule redkbuild added at 9f3a81cc23

View File

@@ -1,13 +0,0 @@
#pragma db object
class Author {
public:
Author() = default;
Author(const std::string& name) : name(name) {}
const std::string& getName() const { return name; }
private:
#pragma db id auto
int id;
std::string name;
};

View File

@@ -1,22 +0,0 @@
#pragma db object
class Book {
public:
Book() = default;
Book(const std::string& title, const std::string& author, const std::string& filePath,
int fileSize, const std::string& format);
const std::string& getTitle() const { return title; }
const std::string& getAuthor() const { return author; }
const std::string& getFilePath() const { return filePath; }
int getFileSize() const { return fileSize; }
const std::string& getFormat() const { return format; }
private:
#pragma db id auto
int id;
std::string title;
std::string author;
std::string filePath;
int fileSize;
std::string format;
};

27
src/cpp-opds.qbs Normal file
View File

@@ -0,0 +1,27 @@
/*!
\qmltype cpp-opds
\inherits Application
\brief Описание
*/
PSApplication {
cpp.defines: [
// You can make your code fail to compile if it uses deprecated APIs.
// In order to do so, uncomment the following line.
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
"DATABASE_SQLITE"]
consoleApplication: true
// Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
Depends { name: "Qt"; submodules: ["core", "sql", "network"] }
Depends { name: "cpp" }
Depends { name: "database" }
Depends { name: "services" }
Depends { name: "restapi" }
Group {
name: "cpp"
files: [
"main.cpp",
]
}
} // Application

View File

@@ -1,13 +0,0 @@
#include "database.h"
#include <odb/sqlite/database.hxx>
#include <odb/schema-catalog.hxx>
#include "book-odb.hxx"
#include "author-odb.hxx"
std::unique_ptr<odb::sqlite::database> initializeDatabase(const std::string& dbPath) {
auto db = std::make_unique<odb::sqlite::database>(dbPath, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
odb::transaction t(db->begin());
odb::schema_catalog::create_schema(*db);
t.commit();
return db;
}

16
src/database/database.qbs Normal file
View File

@@ -0,0 +1,16 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: "sql"}
Depends { name: "model" }
Depends { name: "sql_builder" }
Group {
name: "cpp"
files: [
"**/*.h",
"**/*.cpp"
]
}
}

View File

@@ -0,0 +1,38 @@
#include "databasemanager.h"
#include <model/author.h>
#include <model/book.h>
DatabaseManager::DatabaseManager() {}
DatabaseManager::~DatabaseManager()
{
if (db.isOpen())
db.close();
}
DatabaseManager& DatabaseManager::instance()
{
static DatabaseManager instance;
return instance;
}
bool DatabaseManager::connect(const QString& dbPath)
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbPath);
return db.open();
}
QSqlDatabase& DatabaseManager::database()
{
return db;
}
void DatabaseManager::initializeDatabase()
{
QSqlQuery query;
query.exec(model::createTableBook());
query.exec(model::createTableAuthor());
}

View File

@@ -0,0 +1,21 @@
#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H
#include <QDebug>
#include <QtSql>
class Q_DECL_EXPORT DatabaseManager
{
public:
static DatabaseManager& instance();
bool connect(const QString& dbPath);
QSqlDatabase& database();
void initializeDatabase();
private:
DatabaseManager();
~DatabaseManager();
QSqlDatabase db;
};
#endif // DATABASEMANAGER_H

View File

@@ -1,12 +1,65 @@
#include <QCoreApplication> #include <QCoreApplication>
#include "backend.h"
int main(int argc, char *argv[]) #include <restapi/restapiserver.h>
#include <database/databasemanager.h>
#include <services/author_service.h>
#include <services/book_service.h>
#include <QDebug>
int main(int argc, char* argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
Backend backend; if (!DatabaseManager::instance().connect("library.db"))
backend.start(); {
qCritical() << "Ошибка: не удалось подключиться к базе данных!";
return -1;
}
DatabaseManager::instance().initializeDatabase();
if (BookService::add({ .title = "Война и мир",
.author = { .name = "Лев Толстой" },
.filePath = "/books/war_and_peace.epub" }))
{
qDebug() << "Книга успешно добавлена!";
}
else
{
qCritical() << "Ошибка при добавлении книги!";
}
QVector<model::Book> books = BookService::fetchAll();
for (const model::Book& book : books)
{
qDebug() << "ID:" << book.id << "Название:" << book.title
<< "Автор:" << book.author.name << "Файл:" << book.filePath;
}
QVector<model::Author> authors = AuthorService::fetchAll();
for (const model::Author& author : authors)
{
qDebug() << "ID:" << author.id << "Имя:" << author.name;
}
RestApiServer server;
server.start(8080);
// Set up code that uses the Qt event loop here.
// Call a.quit() or a.exit() to quit the application.
// A not very useful example would be including
// #include <QTimer>
// near the top of the file and calling
// QTimer::singleShot(5000, &a, &QCoreApplication::quit);
// which quits the application after 5 seconds.
// If you do not need a running Qt event loop, remove the call
// to a.exec() or use the Non-Qt Plain C++ Application template.
return a.exec(); return a.exec();
} }

56
src/model/author.h Normal file
View File

@@ -0,0 +1,56 @@
#ifndef AUTHOR_H
#define AUTHOR_H
#include <sql_builder/create_table.h>
#include <QString>
namespace model
{
namespace authorconst
{
const QString TABLE = "authors";
const QString ID = "id";
const QString NAME = "name";
// const QString M_AUTHOR_ID = "id";
// const QString M_AUTHOR_FIRST_NAME = "first_name";
// const QString M_AUTHOR_LAST_NAME = "last_name";
// const QString M_AUTHOR_YEAR = "year";
}
struct Author
{
int id = 0;
QString name;
// QString firstName;
// QString lastName;
// QString birthdayYear;
};
static inline QString createTableAuthor()
{
// query.exec("CREATE TABLE IF NOT EXISTS authors ("
// "id INTEGER PRIMARY KEY AUTOINCREMENT,"
// "name TEXT UNIQUE NOT NULL"
// ")");
Builder::TableSchema tBuilder(model::authorconst::TABLE);
tBuilder.addColumn({ .name = model::authorconst::ID,
.type = "INTEGER",
.primaryKey = true });
tBuilder.addColumn({ .name = model::authorconst::NAME,
.type = "TEXT",
.notNull = true,
.unique = true });
return tBuilder.get();
}
} // namespace model
#endif // AUTHOR_H

65
src/model/book.h Normal file
View File

@@ -0,0 +1,65 @@
#ifndef BOOK_H
#define BOOK_H
#include <model/author.h>
#include <sql_builder/create_table.h>
#include <QString>
namespace model
{
namespace bookconst
{
const QString TABLE = "books";
const QString ID = "id";
const QString TITLE = "title";
const QString AUTHOR_ID = "author_id";
const QString FILEPATH = "file_path";
}
struct Book
{
int id = 0;
QString title;
Author author;
QString filePath;
};
static inline QString createTableBook()
{
// query.exec("CREATE TABLE IF NOT EXISTS books ("
// "id INTEGER PRIMARY KEY AUTOINCREMENT,"
// "title TEXT NOT NULL,"
// "file_path TEXT NOT NULL,"
// "author_id INTEGER NOT NULL,"
// "FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE"
// ");");
Builder::TableSchema tBuilder(model::bookconst::TABLE);
tBuilder.addColumn({ .name = model::bookconst::ID,
.type = "INTEGER",
.primaryKey = true });
tBuilder.addColumn({ .name = model::bookconst::TITLE,
.type = "TEXT",
.notNull = true });
tBuilder.addColumn({ .name = model::bookconst::FILEPATH,
.type = "TEXT",
.notNull = true });
tBuilder.addColumn({ .name = model::bookconst::AUTHOR_ID,
.type = "INTEGER",
.notNull = true,
.foreignKeyTable = model::authorconst::TABLE,
.foreignKeyColumn = model::authorconst::ID });
return tBuilder.get();
}
} // namespace model
#endif // BOOK_H

15
src/model/model.qbs Normal file
View File

@@ -0,0 +1,15 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: "core", "sql"}
Depends { name: "sql_builder" }
Group {
name: "cpp"
files: [
"*.h",
"*.cpp"
]
}
}

View File

@@ -0,0 +1,78 @@
// #define BUILD_REPOSITORIES
#include "author_repository.h"
#include <sql_builder/insert.h>
#include <sql_builder/select.h>
#include <QDebug>
QVector<model::Author> AuthorRepository::getAll()
{
QVector<model::Author> authors;
Builder::Select sBuilder({ .tableName = model::authorconst::TABLE, .rows = { model::authorconst::ID, model::authorconst::NAME } });
QSqlQuery query(sBuilder.get());
qDebug() << query.lastError().text();
while (query.next())
{
authors.append({ query.value(0).toInt(),
query.value(1).toString() });
}
return authors;
}
std::optional<model::Author> AuthorRepository::getByName(const QString& name)
{
Q_UNUSED(name);
Builder::Select sBuilder({ .tableName = model::authorconst::TABLE, .rows = { model::authorconst::ID, model::authorconst::NAME } });
QString qResult = sBuilder.get() + QString(" WHERE %1 = \"%2\"").arg(model::authorconst::NAME).arg(name);
QSqlQuery query(qResult);
if (query.next())
{
model::Author author({ .id = query.value(0).toInt(),
.name = query.value(1).toString() });
return { author };
}
return std::nullopt;
}
bool AuthorRepository::insert(const model::Author& author)
{
QSqlQuery query(DatabaseManager::instance().database());
Builder::Insert iBuild({ model::authorconst::TABLE,
{ model::authorconst::NAME },
true });
query.prepare(iBuild.get());
query.bindValue(":name", author.name);
int authorId = 0;
Q_UNUSED(authorId);
if (!query.exec())
{
qDebug() << query.lastError().text();
if (query.lastError().nativeErrorCode() == 19)
{
// Автор уже есть, ищем его ID
QSqlQuery findQuery(DatabaseManager::instance().database());
findQuery.prepare("SELECT id FROM authors WHERE name = :name");
findQuery.bindValue(":name", author.name);
if (findQuery.exec() && findQuery.next())
{
authorId = findQuery.value(0).toInt();
return true;
}
}
qCritical() << "Ошибка добавления автора:" << query.lastError().text();
return false;
}
authorId = query.lastInsertId().toInt();
return true;
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <model/author.h>
#include <database/databasemanager.h>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>
#include <QVariant>
#include <QVector>
#include <optional>
#ifdef BUILD_REPOSITORIES
#define REPOSITORIES_EXPORT Q_DECL_EXPORT
#else
#define REPOSITORIES_EXPORT Q_DECL_IMPORT
#endif
class REPOSITORIES_EXPORT AuthorRepository
{
public:
static QVector<model::Author> getAll();
static std::optional<model::Author> getByName(const QString& name);
static bool insert(const model::Author& author);
// TODO Возвращать не bool - а номер id записи
};

View File

@@ -0,0 +1,59 @@
// #define BUILD_REPOSITORIES
#include "book_repository.h"
#include <QDebug>
#include <qsqlerror.h>
#include <sql_builder/insert.h>
#include <sql_builder/select.h>
QVector<model::Book> BookRepository::getAll()
{
QVector<model::Book> books;
Builder::Select qBuilder {
model::bookconst::TABLE,
{ model::bookconst::ID,
model::bookconst::TITLE,
model::bookconst::AUTHOR_ID,
model::bookconst::FILEPATH }
};
QSqlQuery query(qBuilder.get());
qDebug() << query.lastError().text();
while (query.next())
{
books.append({ query.value(0).toInt(),
query.value(1).toString(),
{ query.value(2).toInt() },
query.value(3).toString() });
}
// TODO получить так же авторов как строку
return books;
}
bool BookRepository::insert(const model::Book& book)
{
QSqlQuery query(DatabaseManager::instance().database());
Builder::Insert iBuilder {
model::bookconst::TABLE,
{ model::bookconst::TITLE,
model::bookconst::FILEPATH,
model::bookconst::AUTHOR_ID }
};
query.prepare(iBuilder.get());
query.bindValue(":" + model::bookconst::TITLE, book.title);
query.bindValue(":" + model::bookconst::FILEPATH, book.filePath);
query.bindValue(":" + model::bookconst::AUTHOR_ID, book.author.id);
bool res = query.exec();
if (!res)
qDebug() << query.lastError().text();
return res;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include <model/book.h>
#include <database/databasemanager.h>
#include <QSqlQuery>
#include <QVariant>
#include <QVector>
#ifdef BUILD_REPOSITORIES
#define REPOSITORIES_EXPORT Q_DECL_EXPORT
#else
#define REPOSITORIES_EXPORT Q_DECL_IMPORT
#endif
class REPOSITORIES_EXPORT BookRepository
{
public:
static QVector<model::Book> getAll();
static bool insert(const model::Book& book);
};

View File

@@ -0,0 +1,21 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: "core", "sql"}
Depends { name: "database" }
Depends { name: "sql_builder" }
Group {
name: "cpp"
files: [
"*.h",
"*.cpp"
]
}
Properties {
condition: qbs.buildVariant === "debug"
cpp.defines: ["BUILD_REPOSITORIES"]
}
}

15
src/restapi/restapi.qbs Normal file
View File

@@ -0,0 +1,15 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: ["core", "sql", "network"] }
Depends { name: "services" }
Group {
name: "cpp"
files: [
"*.h",
"*.cpp"
]
}
}

View File

@@ -0,0 +1,121 @@
#include "restapiserver.h"
#include <services/author_service.h>
#include <services/book_service.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
RestApiServer::RestApiServer(QObject* parent) :
QTcpServer(parent) {}
void RestApiServer::start(quint16 port)
{
if (!this->listen(QHostAddress::Any, port))
{
qWarning() << "Ошибка запуска сервера!";
}
else
{
qDebug() << "REST API сервер запущен на порту:" << port;
qDebug() << QString("http://127.0.0.1:%1").arg(port);
}
}
void RestApiServer::incomingConnection(qintptr socketDescriptor)
{
QTcpSocket* socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
connect(socket, &QTcpSocket::readyRead, this, &RestApiServer::handleRequest);
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
}
void RestApiServer::handleRequest()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if (!socket)
return;
QString request = QString::fromUtf8(socket->readAll());
QByteArray response = processRequest(request);
socket->write(response);
socket->flush();
socket->disconnectFromHost();
}
QByteArray RestApiServer::processRequest(const QString& request)
{
if (request.startsWith("GET /books/author/"))
{
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
// Books books;
// QList<BookRecord> results = books.getBooksByAuthor(author);
QByteArray jsonResponse = "{ \"books\": [";
// for (const BookRecord& book : results)
// {
// jsonResponse += QString("{ \"id\": %1, \"title\": \"%2\", \"author\": \"%3\", \"year\": %4 },")
// .arg(book.id)
// .arg(book.title)
// .arg(book.author)
// .arg(book.year)
// .toUtf8();
// }
// if (results.size() > 0)
// jsonResponse.chop(1); // Убираем последнюю запятую
// jsonResponse += "] }";
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse;
}
if (request.startsWith("GET /books"))
{
auto books = BookService::fetchAll();
QJsonArray jArray;
for (auto it = books.begin(); it != books.end(); ++it)
{
const auto& book = *it;
QJsonObject j;
j["id"] = QString::number(book.id);
j["title"] = QString(book.title);
j["author"] = QString(book.author.name);
j["local_path"] = book.filePath;
jArray.push_back(j);
}
auto jDoc = QJsonDocument(jArray);
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
}
if (request.startsWith("GET /author"))
{
// auto books = BookService::fetchAll();
auto authors = AuthorService::fetchAll();
QJsonArray jArray;
for (auto it = authors.begin(); it != authors.end(); ++it)
{
const auto& author = *it;
QJsonObject j;
j["id"] = QString::number(author.id);
j["name"] = QString(author.name);
// j["last"] = QString::fromStdString(author.last());
// j["age"] = author.age();
jArray.push_back(j);
}
auto jDoc = QJsonDocument(jArray);
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
}
return "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nNot Found";
}

View File

@@ -0,0 +1,24 @@
#ifndef RESTAPISERVER_H
#define RESTAPISERVER_H
#include <QTcpServer>
#include <QTcpSocket>
class Q_DECL_EXPORT RestApiServer : public QTcpServer
{
Q_OBJECT
public:
explicit RestApiServer(QObject* parent = nullptr);
void start(quint16 port = 8080);
protected:
void incomingConnection(qintptr socketDescriptor) override;
private slots:
void handleRequest();
private:
QByteArray processRequest(const QString& request);
};
#endif // RESTAPISERVER_H

View File

@@ -0,0 +1,12 @@
// #define BUILD_SERVICES
#include "author_service.h"
QVector<model::Author> AuthorService::fetchAll()
{
return AuthorRepository::getAll();
}
bool AuthorService::add(const model::Author& author)
{
return AuthorRepository::insert(author);
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <model/author.h>
#include <repositories/author_repository.h>
#include <QVector>
#ifdef BUILD_SERVICES
#define SERVICES_EXPORT Q_DECL_EXPORT
#else
#define SERVICES_EXPORT Q_DECL_IMPORT
#endif
class SERVICES_EXPORT AuthorService
{
public:
static QVector<model::Author> fetchAll();
static bool add(const model::Author& name);
};

View File

@@ -0,0 +1,28 @@
// #define BUILD_SERVICES
#include "book_service.h"
#include "author_service.h"
QVector<model::Book> BookService::fetchAll()
{
return BookRepository::getAll();
}
bool BookService::add(const model::Book& book)
{
auto res = AuthorRepository::getByName(book.author.name);
if (!res.has_value())
{
if (!AuthorService::add(book.author))
{
qCritical() << "Ошибка добавления автора!";
return false;
}
res = AuthorRepository::getByName(book.author.name);
}
model::Book BookAuthor = book;
BookAuthor.author = res.value();
return BookRepository::insert(BookAuthor);
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <model/book.h>
#include <repositories/book_repository.h>
#include <QVector>
#ifdef BUILD_SERVICES
#define SERVICES_EXPORT Q_DECL_EXPORT
#else
#define SERVICES_EXPORT Q_DECL_IMPORT
#endif
class SERVICES_EXPORT BookService
{
public:
static QVector<model::Book> fetchAll();
static bool add(const model::Book& book);
};

22
src/services/services.qbs Normal file
View File

@@ -0,0 +1,22 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: "core", "sql"}
Depends { name: "repositories" }
Group {
name: "cpp"
files: [
"*.h",
"*.cpp"
]
}
Properties {
condition: qbs.buildVariant === "debug"
cpp.defines: ["BUILD_SERVICES"]
}
}

View File

@@ -0,0 +1,45 @@
#include "create_table.h"
#include <QDebug>
namespace Builder
{
QString Column::toSQL() const
{
QString columnDef = name + " " + type;
if (primaryKey)
columnDef += " PRIMARY KEY";
if (notNull)
columnDef += " NOT NULL";
if (unique)
columnDef += " UNIQUE";
if (defaultValue)
columnDef += " DEFAULT " + *defaultValue;
if (foreignKeyTable && foreignKeyColumn)
{
columnDef += " REFERENCES " + *foreignKeyTable + "(" + *foreignKeyColumn + ")";
}
return columnDef;
}
TableSchema& TableSchema::addColumn(const Column& column)
{
columns.push_back(column);
return *this;
}
QString TableSchema::get() const
{
QStringList columnDefs;
for (const auto& col : columns)
{
columnDefs.append(col.toSQL());
}
QString result = "CREATE TABLE IF NOT EXISTS " + name + " ( " + columnDefs.join(", ") + " );";
// qDebug() << result;
return result;
}
} // namespace Builder

View File

@@ -0,0 +1,50 @@
#pragma once
#include <QString>
#include <QStringList>
#include <QVector>
#include <optional>
namespace Builder
{
struct Column
{
QString name; // Имя колонки
QString type; // Тип колонки
bool primaryKey = false; // Является ли колонка первичным ключом?
bool notNull = false; // Колонка не должна быть пуста
bool unique = false; // Значение в колонке должны быть уникальны
std::optional<QString> defaultValue;
std::optional<QString> foreignKeyTable;
std::optional<QString> foreignKeyColumn;
/*!
* \brief Сформировать SQL код для колонки
*/
QString toSQL() const;
};
class Q_DECL_EXPORT TableSchema
{
public:
TableSchema(QString tableName) :
name(std::move(tableName)) {}
/*!
* \brief Добавить колонку
*/
TableSchema& addColumn(const Column& column);
/*!
* \brief Сформировать SQL код для создания таблицы
*/
QString get() const;
private:
QString name; // Имя таблицы
QVector<Column> columns; // Список колонок таблицы
};
} // namespace Builder

View File

@@ -0,0 +1,39 @@
#include "insert.h"
#include <QDebug>
namespace Builder
{
namespace
{
const QString INSERT = QStringLiteral("INSERT");
const QString INTO = QStringLiteral("INTO");
const QString VALUES = QStringLiteral("VALUES");
const QString IGNORE = QStringLiteral("IGNORE");
}
QString Insert::get() const
{
QString resultString = QString("%1 ").arg(INSERT);
if (orIgnore)
resultString += QString("OR %1 ").arg(IGNORE);
resultString += QString("%1 %2 ").arg(INTO).arg(tableName);
resultString += QString("( %1 ) ").arg(rows.join(", "));
// TODO как будто бы проще сразу это заполнить значениями
QStringList allValue;
for (const auto& row : rows)
allValue.push_back(QString(":%1").arg(row));
resultString += QString("%1 ( %2 )").arg(VALUES).arg(allValue.join(", "));
resultString += ';';
// qDebug() << resultString;
return resultString;
}
} // namespace Builder

22
src/sql_builder/insert.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <QString>
#include <QStringList>
namespace Builder
{
struct Q_DECL_EXPORT Insert
{
QString tableName; // Имя таблицы
QStringList rows; // Именования колонок
// QStringList params;
bool orIgnore = false;
/*!
* \brief Сформировать SQL код для выполнения
*/
QString get() const;
};
} // namespace Builder

View File

@@ -0,0 +1,32 @@
#include "select.h"
#include <QDebug>
namespace Builder
{
namespace
{
const QString SELECT = QStringLiteral("SELECT");
const QString FROM = QStringLiteral("FROM");
const QString WHERE = QStringLiteral("WHERE");
}
QString Select::get() const
{
QString allRows = rows.join(", ");
QString resultString = QString("%1 %2").arg(SELECT).arg(allRows);
resultString += QString(" %1 %2").arg(FROM).arg(tableName);
if (!where.isEmpty())
resultString += QString(" %1 %2").arg(WHERE).arg(where);
// resultString += ';';
return resultString;
}
} // namespace Builder

21
src/sql_builder/select.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <QString>
#include <QStringList>
namespace Builder
{
struct Q_DECL_EXPORT Select
{
QString tableName; // Имя таблицы
QStringList rows = { "*" }; // Колонки
QString where = {}; // Условие поиска
/*!
* \brief Сформировать SQL код для выполнения
*/
QString get() const;
};
} // namespace Builder

View File

@@ -0,0 +1,14 @@
import qbs
PSLibrary {
Depends { name: "Qt"; submodules: "sql"}
Group {
name: "cpp"
files: [
"**/*.h",
"**/*.cpp"
]
}
}

View File

@@ -0,0 +1 @@
GET http://localhost:8080/books/

View File

@@ -0,0 +1 @@
GET http://localhost:8080/books/author/George%20Orwell