From 96bdf1e5e8dcf814b518dce80caa1fc1255d1170 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 5 Aug 2025 00:06:02 +0500 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D0=B2?= =?UTF-8?q?=20=D0=91=D0=94=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/database.qbs | 3 + src/database/database_utils.cpp | 98 +++++++++-------- src/database/database_utils.h | 15 +-- src/main.cpp | 179 +++++++++++++------------------- src/model/author_book_s.h | 46 ++++++++ src/model/author_s.h | 6 +- src/model/book_s.h | 7 +- src/model/model.qbs | 3 +- src/restapi/restapiserver.cpp | 62 +++++------ 9 files changed, 217 insertions(+), 202 deletions(-) create mode 100644 src/model/author_book_s.h diff --git a/src/database/database.qbs b/src/database/database.qbs index c1e4a3e..48a25ac 100644 --- a/src/database/database.qbs +++ b/src/database/database.qbs @@ -18,6 +18,9 @@ PSLibrary { Depends { name: "odb.gen" } Depends { name: "rdbase" } + Depends { name: "model" } + + Depends { name: "redkit_gen" } odb.gen.databases: "sqlite" cpp.cxxLanguageVersion: "c++17" diff --git a/src/database/database_utils.cpp b/src/database/database_utils.cpp index 0081295..305a107 100644 --- a/src/database/database_utils.cpp +++ b/src/database/database_utils.cpp @@ -1,57 +1,55 @@ #include "database_utils.h" -#include - -namespace +void addBook(oDBase& db, const QString title, const QVector& authors) { - -// Функция для генерации случайной строки (имени или фамилии) -QString generate_random_string(const QVector& pool) -{ - static std::random_device rd; - static std::mt19937 gen(rd()); - std::uniform_int_distribution<> dis(0, pool.size() - 1); - return pool[dis(gen)]; -} - -// Функция для генерации случайного года -int generate_random_year(int min_year = 1900, int max_year = 2020) -{ - static std::random_device rd; - static std::mt19937 gen(rd()); - std::uniform_int_distribution<> dis(min_year, max_year); - return dis(gen); -} - -} // namespace - -QVector fillAuthorDB() -{ - QVector first_names = { - "John", "Jane", "Alex", "Chris", "Robert", "Emily", "James", "Linda", "David", "Sarah", - "Michael", "Elizabeth", "Daniel", "Samantha", "William", "Olivia", "Ethan", "Sophia", "Joshua", "Charlotte", - "Daniel", "Grace", "Benjamin", "Isabella", "Matthew", "Victoria", "Henry", "Abigail", "Samuel", "Megan", - "Lucas", "Lily", "Andrew", "Madison", "Jackson", "Chloe", "Aiden", "Amelia", "Thomas", "Natalie", - "Ryan", "Zoe", "Jack", "Harper", "Elijah", "Ava", "Isaac", "Mia", "Caleb", "Ella" - }; - QVector last_names = { - "Doe", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", - "Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez", - "Roberts", "Clark", "Lewis", "Walker", "Young", "Allen", "King", "Wright", "Scott", "Adams", - "Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Robinson", "Hughes", "Flores", "Cook", - "Rogers", "Gutierrez", "Ramirez", "Diaz", "Perez", "Ross", "Sanders", "Price", "Howard", "Cooper" - }; - - QVector vecTest; - - for (int i = 0; i < 50; ++i) + odb::transaction t(db.begin()); + try { - QString first_name = generate_random_string(first_names); - QString last_name = generate_random_string(last_names); - int birth_year = generate_random_year(1900, 2000); + // 1. Создаем/получаем книгу + auto books = db.query(odb::query::title == title); + SH book; - vecTest.push_back({ first_name, last_name, birth_year }); + if (books.empty()) + { + book = SH::create(); + book->setTitle(title); + + db.persist(book); + } + else + { + book = books.begin().load(); // Берем первую найденную книгу + } + + // 2. Обрабатываем авторов + for (const auto& name : authors) + { + auto authors = db.query(odb::query::fullName == name); + SH author = authors.empty() ? SH::create() : authors.begin().load(); + + if (authors.empty()) + { + author->setFullName(name); + db.persist(author); + } + + // Проверяем и создаем связь + + auto links = db.query( + odb::query::author->id == author->id() + && odb::query::book->id == book->id()); + + if (links.empty()) + { + db.persist(SH::create(author, book)); + } + } + + t.commit(); + } + catch (...) + { + t.rollback(); + throw; } - - return vecTest; } diff --git a/src/database/database_utils.h b/src/database/database_utils.h index a4f227a..67bcb6e 100644 --- a/src/database/database_utils.h +++ b/src/database/database_utils.h @@ -1,3 +1,4 @@ +#include #include #include @@ -27,11 +28,11 @@ using oDBase = odb::sqlite::database; using uDBase = U; -struct testData -{ - QString first; - QString last; - int age; -}; +#include +#include +#include +#include +#include +#include -QVector DATABASE_EXPORT fillAuthorDB(); +void DATABASE_EXPORT addBook(oDBase& db, const QString title, const QVector& authors); diff --git a/src/main.cpp b/src/main.cpp index b4a639e..769cba3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,11 +8,8 @@ #include /* Опыты с odb */ -#include // create_database -#include // Должен быть здесь -#include -#include // Должен быть здесь -#include +#include // create_database +#include #include #include @@ -21,97 +18,81 @@ void fillBooksBD(uDBase& db) { try { - QVector> authors = { - SH::create("George Orwell", "en"), - SH::create("J.K. Rowling", "en"), - SH::create("J.R.R. Tolkien", "en"), - SH::create("Leo Tolstoy", "ru"), - SH::create("Fyodor Dostoevsky", "ru"), - SH::create("Mark Twain", "en"), - SH::create("Charles Dickens", "en"), - SH::create("Virginia Woolf", "en"), - SH::create("Ernest Hemingway", "en"), - SH::create("Gabriel García Márquez", "en"), - SH::create("Franz Kafka", "de"), - SH::create("Harper Lee", "en"), - SH::create("William Shakespeare", "en"), - SH::create("Oscar Wilde", "en"), - SH::create("Aldous Huxley", "en"), - SH::create("Jane Austen", "en"), - SH::create("John Steinbeck", "en"), - SH::create("Agatha Christie", "en"), - SH::create("Isaac Asimov", "ru"), + QVector authors = { + Author_S("George Orwell", "en"), + Author_S("J.K. Rowling", "en"), + Author_S("J.R.R. Tolkien", "en"), + Author_S("Leo Tolstoy", "ru"), + Author_S("Fyodor Dostoevsky", "ru"), + Author_S("Mark Twain", "en"), + Author_S("Charles Dickens", "en"), + Author_S("Virginia Woolf", "en"), + Author_S("Ernest Hemingway", "en"), + Author_S("Gabriel García Márquez", "en"), + Author_S("Franz Kafka", "de"), + Author_S("Harper Lee", "en"), + Author_S("William Shakespeare", "en"), + Author_S("Oscar Wilde", "en"), + Author_S("Aldous Huxley", "en"), + Author_S("Jane Austen", "en"), + Author_S("John Steinbeck", "en"), + Author_S("Agatha Christie", "en"), + Author_S("Isaac Asimov", "ru"), }; - // Список книг - QVector books = { - Book_S({ .author = authors[0], .title = "1984" }), - Book_S({ .author = authors[1], .title = "Harry Potter and the Philosopher's Stone" }), - Book_S({ .author = authors[2], .title = "The Hobbit" }), - Book_S({ .author = authors[3], .title = "War and Peace" }), - Book_S({ .author = authors[4], .title = "Crime and Punishment" }), - Book_S({ .author = authors[5], .title = "Adventures of Huckleberry Finn" }), - Book_S({ .author = authors[6], .title = "A Tale of Two Cities" }), - Book_S({ .author = authors[7], .title = "Mrs. Dalloway" }), - Book_S({ .author = authors[8], .title = "The Old Man and the Sea" }), - Book_S({ .author = authors[9], .title = "One Hundred Years of Solitude" }), - Book_S({ .author = authors[10], .title = "The Trial" }), - Book_S({ .author = authors[11], .title = "To Kill a Mockingbird" }), - Book_S({ .author = authors[12], .title = "Macbeth" }), - Book_S({ .author = authors[13], .title = "The Picture of Dorian Gray" }), - Book_S({ .author = authors[14], .title = "Brave New World" }), - Book_S({ .author = authors[15], .title = "Pride and Prejudice" }), - Book_S({ .author = authors[4], .title = "The Brothers Karamazov" }), - Book_S({ .author = authors[16], .title = "The Grapes of Wrath" }), - Book_S({ .author = authors[17], .title = "Murder on the Orient Express" }), - Book_S({ .author = authors[18], .title = "I, Robot" }), - Book_S({ .author = authors[0], .title = "Animal Farm" }), - Book_S({ .author = authors[1], .title = "Harry Potter and the Chamber of Secrets" }), - Book_S({ .author = authors[2], .title = "The Lord of the Rings: The Fellowship of the Ring" }), - Book_S({ .author = authors[3], .title = "Anna Karenina" }), - Book_S({ .author = authors[4], .title = "The Idiot" }), - Book_S({ .author = authors[5], .title = "The Adventures of Tom Sawyer" }), - Book_S({ .author = authors[6], .title = "Oliver Twist" }), - Book_S({ .author = authors[7], .title = "To the Lighthouse" }), - Book_S({ .author = authors[8], .title = "For Whom the Bell Tolls" }), - Book_S({ .author = authors[9], .title = "Love in the Time of Cholera" }), - Book_S({ .author = authors[10], .title = "The Metamorphosis" }), - Book_S({ .author = authors[11], .title = "Go Set a Watchman" }), - Book_S({ .author = authors[12], .title = "King Lear" }), - Book_S({ .author = authors[13], .title = "The Importance of Being Earnest" }), - Book_S({ .author = authors[14], .title = "Brave New World Revisited" }), - Book_S({ .author = authors[15], .title = "Emma" }), - Book_S({ .author = authors[4], .title = "The Double" }), - Book_S({ .author = authors[16], .title = "Of Mice and Men" }), - Book_S({ .author = authors[17], .title = "And Then There Were None" }), - Book_S({ .author = authors[18], .title = "The Foundation Trilogy" }), - Book_S({ .author = authors[0], .title = "Down and Out in Paris and London" }), - Book_S({ .author = authors[1], .title = "Harry Potter and the Prisoner of Azkaban" }), - Book_S({ .author = authors[2], .title = "The Lord of the Rings: The Two Towers" }), - Book_S({ .author = authors[3], .title = "War and Peace" }), - Book_S({ .author = authors[4], .title = "The Brothers Karamazov" }), - Book_S({ .author = authors[5], .title = "The Prince and the Pauper" }), - Book_S({ .author = authors[6], .title = "David Copperfield" }), - Book_S({ .author = authors[7], .title = "The Waves" }), - Book_S({ .author = authors[8], .title = "A Farewell to Arms" }), - Book_S({ .author = authors[9], .title = "Chronicle of a Death Foretold" }), - }; + 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() }); - // Сохранение авторов в базу данных - { - odb::core::transaction t(db->begin()); - for (auto& author : authors) - db->persist(author); - t.commit(); - } - - // Сохранение книг в базу данных - { - odb::core::transaction t(db->begin()); - for (auto& book : books) - db->persist(book); - t.commit(); - } + addBook(*db, "1984", { "George Orwell" }); + addBook(*db, "Harry Potter and the Philosopher's Stone", { "J.K. Rowling" }); std::cout << "Test data added successfully." << std::endl; } @@ -131,18 +112,6 @@ int main(int argc, char* argv[]) // TODO Как-то нужно выполнять лишь раз fillBooksBD(db); - // Сохранение дополнителньых авторов в базу данных - // { - // auto authors = fillAuthorDB(); - // odb::core::transaction t(db->begin()); - // for (auto& author : authors) - // { - // SH tempAuthor = SH::create(author., author.last, author.age); - // db->persist(tempAuthor); - // } - // t.commit(); - // } - RestApiServer server(*db); server.start(8080); diff --git a/src/model/author_book_s.h b/src/model/author_book_s.h new file mode 100644 index 0000000..32a90ae --- /dev/null +++ b/src/model/author_book_s.h @@ -0,0 +1,46 @@ +#ifndef AUTHOR_BOOK_H +#define AUTHOR_BOOK_H + +#include "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, + SH book) : + m_author(author), + m_book(book) {} + + quint64 id() const { return m_id; } + void setId(quint64 newId) { m_id = newId; } + + SH author() const { return m_author; } + void setAuthor(const SH& newAuthor) { m_author = newAuthor; } + + SH book() const { return m_book; } + void setBook(const SH& newBook) { m_book = newBook; } + +private: + friend class odb::access; + +private: +#pragma db id auto + quint64 m_id; + +#pragma db not_null + SH m_author; + +#pragma db not_null + SH m_book; + +#pragma db index("author_book_unique") member(m_author) member(m_book) unique +}; + +#endif // AUTHOR_BOOK_H diff --git a/src/model/author_s.h b/src/model/author_s.h index 4113b11..bef4022 100644 --- a/src/model/author_s.h +++ b/src/model/author_s.h @@ -3,12 +3,14 @@ #include "model_global.h" +// @JsonSerializer +class Author_S; + #pragma db object class MODEL_EXPORT Author_S { public: Author_S() = default; - Author_S(const QString& fullName, const QString& langCode) : m_fullName(fullName), @@ -27,7 +29,7 @@ public: private: friend class odb::access; -private: +public: // for Redkit-gen #pragma db id auto quint64 m_id; diff --git a/src/model/book_s.h b/src/model/book_s.h index 659cb35..3a09af6 100644 --- a/src/model/book_s.h +++ b/src/model/book_s.h @@ -11,7 +11,6 @@ struct Book_SZ { - SH author; QString title; SH series; quint8 year; @@ -27,7 +26,6 @@ public: Book_S() = default; Book_S(const Book_SZ& book) { - m_author = book.author; m_title = book.title; m_series = book.series; m_year = book.year; @@ -39,9 +37,6 @@ public: quint64 id() const { return m_id; } void setId(const quint64& newId) { m_id = newId; } - SH author() const { return m_author; } - void setAuthor(const SH& newAuthor) { m_author = newAuthor; } - QString title() const { return m_title; } void setTitle(const QString& newTitle) { m_title = newTitle; } @@ -63,11 +58,11 @@ public: private: friend class odb::access; +// public: // for Redkit-gen private: #pragma db id auto quint64 m_id; - SH m_author; QString m_title; SH m_series; quint8 m_year; diff --git a/src/model/model.qbs b/src/model/model.qbs index fbd8c8d..9e0d2ab 100644 --- a/src/model/model.qbs +++ b/src/model/model.qbs @@ -20,7 +20,8 @@ PSLibrary { Depends { name: "rdbase" } Depends { name: "redkit_gen" } - redkit_gen.includeModules: ["JsonSerializer"] + redkit_gen.includeModules: [redkit_gen.module.JsonSerializer, + redkit_gen.module.SettingsSerializer,] odb.gen.databases: "sqlite" cpp.cxxLanguageVersion: "c++17" diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 4298e65..59a11bc 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -64,7 +64,7 @@ QByteArray RestApiServer::processRequest(const QString& request) if (request.startsWith("GET /books/author/")) { QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); - quint64 ageReq = author.toInt(); + // quint64 ageReq = author.toInt(); QStringList nameParts = author.split(' '); QString firstName = nameParts.size() > 0 ? nameParts[0] : ""; @@ -72,27 +72,27 @@ QByteArray RestApiServer::processRequest(const QString& request) odb::transaction t(m_db.begin()); - auto books = m_db.query(odb::query::author->id == ageReq); + // auto books = m_db.query(odb::query::author->id == ageReq); QJsonArray jArray; - for (const auto& book : books) - { - QJsonObject j; - j["id"] = QString::number(book.id()); + // for (const auto& book : books) + // { + // QJsonObject j; + // j["id"] = QString::number(book.id()); - QJsonObject author; - author["id"] = QString::number(book.author()->id()); - author["fullName"] = book.author()->fullName(); - author["langCode"] = book.author()->langCode(); - j["author"] = author; - // j["series"] = book.series()->serName(); - // j["genre"] = book.genre()->name(); - j["title"] = book.title(); - j["year"] = book.year(); - j["lastModified"] = book.lastModified().toString(); - j["lang"] = book.lang(); - jArray.push_back(j); - } + // QJsonObject author; + // author["id"] = QString::number(book.author()->id()); + // author["fullName"] = book.author()->fullName(); + // author["langCode"] = book.author()->langCode(); + // j["author"] = author; + // // j["series"] = book.series()->serName(); + // // j["genre"] = book.genre()->name(); + // j["title"] = book.title(); + // j["year"] = book.year(); + // j["lastModified"] = book.lastModified().toString(); + // j["lang"] = book.lang(); + // jArray.push_back(j); + // } auto jDoc = QJsonDocument(jArray); t.commit(); @@ -113,18 +113,18 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(book.id()); - QJsonObject author; - author["id"] = QString::number(book.author()->id()); - author["fullName"] = book.author()->fullName(); - author["langCode"] = book.author()->langCode(); - j["author"] = author; - // j["series"] = book.series()->serName(); - // j["genre"] = book.genre()->name(); - j["title"] = book.title(); - j["year"] = book.year(); - j["lastModified"] = book.lastModified().toString(); - j["lang"] = book.lang(); - jArray.push_back(j); + // QJsonObject author; + // author["id"] = QString::number(book.author()->id()); + // author["fullName"] = book.author()->fullName(); + // author["langCode"] = book.author()->langCode(); + // j["author"] = author; + // // j["series"] = book.series()->serName(); + // // j["genre"] = book.genre()->name(); + // j["title"] = book.title(); + // j["year"] = book.year(); + // j["lastModified"] = book.lastModified().toString(); + // j["lang"] = book.lang(); + // jArray.push_back(j); } auto jDoc = QJsonDocument(jArray); t.commit();