Compare commits
19 Commits
11ea3a8cd8
...
Новые-табл
| Author | SHA1 | Date | |
|---|---|---|---|
| 7406bcec21 | |||
| b4fa718be6 | |||
| 78b01bc0b1 | |||
| b993116e09 | |||
| 4568ef65db | |||
| c151fdd8d1 | |||
| d01c789b06 | |||
| 34fcabc04e | |||
| 89b43f5aad | |||
| 8cb56837f5 | |||
| 96bdf1e5e8 | |||
| 0198f6d022 | |||
| 905dd309d0 | |||
| 65059351f7 | |||
| 594b3a936f | |||
| f8e504bb07 | |||
| b16ae39263 | |||
| d47988adda | |||
| 4fbe63701f |
113
.gitignore
vendored
113
.gitignore
vendored
@@ -1,38 +1,87 @@
|
||||
# user ignore
|
||||
build
|
||||
opds.db
|
||||
# This file is used to ignore files which are generated
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ---> C++
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*~
|
||||
*.autosave
|
||||
*.a
|
||||
*.core
|
||||
*.moc
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.orig
|
||||
*.rej
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.so.*
|
||||
*_pch.h.cpp
|
||||
*_resource.rc
|
||||
*.qm
|
||||
.#*
|
||||
*.*#
|
||||
core
|
||||
!core/
|
||||
tags
|
||||
.DS_Store
|
||||
.directory
|
||||
*.debug
|
||||
Makefile*
|
||||
*.prl
|
||||
*.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
|
||||
9
.gitmodules
vendored
Normal file
9
.gitmodules
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
[submodule "redkbuild"]
|
||||
path = redkbuild
|
||||
url = git@gitea.redkit-lab.work:redkit-lab/redkbuild.git
|
||||
[submodule "redkitty"]
|
||||
path = redkitty
|
||||
url = git@gitea.redkit-lab.work:redkit-lab/redkitty.git
|
||||
[submodule "external_libs/quazip"]
|
||||
path = external_libs/quazip
|
||||
url = https://github.com/stachenov/quazip.git
|
||||
@@ -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})
|
||||
34
external_libs/quazip.qbs
Normal file
34
external_libs/quazip.qbs
Normal file
@@ -0,0 +1,34 @@
|
||||
import qbs
|
||||
|
||||
PSLibrary {
|
||||
name: "quazip"
|
||||
|
||||
property string quazipPath: path + "/quazip" // Путь к исходникам QuaZip
|
||||
property string noname: {
|
||||
console.error("noname" + exportedIncludePaths)
|
||||
return "noname"
|
||||
}
|
||||
|
||||
Depends { name: "zlib" }
|
||||
|
||||
Group {
|
||||
name: "h"
|
||||
id: qh
|
||||
files: [
|
||||
quazipPath + "/quazip/*.h",
|
||||
]
|
||||
}
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
quazipPath + "/quazip/*.cpp",
|
||||
quazipPath + "/quazip/*.c"
|
||||
]
|
||||
}
|
||||
|
||||
// Export {
|
||||
// Depends { name:"cpp" }
|
||||
// cpp.includePaths: qh
|
||||
// }
|
||||
}
|
||||
@@ -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.";
|
||||
}
|
||||
@@ -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
|
||||
8
prerequisites/qbs/imports/SQLiteArtifactsExport.qbs
Normal file
8
prerequisites/qbs/imports/SQLiteArtifactsExport.qbs
Normal file
@@ -0,0 +1,8 @@
|
||||
import qbs
|
||||
|
||||
Export {
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "rsc.sqlite.settings" }
|
||||
|
||||
rsc.sqlite.settings.includeRegexValues: exportingProduct.includeRegexValue
|
||||
}
|
||||
53
prerequisites/qbs/modules/karchive/karchive.qbs
Normal file
53
prerequisites/qbs/modules/karchive/karchive.qbs
Normal file
@@ -0,0 +1,53 @@
|
||||
import qbs
|
||||
|
||||
Module {
|
||||
id: karchive
|
||||
|
||||
readonly property string karchive_path: project.sourceDirectory + "/external_libs/karchive"
|
||||
readonly property string libKF5ArchivePath: karchive.karchive_path + "/lib"
|
||||
+ (qbs.buildVariant == "release" ? "/Release" : "/Debug")
|
||||
property bool installFiles: true
|
||||
|
||||
Depends { name: "glob" }
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "zlib" }
|
||||
|
||||
Properties
|
||||
{
|
||||
condition: qbs.targetOS.contains("linux") || qbs.targetOS.contains("astra")
|
||||
cpp.includePaths: [ karchive.karchive_path + "/include/lin" ]
|
||||
cpp.libraryPaths: [ karchive.libKF5ArchivePath + "/lin" ]
|
||||
cpp.dynamicLibraries: "KF5Archive"
|
||||
}
|
||||
|
||||
Properties {
|
||||
condition: qbs.targetOS.contains("windows")
|
||||
cpp.includePaths: [ karchive.karchive_path + "/include/win" ]
|
||||
cpp.libraryPaths: [ karchive.libKF5ArchivePath + "/win" ]
|
||||
cpp.dynamicLibraries: "KF5Archive"
|
||||
}
|
||||
|
||||
InstallGroup {
|
||||
name: "karchive_lin"
|
||||
condition: qbs.targetOS.contains("linux") || qbs.targetOS.contains("astra")
|
||||
files: [
|
||||
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so",
|
||||
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so.5",
|
||||
karchive.libKF5ArchivePath + "/lin/libKF5Archive.so.5.108.0"
|
||||
]
|
||||
qbs.install: karchive.installFiles
|
||||
qbs.installDir: glob.binInstallPrefix
|
||||
}
|
||||
|
||||
InstallGroup {
|
||||
name: "karchive_win"
|
||||
condition: qbs.targetOS.contains("windows")
|
||||
files: [
|
||||
karchive.libKF5ArchivePath + "/win/KF5Archive.dll",
|
||||
karchive.libKF5ArchivePath + "/win/KF5Archive.lib"
|
||||
]
|
||||
qbs.install: karchive.installFiles
|
||||
qbs.installDir: glob.binInstallPrefix
|
||||
}
|
||||
zlib.installFiles: true
|
||||
}
|
||||
25
project.qbs
Normal file
25
project.qbs
Normal file
@@ -0,0 +1,25 @@
|
||||
import qbs
|
||||
|
||||
Project {
|
||||
name: "opds++"
|
||||
qbsSearchPaths: [
|
||||
"redkitty/qbs",
|
||||
"redkbuild",
|
||||
"redkitty/lib/redkit-gen-integration/qbs",
|
||||
"prerequisites/qbs",
|
||||
]
|
||||
|
||||
references: [
|
||||
"redkitty/lib/rsc/libs_impl/rdbaseimpl/rdbaseimpl.qbs",
|
||||
"redkitty/lib/rcore/rcore.qbs",
|
||||
|
||||
"src/cpp-opds.qbs",
|
||||
"src/database/database.qbs",
|
||||
"src/model/model.qbs",
|
||||
"src/restapi/restapi.qbs",
|
||||
"src/utils/utils.qbs",
|
||||
"src/repository/repository.qbs",
|
||||
|
||||
"external_libs/quazip.qbs",
|
||||
]
|
||||
}
|
||||
1
redkbuild
Submodule
1
redkbuild
Submodule
Submodule redkbuild added at 3adf92aad6
1
redkitty
Submodule
1
redkitty
Submodule
Submodule redkitty added at 47579f9e2e
@@ -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;
|
||||
};
|
||||
22
src/book.hpp
22
src/book.hpp
@@ -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;
|
||||
};
|
||||
45
src/cpp-opds.qbs
Normal file
45
src/cpp-opds.qbs
Normal file
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSApplication {
|
||||
// CppApplication {
|
||||
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: "cpp" }
|
||||
Depends { name: "database" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "sqlite3" }
|
||||
Depends { name: "restapi" }
|
||||
Depends { name: "redkit_gen" }
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "model" }
|
||||
Depends { name: "quazip" }
|
||||
Depends { name: "utils" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"main.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
|
||||
// }
|
||||
} // Project
|
||||
@@ -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;
|
||||
}
|
||||
105
src/database/database.hxx
Normal file
105
src/database/database.hxx
Normal file
@@ -0,0 +1,105 @@
|
||||
// file : hello/database.hxx
|
||||
// copyright : not copyrighted - public domain
|
||||
|
||||
//
|
||||
// Create concrete database instance based on the DATABASE_* macros.
|
||||
//
|
||||
|
||||
#ifndef DATABASE_HXX
|
||||
#define DATABASE_HXX
|
||||
|
||||
#include <cstdlib> // std::exit
|
||||
#include <iostream>
|
||||
#include <memory> // std::unique_ptr
|
||||
#include <string>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "database_utils.h"
|
||||
|
||||
inline uDBase openDB(const std::string path_db)
|
||||
{
|
||||
// Открыть БД, если она существует
|
||||
auto db_ptr = new oDBase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (std::filesystem::is_regular_file(path_db))
|
||||
return uDBase(db_ptr);
|
||||
|
||||
// И создать новую, если не существует
|
||||
uDBase db(db_ptr);
|
||||
|
||||
odb::connection_ptr connect_ptr(db->connection());
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
||||
|
||||
odb::transaction tans(db->begin());
|
||||
odb::schema_catalog::create_schema(*db);
|
||||
tans.commit();
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<odb::database> create_database(int& argc, char* argv[])
|
||||
{
|
||||
using namespace std;
|
||||
using namespace odb::core;
|
||||
|
||||
if (argc > 1 && argv[1] == string("--help"))
|
||||
{
|
||||
cout << "Usage: " << argv[0] << " [options]" << endl
|
||||
<< "Options:" << endl;
|
||||
|
||||
#if defined(DATABASE_MYSQL)
|
||||
odb::mysql::database::print_usage(cout);
|
||||
#elif defined(DATABASE_SQLITE)
|
||||
odb::sqlite::database::print_usage(cout);
|
||||
#elif defined(DATABASE_PGSQL)
|
||||
odb::pgsql::database::print_usage(cout);
|
||||
#elif defined(DATABASE_ORACLE)
|
||||
odb::oracle::database::print_usage(cout);
|
||||
#elif defined(DATABASE_MSSQL)
|
||||
odb::mssql::database::print_usage(cout);
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#if defined(DATABASE_MYSQL)
|
||||
unique_ptr<database> db(new odb::mysql::database(argc, argv));
|
||||
#elif defined(DATABASE_SQLITE)
|
||||
|
||||
unique_ptr<database> db(new odb::sqlite::database(argc,
|
||||
argv,
|
||||
false,
|
||||
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
||||
|
||||
// Create the database schema. Due to bugs in SQLite foreign key
|
||||
// support for DDL statements, we need to temporarily disable
|
||||
// foreign keys.
|
||||
//
|
||||
{
|
||||
connection_ptr connect_ptr(db->connection());
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
||||
|
||||
transaction tans(connect_ptr->begin());
|
||||
schema_catalog::create_schema(*db);
|
||||
tans.commit();
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
||||
}
|
||||
#elif defined(DATABASE_PGSQL)
|
||||
unique_ptr<database> db(new odb::pgsql::database(argc, argv));
|
||||
#elif defined(DATABASE_ORACLE)
|
||||
unique_ptr<database> db(new odb::oracle::database(argc, argv));
|
||||
#elif defined(DATABASE_MSSQL)
|
||||
unique_ptr<database> db(
|
||||
new odb::mssql::database(argc, argv, false, "TrustServerCertificate=yes"));
|
||||
#endif
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
#endif // DATABASE_HXX
|
||||
42
src/database/database.qbs
Normal file
42
src/database/database.qbs
Normal file
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
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",
|
||||
"DATABASE_LIBRARY"
|
||||
]
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "model" }
|
||||
|
||||
Depends { name: "redkit_gen" }
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.hxx",
|
||||
"**/*.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
}
|
||||
12
src/database/database_global.h
Normal file
12
src/database/database_global.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef DATABASE_GLOBAL_H
|
||||
#define DATABASE_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(DATABASE_LIBRARY)
|
||||
#define DATABASE_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define DATABASE_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // DATABASE_GLOBAL_H
|
||||
55
src/database/database_utils.cpp
Normal file
55
src/database/database_utils.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "database_utils.h"
|
||||
|
||||
void addBook(oDBase& db, const QString title, const QVector<QString>& authors)
|
||||
{
|
||||
odb::transaction t(db.begin());
|
||||
try
|
||||
{
|
||||
// 1. Создаем/получаем книгу
|
||||
auto books = db.query<Book_S>(odb::query<Book_S>::title == title);
|
||||
SH<Book_S> book;
|
||||
|
||||
if (books.empty())
|
||||
{
|
||||
book = SH<Book_S>::create();
|
||||
book->setTitle(title);
|
||||
|
||||
db.persist(book);
|
||||
}
|
||||
else
|
||||
{
|
||||
book = books.begin().load(); // Берем первую найденную книгу
|
||||
}
|
||||
|
||||
// 2. Обрабатываем авторов
|
||||
for (const auto& name : authors)
|
||||
{
|
||||
auto authors = db.query<Author_S>(odb::query<Author_S>::fullName == name);
|
||||
SH<Author_S> author = authors.empty() ? SH<Author_S>::create() : authors.begin().load();
|
||||
|
||||
if (authors.empty())
|
||||
{
|
||||
author->setFullName(name);
|
||||
db.persist(author);
|
||||
}
|
||||
|
||||
// Проверяем и создаем связь
|
||||
|
||||
auto links = db.query<AuthorBook_S>(
|
||||
odb::query<AuthorBook_S>::author->id == author->id()
|
||||
&& odb::query<AuthorBook_S>::book->id == book->id());
|
||||
|
||||
if (links.empty())
|
||||
{
|
||||
db.persist(SH<AuthorBook_S>::create(author, book));
|
||||
}
|
||||
}
|
||||
|
||||
t.commit();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
t.rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
38
src/database/database_utils.h
Normal file
38
src/database/database_utils.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
#include <odb/database.hxx>
|
||||
|
||||
#if defined(DATABASE_MYSQL)
|
||||
#include <odb/mysql/database.hxx>
|
||||
#elif defined(DATABASE_SQLITE)
|
||||
#include <odb/connection.hxx>
|
||||
#include <odb/schema-catalog.hxx>
|
||||
#include <odb/sqlite/database.hxx>
|
||||
#include <odb/transaction.hxx>
|
||||
#elif defined(DATABASE_PGSQL)
|
||||
#include <odb/pgsql/database.hxx>
|
||||
#elif defined(DATABASE_ORACLE)
|
||||
#include <odb/oracle/database.hxx>
|
||||
#elif defined(DATABASE_MSSQL)
|
||||
#include <odb/mssql/database.hxx>
|
||||
#else
|
||||
#error unknown database; did you forget to define the DATABASE_* macros?
|
||||
#endif
|
||||
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
using oDBase = odb::sqlite::database;
|
||||
using uDBase = U<oDBase>;
|
||||
|
||||
#include <model/books/author_book_s-odb.hxx>
|
||||
#include <model/books/author_book_s.h>
|
||||
#include <model/books/author_s-odb.hxx>
|
||||
#include <model/books/author_s.h>
|
||||
#include <model/books/book_s-odb.hxx>
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
void DATABASE_EXPORT addBook(oDBase& db, const QString title, const QVector<QString>& authors);
|
||||
150
src/main.cpp
150
src/main.cpp
@@ -1,12 +1,156 @@
|
||||
#include <QCoreApplication>
|
||||
#include "backend.h"
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QPointer>
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
#include <restapi/restapiserver.h>
|
||||
|
||||
/* Опыты с odb */
|
||||
#include <database/database.hxx> // create_database
|
||||
#include <database/database_utils.h>
|
||||
#include <odb/database.hxx>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <utils/fb2extractor.h>
|
||||
#include <utils/zipwrapper.h>
|
||||
|
||||
void fillBooksBD(uDBase& db)
|
||||
{
|
||||
try
|
||||
{
|
||||
QVector<Author_S> authors = {
|
||||
Author_S("George Orwell"),
|
||||
Author_S("J.K. Rowling"),
|
||||
Author_S("J.R.R. Tolkien"),
|
||||
Author_S("Leo Tolstoy"),
|
||||
Author_S("Fyodor Dostoevsky"),
|
||||
Author_S("Mark Twain"),
|
||||
Author_S("Charles Dickens"),
|
||||
Author_S("Virginia Woolf"),
|
||||
Author_S("Ernest Hemingway"),
|
||||
Author_S("Gabriel García Márquez"),
|
||||
Author_S("Franz Kafka"),
|
||||
Author_S("Harper Lee"),
|
||||
Author_S("William Shakespeare"),
|
||||
Author_S("Oscar Wilde"),
|
||||
Author_S("Aldous Huxley"),
|
||||
Author_S("Jane Austen"),
|
||||
Author_S("John Steinbeck"),
|
||||
Author_S("Agatha Christie"),
|
||||
Author_S("Isaac Asimov"),
|
||||
};
|
||||
|
||||
addBook(*db, "Очень странная книжка", { "Автор 1", "Автор 2", "Авторк 3" });
|
||||
|
||||
addBook(*db, "1984", { authors[0].fullName() });
|
||||
addBook(*db, "Harry Potter and the Philosopher's Stone", { authors[1].fullName() });
|
||||
addBook(*db, "The Hobbit", { authors[2].fullName() });
|
||||
addBook(*db, "War and Peace", { authors[3].fullName() });
|
||||
addBook(*db, "Crime and Punishment", { authors[4].fullName() });
|
||||
addBook(*db, "Adventures of Huckleberry Finn", { authors[5].fullName() });
|
||||
addBook(*db, "A Tale of Two Cities", { authors[6].fullName() });
|
||||
addBook(*db, "Mrs. Dalloway", { authors[7].fullName() });
|
||||
addBook(*db, "The Old Man and the Sea", { authors[8].fullName() });
|
||||
addBook(*db, "One Hundred Years of Solitude", { authors[9].fullName() });
|
||||
addBook(*db, "The Trial", { authors[10].fullName() });
|
||||
addBook(*db, "To Kill a Mockingbird", { authors[11].fullName() });
|
||||
addBook(*db, "Macbeth", { authors[12].fullName() });
|
||||
addBook(*db, "The Picture of Dorian Gray", { authors[13].fullName() });
|
||||
addBook(*db, "Brave New World", { authors[14].fullName() });
|
||||
addBook(*db, "Pride and Prejudice", { authors[15].fullName() });
|
||||
addBook(*db, "The Brothers Karamazov", { authors[4].fullName() });
|
||||
addBook(*db, "The Grapes of Wrath", { authors[16].fullName() });
|
||||
addBook(*db, "Murder on the Orient Express", { authors[17].fullName() });
|
||||
addBook(*db, "I, Robot", { authors[18].fullName() });
|
||||
addBook(*db, "Animal Farm", { authors[0].fullName() });
|
||||
addBook(*db, "Harry Potter and the Chamber of Secrets", { authors[1].fullName() });
|
||||
addBook(*db, "The Lord of the Rings: The Fellowship of the Ring", { authors[2].fullName() });
|
||||
addBook(*db, "Anna Karenina", { authors[3].fullName() });
|
||||
addBook(*db, "The Idiot", { authors[4].fullName() });
|
||||
addBook(*db, "The Adventures of Tom Sawyer", { authors[5].fullName() });
|
||||
addBook(*db, "Oliver Twist", { authors[6].fullName() });
|
||||
addBook(*db, "To the Lighthouse", { authors[7].fullName() });
|
||||
addBook(*db, "For Whom the Bell Tolls", { authors[8].fullName() });
|
||||
addBook(*db, "Love in the Time of Cholera", { authors[9].fullName() });
|
||||
addBook(*db, "The Metamorphosis", { authors[10].fullName() });
|
||||
addBook(*db, "Go Set a Watchman", { authors[11].fullName() });
|
||||
addBook(*db, "King Lear", { authors[12].fullName() });
|
||||
addBook(*db, "The Importance of Being Earnest", { authors[13].fullName() });
|
||||
addBook(*db, "Brave New World Revisited", { authors[14].fullName() });
|
||||
addBook(*db, "Emma", { authors[15].fullName() });
|
||||
addBook(*db, "The Double", { authors[4].fullName() });
|
||||
addBook(*db, "Of Mice and Men", { authors[16].fullName() });
|
||||
addBook(*db, "And Then There Were None", { authors[17].fullName() });
|
||||
addBook(*db, "The Foundation Trilogy", { authors[18].fullName() });
|
||||
addBook(*db, "Down and Out in Paris and London", { authors[0].fullName() });
|
||||
addBook(*db, "Harry Potter and the Prisoner of Azkaban", { authors[1].fullName() });
|
||||
addBook(*db, "The Lord of the Rings: The Two Towers", { authors[2].fullName() });
|
||||
addBook(*db, "War and Peace", { authors[3].fullName() });
|
||||
addBook(*db, "The Brothers Karamazov", { authors[4].fullName() });
|
||||
addBook(*db, "The Prince and the Pauper", { authors[5].fullName() });
|
||||
addBook(*db, "David Copperfield", { authors[6].fullName() });
|
||||
addBook(*db, "The Waves", { authors[7].fullName() });
|
||||
addBook(*db, "A Farewell to Arms", { authors[8].fullName() });
|
||||
addBook(*db, "Chronicle of a Death Foretold", { authors[9].fullName() });
|
||||
|
||||
addBook(*db, "1984", { "George Orwell" });
|
||||
addBook(*db, "Harry Potter and the Philosopher's Stone", { "J.K. Rowling" });
|
||||
|
||||
addBook(*db, "Очень странная книжка 2", { "Автор 4", "Авторк 3" });
|
||||
|
||||
std::cout << "Test data added successfully." << std::endl;
|
||||
}
|
||||
catch (const odb::exception& e)
|
||||
{
|
||||
std::cerr << "Error adding test data: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
Backend backend;
|
||||
backend.start();
|
||||
const std::string dbPath = "test_db.sqlite";
|
||||
uDBase db(openDB(dbPath));
|
||||
|
||||
// TODO Как-то нужно выполнять лишь раз
|
||||
// fillBooksBD(db);
|
||||
|
||||
RestApiServer server(*db);
|
||||
server.start(8080);
|
||||
|
||||
QString zipArzh = "/home/alex/repos/exp/cpp-opds/f.fb2-631519-634744.zip";
|
||||
|
||||
auto z = ZipWrapper(zipArzh);
|
||||
const auto books = z.work();
|
||||
|
||||
for (const auto& book : books)
|
||||
{
|
||||
try
|
||||
{
|
||||
addBook(*db, book.title, book.authors);
|
||||
}
|
||||
catch (const odb::exception& e)
|
||||
{
|
||||
std::cerr << "Error adding test data: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
// qWarning() << "S EXIT";
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
47
src/model/books/author_book_s.h
Normal file
47
src/model/books/author_book_s.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef AUTHOR_BOOK_H
|
||||
#define AUTHOR_BOOK_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#include "author_s.h"
|
||||
#include "book_s.h"
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT AuthorBook_S
|
||||
{
|
||||
private:
|
||||
AuthorBook_S() = default;
|
||||
|
||||
public:
|
||||
AuthorBook_S(SH<Author_S> author,
|
||||
SH<Book_S> book) :
|
||||
m_author(author),
|
||||
m_book(book) {}
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(quint64 newId) { m_id = newId; }
|
||||
|
||||
SH<Author_S> author() const { return m_author; }
|
||||
void setAuthor(const SH<Author_S>& newAuthor) { m_author = newAuthor; }
|
||||
|
||||
SH<Book_S> book() const { return m_book; }
|
||||
void setBook(const SH<Book_S>& newBook) { m_book = newBook; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Author_S> m_author;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Book_S> m_book;
|
||||
|
||||
#pragma db index("author_book_unique") member(m_author) member(m_book) unique
|
||||
};
|
||||
|
||||
#endif // AUTHOR_BOOK_H
|
||||
42
src/model/books/author_s.h
Normal file
42
src/model/books/author_s.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef AUTHOR_S_H
|
||||
#define AUTHOR_S_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Author_S
|
||||
{
|
||||
public:
|
||||
Author_S() = default;
|
||||
Author_S(const QString& fullName) :
|
||||
m_fullName(fullName) {}
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(quint64 newId) { m_id = newId; }
|
||||
|
||||
QString fullName() const { return m_fullName; }
|
||||
void setFullName(const QString& newFullName) { m_fullName = newFullName; }
|
||||
|
||||
QString langCode() const { return m_langCode; }
|
||||
void setLangCode(const QString& newLangCode) { m_langCode = newLangCode; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
QString m_fullName;
|
||||
QString m_langCode;
|
||||
};
|
||||
|
||||
#pragma db view object(Author_S)
|
||||
struct person_stat
|
||||
{
|
||||
#pragma db column("count(" + Author_S::m_id + ")")
|
||||
std::size_t count;
|
||||
};
|
||||
|
||||
#endif // AUTHOR_S_H
|
||||
43
src/model/books/book_s.h
Normal file
43
src/model/books/book_s.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef BOOK_S_H
|
||||
#define BOOK_S_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Book_S
|
||||
{
|
||||
public:
|
||||
Book_S() = default;
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(const quint64& newId) { m_id = newId; }
|
||||
|
||||
QString title() const { return m_title; }
|
||||
void setTitle(const QString& newTitle) { m_title = newTitle; }
|
||||
|
||||
quint8 year() const { return m_year; }
|
||||
void setYear(const quint8& newYear) { m_year = newYear; }
|
||||
|
||||
QDateTime lastModified() const { return m_lastModified; }
|
||||
void setLastModified(const QDateTime& newLastModified) { m_lastModified = newLastModified; }
|
||||
|
||||
QString lang() const { return m_lang; }
|
||||
void setLang(const QString& newLang) { m_lang = newLang; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
QString m_title;
|
||||
quint8 m_year;
|
||||
QDateTime m_lastModified;
|
||||
QString m_lang;
|
||||
};
|
||||
|
||||
#endif // BOOK_S_H
|
||||
46
src/model/books/genre_book_s.h
Normal file
46
src/model/books/genre_book_s.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef GENRE_BOOK_S_H
|
||||
#define GENRE_BOOK_S_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#include "book_s.h"
|
||||
#include "genre_s.h"
|
||||
|
||||
#pragma db object
|
||||
class GenreBook_S
|
||||
{
|
||||
private:
|
||||
GenreBook_S() = default;
|
||||
|
||||
public:
|
||||
GenreBook_S(const SH<Genre_S>& genreId, const SH<Book_S>& bookId) :
|
||||
m_genreId(genreId),
|
||||
m_bookId(bookId) {}
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(quint64 newId) { m_id = newId; }
|
||||
|
||||
SH<Genre_S> genreId() const { return m_genreId; }
|
||||
void setGenreId(const SH<Genre_S>& newGenreId) { m_genreId = newGenreId; }
|
||||
|
||||
SH<Book_S> bookId() const { return m_bookId; }
|
||||
void setBookId(const SH<Book_S>& newBookId) { m_bookId = newBookId; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Genre_S> m_genreId;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Book_S> m_bookId;
|
||||
|
||||
#pragma db index("genre_book_unique") member(m_genreId) member(m_bookId) unique
|
||||
};
|
||||
|
||||
#endif // GENRE_BOOK_S_H
|
||||
29
src/model/books/genre_s.h
Normal file
29
src/model/books/genre_s.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef GENRE_H
|
||||
#define GENRE_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Genre_S
|
||||
{
|
||||
public:
|
||||
Genre_S() = default;
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(const quint64& newId) { m_id = newId; }
|
||||
|
||||
QString name() const { return m_name; }
|
||||
void setName(const QString& newName) { m_name = newName; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
QString m_name;
|
||||
};
|
||||
|
||||
#endif // GENRE_H
|
||||
10
src/model/books/manutomanybase.h
Normal file
10
src/model/books/manutomanybase.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef MANUTOMANYBASE_H
|
||||
#define MANUTOMANYBASE_H
|
||||
|
||||
class ManuToManyBase
|
||||
{
|
||||
public:
|
||||
ManuToManyBase();
|
||||
};
|
||||
|
||||
#endif // MANUTOMANYBASE_H
|
||||
46
src/model/books/series_book_s.h
Normal file
46
src/model/books/series_book_s.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef SERIES_BOOK_S_H
|
||||
#define SERIES_BOOK_S_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#include "book_s.h"
|
||||
#include "series_s.h"
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT SeriesBook_S
|
||||
{
|
||||
private:
|
||||
SeriesBook_S() = default;
|
||||
|
||||
public:
|
||||
SeriesBook_S(const SH<Series_S>& serieId, const SH<Book_S>& bookId) :
|
||||
m_serieId(serieId),
|
||||
m_bookId(bookId) {}
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(quint64 newId) { m_id = newId; }
|
||||
|
||||
SH<Series_S> serieId() const { return m_serieId; }
|
||||
void setSerieId(const SH<Series_S>& newSerieId) { m_serieId = newSerieId; }
|
||||
|
||||
SH<Book_S> bookId() const { return m_bookId; }
|
||||
void setBookId(const SH<Book_S>& newBookId) { m_bookId = newBookId; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Series_S> m_serieId;
|
||||
|
||||
#pragma db not_null
|
||||
SH<Book_S> m_bookId;
|
||||
|
||||
#pragma db index("series_book_unique") member(m_serieId) member(m_bookId) unique
|
||||
};
|
||||
|
||||
#endif // SERIES_BOOK_S_H
|
||||
29
src/model/books/series_s.h
Normal file
29
src/model/books/series_s.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef SERIES_H
|
||||
#define SERIES_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Series_S
|
||||
{
|
||||
public:
|
||||
Series_S() = default;
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(const quint64& newId) { m_id = newId; }
|
||||
|
||||
QString name() const { return m_serName; }
|
||||
void setName(const QString& newSerName) { m_serName = newSerName; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
public:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
|
||||
QString m_serName;
|
||||
};
|
||||
|
||||
#endif // SERIES_H
|
||||
53
src/model/model.qbs
Normal file
53
src/model/model.qbs
Normal file
@@ -0,0 +1,53 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
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",
|
||||
"MODEL_LIBRARY"
|
||||
]
|
||||
consoleApplication: true
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "rdbase" }
|
||||
|
||||
Depends { name: "redkit_gen" }
|
||||
redkit_gen.includeModules: [redkit_gen.module.JsonSerializer,
|
||||
redkit_gen.module.SettingsSerializer,]
|
||||
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Group {
|
||||
id: headers
|
||||
name: "headers"
|
||||
files: [
|
||||
"model_global.h",
|
||||
]
|
||||
}
|
||||
|
||||
Group {
|
||||
id: odbs
|
||||
name: "odb"
|
||||
files: [
|
||||
"**/*.h",
|
||||
]
|
||||
excludeFiles: headers.files
|
||||
fileTags: ["hpp", "odbxx", "rgen"]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
}
|
||||
15
src/model/model_global.h
Normal file
15
src/model/model_global.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef MODEL_GLOBAL_H
|
||||
#define MODEL_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(MODEL_LIBRARY)
|
||||
#define MODEL_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define MODEL_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <rcore/smarttypes.h>
|
||||
|
||||
#endif // MODEL_GLOBAL_H
|
||||
23
src/model/settings/settings.h
Normal file
23
src/model/settings/settings.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef SETTINGS_S_H
|
||||
#define SETTINGS_S_H
|
||||
|
||||
#include <model/model_global.h>
|
||||
|
||||
#pragma db object
|
||||
class MODEL_EXPORT Settings_S
|
||||
{
|
||||
public:
|
||||
Settings_S() = default;
|
||||
|
||||
quint64 id() const { return m_id; }
|
||||
void setId(const quint64& newId) { m_id = newId; }
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
private:
|
||||
#pragma db id auto
|
||||
quint64 m_id;
|
||||
};
|
||||
|
||||
#endif // SETTINGS_S_H
|
||||
81
src/repository/authorrepository.cpp
Normal file
81
src/repository/authorrepository.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "authorrepository.h"
|
||||
|
||||
#include <model/books/author_book_s-odb.hxx>
|
||||
#include <model/books/author_book_s.h>
|
||||
#include <model/books/author_s-odb.hxx>
|
||||
#include <model/books/author_s.h>
|
||||
#include <model/books/book_s-odb.hxx>
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
namespace repository
|
||||
{
|
||||
|
||||
AuthorRepository::AuthorRepository(odb::core::database& db) :
|
||||
m_db(db)
|
||||
{}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findAll()
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Author_S> res(m_db.query<Author_S>());
|
||||
|
||||
QVector<Author_S> authors;
|
||||
|
||||
for (auto it = res.begin(); it != res.end(); ++it)
|
||||
{
|
||||
const auto& author = *it;
|
||||
authors.push_back(author);
|
||||
}
|
||||
|
||||
t.commit();
|
||||
|
||||
return authors;
|
||||
}
|
||||
|
||||
Author_S AuthorRepository::findById(int id)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Author_S> res(m_db.query<Author_S>(odb::query<Author_S>::id == id));
|
||||
|
||||
Author_S author;
|
||||
res.value(author);
|
||||
|
||||
t.commit();
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findByBook(const int& bookId)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
|
||||
QVector<Author_S> authors;
|
||||
|
||||
using ABQuery = odb::query<AuthorBook_S>;
|
||||
using ABResult = odb::result<AuthorBook_S>;
|
||||
ABResult abResult(m_db.query<AuthorBook_S>(ABQuery::book == bookId));
|
||||
|
||||
for (const AuthorBook_S& ab : abResult)
|
||||
{
|
||||
// Загружаем каждую книгу по ID
|
||||
SH<Author_S> author(m_db.load<Author_S>(ab.author()->id()));
|
||||
authors.push_back(*author.data());
|
||||
}
|
||||
t.commit();
|
||||
|
||||
return authors;
|
||||
}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findByBook(const QString& book_name)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
auto book_s = m_db.query<Book_S>(odb::query<Book_S>::title == book_name).one();
|
||||
|
||||
return findByBook(book_s->id());
|
||||
}
|
||||
|
||||
} // namespace repository
|
||||
41
src/repository/authorrepository.h
Normal file
41
src/repository/authorrepository.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef AUTHORREPOSITORY_H
|
||||
#define AUTHORREPOSITORY_H
|
||||
|
||||
#include "repository_global.h"
|
||||
|
||||
#include <model/books/author_s.h>
|
||||
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
namespace repository
|
||||
{
|
||||
|
||||
class REPOSITORY_EXPORT AuthorRepository
|
||||
{
|
||||
public:
|
||||
AuthorRepository(odb::core::database& db);
|
||||
|
||||
// Получить всех авторов
|
||||
QVector<Author_S> findAll();
|
||||
|
||||
// Найти автора по id
|
||||
Author_S findById(int id);
|
||||
|
||||
// Сохранить книгу (создать или обновить)
|
||||
// void save(const Book_S& book);
|
||||
|
||||
// Удалить автора
|
||||
// void remove(int id);
|
||||
|
||||
// Найти авторов по книге
|
||||
QVector<Author_S> findByBook(const int& book_id);
|
||||
QVector<Author_S> findByBook(const QString& bookName);
|
||||
|
||||
private:
|
||||
odb::core::database& m_db;
|
||||
};
|
||||
|
||||
} // namespace repository
|
||||
|
||||
#endif // AUTHORREPOSITORY_H
|
||||
96
src/repository/bookrepository.cpp
Normal file
96
src/repository/bookrepository.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "bookrepository.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include <model/books/author_book_s-odb.hxx>
|
||||
#include <model/books/author_book_s.h>
|
||||
#include <model/books/author_s-odb.hxx>
|
||||
#include <model/books/author_s.h>
|
||||
#include <model/books/book_s-odb.hxx>
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
namespace repository
|
||||
{
|
||||
|
||||
BookRepository::BookRepository(odb::core::database& db) :
|
||||
m_db(db)
|
||||
{}
|
||||
|
||||
QVector<Book_S> BookRepository::findAll()
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Book_S> res(m_db.query<Book_S>());
|
||||
|
||||
QVector<Book_S> books;
|
||||
|
||||
for (auto it = res.begin(); it != res.end(); ++it)
|
||||
{
|
||||
const auto& book = *it;
|
||||
books.push_back(book);
|
||||
}
|
||||
|
||||
t.commit();
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
Book_S BookRepository::findById(int id)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::id == id));
|
||||
|
||||
Book_S book;
|
||||
res.value(book);
|
||||
|
||||
t.commit();
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
// void BookRepository::save(const Book_S& book)
|
||||
// {
|
||||
// }
|
||||
|
||||
void BookRepository::remove(int id)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::id == id));
|
||||
res.one().reset();
|
||||
t.commit();
|
||||
}
|
||||
|
||||
QVector<Book_S> BookRepository::findByAuthor(const int& authorId)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
|
||||
QVector<Book_S> books;
|
||||
|
||||
using ABQuery = odb::query<AuthorBook_S>;
|
||||
using ABResult = odb::result<AuthorBook_S>;
|
||||
ABResult abResult(m_db.query<AuthorBook_S>(ABQuery::author == authorId));
|
||||
|
||||
for (const AuthorBook_S& ab : abResult)
|
||||
{
|
||||
// Загружаем каждую книгу по ID
|
||||
SH<Book_S> book(m_db.load<Book_S>(ab.book()->id()));
|
||||
books.push_back(*book.data());
|
||||
}
|
||||
|
||||
t.commit();
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
QVector<Book_S> BookRepository::findByAuthor(const QString& author)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
auto author_s = m_db.query<Author_S>(odb::query<Author_S>::fullName == author).one();
|
||||
|
||||
return findByAuthor(author_s->id());
|
||||
}
|
||||
|
||||
} // namespace repository
|
||||
41
src/repository/bookrepository.h
Normal file
41
src/repository/bookrepository.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BOOKREPOSITORY_H
|
||||
#define BOOKREPOSITORY_H
|
||||
|
||||
#include "repository_global.h"
|
||||
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
namespace repository
|
||||
{
|
||||
|
||||
class REPOSITORY_EXPORT BookRepository
|
||||
{
|
||||
public:
|
||||
BookRepository(odb::core::database& db);
|
||||
|
||||
// Получить все книги
|
||||
QVector<Book_S> findAll();
|
||||
|
||||
// Найти книгу по ID
|
||||
Book_S findById(int id);
|
||||
|
||||
// Сохранить книгу (создать или обновить)
|
||||
// void save(const Book_S& book);
|
||||
|
||||
// Удалить книгу
|
||||
void remove(int id);
|
||||
|
||||
// Найти книги по автору
|
||||
QVector<Book_S> findByAuthor(const QString& authorName);
|
||||
QVector<Book_S> findByAuthor(const int& authorId);
|
||||
|
||||
private:
|
||||
odb::core::database& m_db;
|
||||
};
|
||||
|
||||
} // namespace repository
|
||||
|
||||
#endif // BOOKREPOSITORY_H
|
||||
33
src/repository/repository.qbs
Normal file
33
src/repository/repository.qbs
Normal file
@@ -0,0 +1,33 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
name: "repository"
|
||||
cpp.defines: [
|
||||
"REPOSITORY_LIBRARY"
|
||||
]
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core" ] }
|
||||
Depends { name: "model" }
|
||||
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "database" }
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
}
|
||||
12
src/repository/repository_global.h
Normal file
12
src/repository/repository_global.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef REPOSITORY_GLOBAL_H
|
||||
#define REPOSITORY_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(REPOSITORY_LIBRARY)
|
||||
#define REPOSITORY_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define REPOSITORY_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // REPOSITORY_GLOBAL_H
|
||||
40
src/restapi/restapi.qbs
Normal file
40
src/restapi/restapi.qbs
Normal file
@@ -0,0 +1,40 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
WPSLibrary {
|
||||
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: "cpp" }
|
||||
Depends { name: "database" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "redkit_gen" }
|
||||
Depends { name: "rdbase" }
|
||||
Depends { name: "model" }
|
||||
Depends { name: "repository" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"restapiserver.*",
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
|
||||
} // Project
|
||||
182
src/restapi/restapiserver.cpp
Normal file
182
src/restapi/restapiserver.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
#include "restapiserver.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
#include <model/books/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/books/author_s.h>
|
||||
#include <model/books/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
#include <repository/authorrepository.h>
|
||||
#include <repository/bookrepository.h>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
#include <redkit-gen-modules/json_serializer.h>
|
||||
|
||||
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
||||
QTcpServer(parent), m_db(db) {}
|
||||
|
||||
void RestApiServer::start(quint16 port)
|
||||
{
|
||||
if (!this->listen(QHostAddress::Any, port))
|
||||
{
|
||||
qWarning() << "Ошибка запуска сервера!";
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "REST API сервер запущен по адресу:";
|
||||
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);
|
||||
|
||||
qWarning() << "Есть входящее подключение" << socket->socketDescriptor();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// qWarning() << request << "\n\n";
|
||||
|
||||
if (request.startsWith("GET /books/author/"))
|
||||
{
|
||||
repository::BookRepository b(m_db);
|
||||
|
||||
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||
qWarning() << author;
|
||||
auto books = b.findByAuthor(author);
|
||||
|
||||
QJsonArray jArray;
|
||||
for (const auto& book : books)
|
||||
{
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(book.id());
|
||||
j["title"] = book.title();
|
||||
|
||||
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 /books/id"))
|
||||
{
|
||||
repository::BookRepository b(m_db);
|
||||
|
||||
QString book_id = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||
qWarning() << book_id;
|
||||
|
||||
auto book = b.findById(book_id.toInt());
|
||||
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(book.id());
|
||||
QJsonArray authors;
|
||||
|
||||
repository::AuthorRepository a(m_db);
|
||||
|
||||
for (const auto& author : a.findByBook(book.id()))
|
||||
{
|
||||
QJsonObject jj;
|
||||
jj["id"] = QString::number(author.id());
|
||||
jj["fullName"] = author.fullName();
|
||||
authors.push_back(jj);
|
||||
}
|
||||
|
||||
j["authors"] = authors;
|
||||
j["title"] = book.title();
|
||||
|
||||
auto jDoc = QJsonDocument(j);
|
||||
|
||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
||||
}
|
||||
|
||||
if (request.startsWith("GET /books"))
|
||||
{
|
||||
repository::BookRepository b(m_db);
|
||||
|
||||
QJsonArray jArray;
|
||||
for (const auto& book : b.findAll())
|
||||
{
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(book.id());
|
||||
j["title"] = book.title();
|
||||
|
||||
QJsonArray authors;
|
||||
repository::AuthorRepository a(m_db);
|
||||
for (const auto& author : a.findByBook(book.id()))
|
||||
{
|
||||
QJsonObject jj;
|
||||
jj["id"] = QString::number(author.id());
|
||||
jj["fullName"] = author.fullName();
|
||||
authors.push_back(jj);
|
||||
}
|
||||
j["authors"] = authors;
|
||||
|
||||
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 /authors"))
|
||||
{
|
||||
repository::AuthorRepository a(m_db);
|
||||
auto res = a.findAll();
|
||||
|
||||
QJsonArray jArray;
|
||||
for (auto it = res.begin(); it != res.end(); ++it)
|
||||
{
|
||||
const auto& author = *it;
|
||||
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(author.id());
|
||||
j["fullName"] = author.fullName();
|
||||
|
||||
QJsonArray books;
|
||||
repository::BookRepository b(m_db);
|
||||
for (const auto& book : b.findByAuthor(author.id()))
|
||||
{
|
||||
QJsonObject jj;
|
||||
jj["id"] = QString::number(book.id());
|
||||
jj["title"] = book.title();
|
||||
books.push_back(jj);
|
||||
}
|
||||
j["books"] = books;
|
||||
|
||||
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";
|
||||
}
|
||||
29
src/restapi/restapiserver.h
Normal file
29
src/restapi/restapiserver.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef RESTAPISERVER_H
|
||||
#define RESTAPISERVER_H
|
||||
|
||||
#include <odb/database.hxx>
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
|
||||
class Q_DECL_EXPORT RestApiServer : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit RestApiServer(odb::core::database& db, QObject* parent = nullptr);
|
||||
void start(quint16 port = 8080);
|
||||
|
||||
protected:
|
||||
void incomingConnection(qintptr socketDescriptor) override;
|
||||
|
||||
private slots:
|
||||
void handleRequest();
|
||||
|
||||
private:
|
||||
QByteArray processRequest(const QString& request);
|
||||
|
||||
odb::core::database& m_db;
|
||||
};
|
||||
|
||||
#endif // RESTAPISERVER_H
|
||||
101
src/utils/fb2extractor.cpp
Normal file
101
src/utils/fb2extractor.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "fb2extractor.h"
|
||||
|
||||
// FB2Extractor::FB2Extractor()
|
||||
// {
|
||||
// }
|
||||
|
||||
FB2Extractor::FB2Extractor(QuaZipFile& file) :
|
||||
m_file(&file)
|
||||
{
|
||||
}
|
||||
|
||||
FB2Extractor::~FB2Extractor()
|
||||
{
|
||||
m_file.close();
|
||||
}
|
||||
|
||||
Fb2Metadata FB2Extractor::parse()
|
||||
{
|
||||
bool isOpen = m_file.open(QIODevice::ReadOnly);
|
||||
// int zipError = fb2File.getZipError();
|
||||
if (!isOpen)
|
||||
{
|
||||
qWarning() << "file in extractor is not open";
|
||||
return {};
|
||||
}
|
||||
|
||||
QXmlStreamReader xmlData(&m_file);
|
||||
|
||||
Fb2Metadata meta;
|
||||
QString currentElement;
|
||||
bool inTitleInfo = false;
|
||||
bool inAuthor = false;
|
||||
QString currentAuthor;
|
||||
|
||||
while (!xmlData.atEnd())
|
||||
{
|
||||
switch (xmlData.readNext())
|
||||
{
|
||||
case QXmlStreamReader::StartElement:
|
||||
currentElement = xmlData.name().toString();
|
||||
|
||||
if (currentElement == "title-info")
|
||||
{
|
||||
inTitleInfo = true;
|
||||
}
|
||||
else if (inTitleInfo)
|
||||
{
|
||||
if (currentElement == "book-title")
|
||||
{
|
||||
meta.title = xmlData.readElementText();
|
||||
}
|
||||
else if (currentElement == "genre")
|
||||
{
|
||||
meta.genres << xmlData.readElementText();
|
||||
}
|
||||
else if (currentElement == "author")
|
||||
{
|
||||
inAuthor = true;
|
||||
currentAuthor.clear();
|
||||
}
|
||||
else if (inAuthor && (currentElement == "first-name" || currentElement == "last-name" || currentElement == "middle-name"))
|
||||
{
|
||||
currentAuthor += xmlData.readElementText() + " ";
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QXmlStreamReader::EndElement:
|
||||
if (xmlData.name().toString() == "title-info")
|
||||
{
|
||||
inTitleInfo = false;
|
||||
}
|
||||
else if (xmlData.name().toString() == "author" && inAuthor)
|
||||
{
|
||||
meta.authors << currentAuthor.trimmed();
|
||||
inAuthor = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case QXmlStreamReader::Characters:
|
||||
// Обработка текста уже делается в readElementText()
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Прерываем парсинг, если нашли все метаданные
|
||||
if (!meta.title.isEmpty() && !meta.authors.isEmpty() && !meta.genres.isEmpty())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (xmlData.hasError())
|
||||
{
|
||||
qWarning() << "XML parsing error:" << xmlData.errorString();
|
||||
}
|
||||
|
||||
return meta;
|
||||
}
|
||||
36
src/utils/fb2extractor.h
Normal file
36
src/utils/fb2extractor.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef FB2EXTRACTOR_H
|
||||
#define FB2EXTRACTOR_H
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
#include <external_libs/quazip/quazip/quazipfile.h>
|
||||
|
||||
struct Fb2Metadata
|
||||
{
|
||||
QString title; // Название книги
|
||||
QVector<QString> authors; // Авторы
|
||||
QStringList genres; // Жанры
|
||||
};
|
||||
|
||||
Fb2Metadata UTILS_EXPORT parseFb2Metadata(QXmlStreamReader& sr);
|
||||
|
||||
class UTILS_EXPORT FB2Extractor
|
||||
{
|
||||
public:
|
||||
FB2Extractor(QuaZipFile& file);
|
||||
~FB2Extractor();
|
||||
|
||||
/*!
|
||||
* \brief Распарсить инфу из содержимого
|
||||
*/
|
||||
Fb2Metadata parse();
|
||||
|
||||
private:
|
||||
QuaZipFile m_file;
|
||||
};
|
||||
|
||||
#endif // FB2EXTRACTOR_H
|
||||
22
src/utils/utils.qbs
Normal file
22
src/utils/utils.qbs
Normal file
@@ -0,0 +1,22 @@
|
||||
/*!
|
||||
\qmltype cpp-opds
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
name: "utils"
|
||||
cpp.defines: [
|
||||
"UTILS_LIBRARY"
|
||||
]
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core" ] }
|
||||
Depends { name: "quazip" }
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"**/*.h",
|
||||
"**/*.cpp",
|
||||
]
|
||||
}
|
||||
}
|
||||
12
src/utils/utils_global.h
Normal file
12
src/utils/utils_global.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef UTILS_GLOBAL_H
|
||||
#define UTILS_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(UTILS_LIBRARY)
|
||||
#define UTILS_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define UTILS_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // UTILS_GLOBAL_H
|
||||
142
src/utils/zipwrapper.cpp
Normal file
142
src/utils/zipwrapper.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "zipwrapper.h"
|
||||
|
||||
#include "fb2extractor.h"
|
||||
|
||||
#include <external_libs/quazip/quazip/quazipfile.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
Fb2Metadata parse(QuaZipFile& m_file)
|
||||
{
|
||||
bool isOpen = m_file.open(QIODevice::ReadOnly);
|
||||
// int zipError = fb2File.getZipError();
|
||||
if (!isOpen)
|
||||
{
|
||||
qWarning() << "file in extractor is not open";
|
||||
return {};
|
||||
}
|
||||
|
||||
QXmlStreamReader xmlData(&m_file);
|
||||
|
||||
Fb2Metadata meta;
|
||||
QString currentElement;
|
||||
bool inTitleInfo = false;
|
||||
bool inAuthor = false;
|
||||
QString currentAuthor;
|
||||
|
||||
while (!xmlData.atEnd())
|
||||
{
|
||||
switch (xmlData.readNext())
|
||||
{
|
||||
case QXmlStreamReader::StartElement:
|
||||
currentElement = xmlData.name().toString();
|
||||
|
||||
if (currentElement == "title-info")
|
||||
{
|
||||
inTitleInfo = true;
|
||||
}
|
||||
else if (inTitleInfo)
|
||||
{
|
||||
if (currentElement == "book-title")
|
||||
{
|
||||
meta.title = xmlData.readElementText();
|
||||
}
|
||||
else if (currentElement == "genre")
|
||||
{
|
||||
meta.genres << xmlData.readElementText();
|
||||
}
|
||||
else if (currentElement == "author")
|
||||
{
|
||||
inAuthor = true;
|
||||
currentAuthor.clear();
|
||||
}
|
||||
else if (inAuthor && (currentElement == "first-name" || currentElement == "last-name" || currentElement == "middle-name"))
|
||||
{
|
||||
currentAuthor += xmlData.readElementText() + " ";
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QXmlStreamReader::EndElement:
|
||||
if (xmlData.name().toString() == "title-info")
|
||||
{
|
||||
inTitleInfo = false;
|
||||
}
|
||||
else if (xmlData.name().toString() == "author" && inAuthor)
|
||||
{
|
||||
meta.authors << currentAuthor.trimmed();
|
||||
inAuthor = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case QXmlStreamReader::Characters:
|
||||
// Обработка текста уже делается в readElementText()
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Прерываем парсинг, если нашли все метаданные
|
||||
if (!meta.title.isEmpty() && !meta.authors.isEmpty() && !meta.genres.isEmpty())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (xmlData.hasError())
|
||||
{
|
||||
qWarning() << "XML parsing error:" << xmlData.errorString();
|
||||
}
|
||||
|
||||
return meta;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ZipWrapper::ZipWrapper(const QString& zipFilePath) :
|
||||
m_zip(zipFilePath)
|
||||
{
|
||||
m_zip.open(QuaZip::mdUnzip);
|
||||
isOpen();
|
||||
}
|
||||
|
||||
ZipWrapper::~ZipWrapper()
|
||||
{
|
||||
// В любом случае закрываем архив
|
||||
m_zip.close();
|
||||
}
|
||||
|
||||
QVector<Fb2Metadata> ZipWrapper::work()
|
||||
{
|
||||
if (!isOpen())
|
||||
return {};
|
||||
|
||||
// int i = 0;
|
||||
QVector<Fb2Metadata> parsedBooks;
|
||||
for (bool more = m_zip.goToFirstFile(); more; more = m_zip.goToNextFile())
|
||||
{
|
||||
// ++i;
|
||||
if (m_zip.getCurrentFileName().endsWith(".fb2"))
|
||||
{
|
||||
QuaZipFile fb2File(&m_zip);
|
||||
|
||||
auto fb2Extr = parse(fb2File);
|
||||
// qWarning() << i << ":" << fb2Extr.title << fb2Extr.authors << fb2Extr.genres;
|
||||
parsedBooks.push_back(fb2Extr);
|
||||
}
|
||||
else
|
||||
qWarning() << "Неизвестный файл" << &m_zip;
|
||||
}
|
||||
|
||||
return parsedBooks;
|
||||
}
|
||||
|
||||
bool ZipWrapper::isOpen()
|
||||
{
|
||||
if (m_zip.isOpen())
|
||||
return true;
|
||||
|
||||
qWarning() << "Не удалось открыть архив:" << m_zip.getZipName();
|
||||
return false;
|
||||
}
|
||||
29
src/utils/zipwrapper.h
Normal file
29
src/utils/zipwrapper.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef ZIPWRAPPER_H
|
||||
#define ZIPWRAPPER_H
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
#include <external_libs/quazip/quazip/quazip.h>
|
||||
#include <external_libs/quazip/quazip/quazipfile.h> // TODO для совместимости. Временно
|
||||
|
||||
#include "fb2extractor.h"
|
||||
|
||||
class UTILS_EXPORT ZipWrapper
|
||||
{
|
||||
public:
|
||||
ZipWrapper(const QString& zipFilePath);
|
||||
~ZipWrapper();
|
||||
|
||||
QVector<Fb2Metadata> work();
|
||||
|
||||
private:
|
||||
bool isOpen();
|
||||
|
||||
QuaZip m_zip;
|
||||
};
|
||||
|
||||
#endif // ZIPWRAPPER_H
|
||||
1
test/requests/all_books.http
Normal file
1
test/requests/all_books.http
Normal file
@@ -0,0 +1 @@
|
||||
GET http://localhost:8080/books/
|
||||
1
test/requests/autor_books.http
Normal file
1
test/requests/autor_books.http
Normal file
@@ -0,0 +1 @@
|
||||
GET http://localhost:8080/books/author/George%20Orwell
|
||||
Reference in New Issue
Block a user