diff --git a/src/main.cpp b/src/main.cpp index 247ddbd..b4a639e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,11 +8,11 @@ #include /* Опыты с odb */ +#include // create_database #include // Должен быть здесь #include #include // Должен быть здесь #include -#include // create_database #include #include @@ -22,79 +22,79 @@ void fillBooksBD(uDBase& db) try { QVector> authors = { - SH::create("George", "Orwell", 142), - SH::create("J.K.", "Rowling", 56), - SH::create("J.R.R.", "Tolkien", 81), - SH::create("Leo", "Tolstoy", 189), - SH::create("Fyodor", "Dostoevsky", 174), - SH::create("Mark", "Twain", 183), - SH::create("Charles", "Dickens", 208), - SH::create("Virginia", "Woolf", 134), - SH::create("Ernest", "Hemingway", 122), - SH::create("Gabriel", "García Márquez", 98), - SH::create("Franz", "Kafka", 100), - SH::create("Harper", "Lee", 64), - SH::create("William", "Shakespeare", 459), - SH::create("Oscar", "Wilde", 155), - SH::create("Aldous", "Huxley", 123), - SH::create("Jane", "Austen", 210), - SH::create("John", "Steinbeck", 116), - SH::create("Agatha", "Christie", 124), - SH::create("Isaac", "Asimov", 105) + 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 books = { - Book_S("1984", authors[0], 1949), - Book_S("Harry Potter and the Philosopher's Stone", authors[1], 1997), - Book_S("The Hobbit", authors[2], 1937), - Book_S("War and Peace", authors[3], 1869), - Book_S("Crime and Punishment", authors[4], 1866), - Book_S("Adventures of Huckleberry Finn", authors[5], 1884), - Book_S("A Tale of Two Cities", authors[6], 1859), - Book_S("Mrs. Dalloway", authors[7], 1925), - Book_S("The Old Man and the Sea", authors[8], 1952), - Book_S("One Hundred Years of Solitude", authors[9], 1967), - Book_S("The Trial", authors[10], 1925), - Book_S("To Kill a Mockingbird", authors[11], 1960), - Book_S("Macbeth", authors[12], 1606), - Book_S("The Picture of Dorian Gray", authors[13], 1890), - Book_S("Brave New World", authors[14], 1932), - Book_S("Pride and Prejudice", authors[15], 1813), - Book_S("The Brothers Karamazov", authors[4], 1880), - Book_S("The Grapes of Wrath", authors[16], 1939), - Book_S("Murder on the Orient Express", authors[17], 1934), - Book_S("I, Robot", authors[18], 1950), - Book_S("Animal Farm", authors[0], 1945), - Book_S("Harry Potter and the Chamber of Secrets", authors[1], 1998), - Book_S("The Lord of the Rings: The Fellowship of the Ring", authors[2], 1954), - Book_S("Anna Karenina", authors[3], 1878), - Book_S("The Idiot", authors[4], 1869), - Book_S("The Adventures of Tom Sawyer", authors[5], 1876), - Book_S("Oliver Twist", authors[6], 1837), - Book_S("To the Lighthouse", authors[7], 1927), - Book_S("For Whom the Bell Tolls", authors[8], 1940), - Book_S("Love in the Time of Cholera", authors[9], 1985), - Book_S("The Metamorphosis", authors[10], 1915), - Book_S("Go Set a Watchman", authors[11], 2015), - Book_S("King Lear", authors[12], 1605), - Book_S("The Importance of Being Earnest", authors[13], 1895), - Book_S("Brave New World Revisited", authors[14], 1958), - Book_S("Emma", authors[15], 1815), - Book_S("The Double", authors[4], 1846), - Book_S("Of Mice and Men", authors[16], 1937), - Book_S("And Then There Were None", authors[17], 1939), - Book_S("The Foundation Trilogy", authors[18], 1951), - Book_S("Down and Out in Paris and London", authors[0], 1933), - Book_S("Harry Potter and the Prisoner of Azkaban", authors[1], 1999), - Book_S("The Lord of the Rings: The Two Towers", authors[2], 1954), - Book_S("War and Peace", authors[3], 1869), - Book_S("The Brothers Karamazov", authors[4], 1880), - Book_S("The Prince and the Pauper", authors[5], 1881), - Book_S("David Copperfield", authors[6], 1850), - Book_S("The Waves", authors[7], 1931), - Book_S("A Farewell to Arms", authors[8], 1929), - Book_S("Chronicle of a Death Foretold", authors[9], 1981) + 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" }), }; // Сохранение авторов в базу данных @@ -132,16 +132,16 @@ int main(int argc, char* argv[]) fillBooksBD(db); // Сохранение дополнителньых авторов в базу данных - { - auto authors = fillAuthorDB(); - odb::core::transaction t(db->begin()); - for (auto& author : authors) - { - SH tempAuthor = SH::create(author.first, author.last, author.age); - db->persist(tempAuthor); - } - t.commit(); - } + // { + // 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_s.h b/src/model/author_s.h index bb9ea34..4113b11 100644 --- a/src/model/author_s.h +++ b/src/model/author_s.h @@ -1,40 +1,28 @@ -// file : hello/person.hxx -// copyright : not copyrighted - public domain - #ifndef AUTHOR_S_H #define AUTHOR_S_H #include "model_global.h" -#include - -#include -#include -#include - #pragma db object class MODEL_EXPORT Author_S { public: Author_S() = default; - Author_S(const QString& firstN, - const QString& lastN, - const unsigned short age) : - m_firstName(firstN), - m_lastName(lastN), - m_age(age) - { - } + Author_S(const QString& fullName, + const QString& langCode) : + m_fullName(fullName), + m_langCode(langCode) + {} - unsigned long long id() const { return m_id; } - QString firstName() const { return m_firstName; } - QString lastName() const { return m_lastName; } - unsigned short age() const { return m_age; } + quint64 id() const { return m_id; } + void setId(quint64 newId) { m_id = newId; } - QString full_name() const { return m_firstName + " " + m_lastName; } + QString fullName() const { return m_fullName; } + void setFullName(const QString& newFullName) { m_fullName = newFullName; } - void age(unsigned short age) { m_age = age; } + QString langCode() const { return m_langCode; } + void setLangCode(const QString& newLangCode) { m_langCode = newLangCode; } private: friend class odb::access; @@ -43,9 +31,8 @@ private: #pragma db id auto quint64 m_id; - QString m_firstName; - QString m_lastName; - quint64 m_age; + QString m_fullName; + QString m_langCode; }; #pragma db view object(Author_S) @@ -53,12 +40,6 @@ struct person_stat { #pragma db column("count(" + Author_S::m_id + ")") std::size_t count; - -#pragma db column("min(" + Author_S::m_age + ")") - unsigned short min_age; - -#pragma db column("max(" + Author_S::m_age + ")") - unsigned short max_age; }; #endif // AUTHOR_S_H diff --git a/src/model/book_s.h b/src/model/book_s.h index 8a82ded..659cb35 100644 --- a/src/model/book_s.h +++ b/src/model/book_s.h @@ -1,37 +1,64 @@ -// file : hello/person.hxx -// copyright : not copyrighted - public domain - #ifndef BOOK_S_H #define BOOK_S_H #include "model_global.h" -#include -#include -#include -#include +#include #include "author_s.h" +#include "genre_s.h" +#include "series_s.h" + +struct Book_SZ +{ + SH author; + QString title; + SH series; + quint8 year; + SH genre; + QDateTime lastModified = QDateTime::currentDateTime(); + QString lang; +}; #pragma db object class MODEL_EXPORT Book_S { public: Book_S() = default; - - Book_S(const QString& name, - const SH& author, - const int year) + Book_S(const Book_SZ& book) { - m_author = author; - m_name = name; - m_year = year; - }; + m_author = book.author; + m_title = book.title; + m_series = book.series; + m_year = book.year; + m_genre = book.genre; + m_lastModified = book.lastModified; + m_lang = book.lang; + } + + quint64 id() const { return m_id; } + void setId(const quint64& newId) { m_id = newId; } - unsigned long long id() const { return m_id; } SH author() const { return m_author; } - QString name() const { return m_name; } - int year() const { return m_year; } + void setAuthor(const SH& newAuthor) { m_author = newAuthor; } + + QString title() const { return m_title; } + void setTitle(const QString& newTitle) { m_title = newTitle; } + + SH series() const { return m_series; } + void setSeries(const SH& newSeries) { m_series = newSeries; } + + quint8 year() const { return m_year; } + void setYear(const quint8& newYear) { m_year = newYear; } + + SH genre() const { return m_genre; } + void setGenre(const SH& newGenre) { m_genre = newGenre; } + + 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; @@ -41,8 +68,12 @@ private: quint64 m_id; SH m_author; - QString m_name; + QString m_title; + SH m_series; quint8 m_year; + SH m_genre; + QDateTime m_lastModified; + QString m_lang; }; // #pragma db view object(Book_S) object(Author_S = author:Book_S::m_author) diff --git a/src/model/genre_s.h b/src/model/genre_s.h new file mode 100644 index 0000000..a577c15 --- /dev/null +++ b/src/model/genre_s.h @@ -0,0 +1,28 @@ +#ifndef GENRE_H +#define GENRE_H + +#include "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: +#pragma db id auto + quint64 m_id; + + QString m_name; +}; + +#endif // GENRE_H diff --git a/src/model/model_global.h b/src/model/model_global.h index c01f3b5..ee08f08 100644 --- a/src/model/model_global.h +++ b/src/model/model_global.h @@ -9,4 +9,7 @@ #define MODEL_EXPORT Q_DECL_IMPORT #endif +#include +#include + #endif // MODEL_GLOBAL_H diff --git a/src/model/series_s.h b/src/model/series_s.h new file mode 100644 index 0000000..59ac95f --- /dev/null +++ b/src/model/series_s.h @@ -0,0 +1,28 @@ +#ifndef SERIES_H +#define SERIES_H + +#include "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 serName() const { return m_serName; } + void setSerName(const QString& newSerName) { m_serName = newSerName; } + +private: + friend class odb::access; + +private: +#pragma db id auto + quint64 m_id; + + QString m_serName; +}; + +#endif // SERIES_H diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index b9820e2..4298e65 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -79,9 +79,18 @@ QByteArray RestApiServer::processRequest(const QString& request) { QJsonObject j; j["id"] = QString::number(book.id()); - j["name"] = book.name(); - j["author"] = book.author()->full_name(); + + 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); @@ -103,9 +112,18 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(book.id()); - j["title"] = book.name(); - j["author"] = book.author()->full_name(); + + 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); @@ -126,9 +144,8 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(author.id()); - j["first"] = author.firstName(); - j["last"] = author.lastName(); - j["age"] = author.age(); + j["fullName"] = author.fullName(); + j["langCode"] = author.langCode(); jArray.push_back(j); } auto jDoc = QJsonDocument(jArray);