From 74b33f455cb8afda4ef6c2983db73cfd44fee841 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 13 Feb 2024 03:44:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B0=20=D0=B2=20=D0=91=D0=94=20=D1=81=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/database.db | Bin 73728 -> 81920 bytes data/sample.test | 8 ++-- source/app.d | 109 +++++++++++++++++++++++++++++++++++++---------- source/sql.d | 51 ++++++++++++++++++++++ 4 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 source/sql.d diff --git a/data/database.db b/data/database.db index 91376c3eb015dcef5f374f12bcb7dc76f9ebc2d4..beaf1ba985693cd2bbe5dd64a6fed3ee73a7bff1 100644 GIT binary patch delta 1832 zcmbVMU2GIp6uvWeW@ql~&ONh>O|!1bG$E#mmdxx{tW8KY!54o?z(-7wG~uVT$`*_v zW_MeVm>No3Db(fCkyTdw(-#WF5F>ho9Pn+52tJuH+CBq+Fp0speh%kZZFuO-J-dn=hf+)9LgQIc+_igXXrSu( zHQ(?Y4yPIX4cFl`l1#z=7r4PHS#?s?R71^0$JC;lj>i3{f<`48QFV1eO>gv9IG|S< zU5?;%9o^>CyqdOk&f2#>zioTo(l~3Qo`a!qxXIuy+=iP-c@-Wrxu$VWsOy{y(e$Y@ zxC1w!iasad^%spwR$D>(zoO!Tw`*}!y2aZB^=h8sOHsXIDtVfgl2_g>yfAav3*G`j{f5=UcxsTo}z0~n{Gk{s{=$153~_(nRojx|}pc+@N& z`XYLuPS!rtsz-_!U&KT#L?5VGqKQf)KGHTv=2fE^FXrPt^gR-lqcIfXM@T%6r>Xb7 zq+w8^($3e`Ni)MMG8&Cz%J3*8Upg+GzkrZ@LuV75VA|K?tyMlaIn8yP2cISHwPyev>#7`eT(;KYUR&kh2{(Swb$Ilj8 zmsi6t;GXnenAO0e+uNCu`xtx!Bk&yLp###8kpIXV@~Zq^UY0dkl^@Du@*TMk*Y(L# zDnnIkvrQvF*`(4fi=!gKlowmV)JalFpj4tKiA749%G!p3Gx(QoYzu>vYZyX`?3``r zXwZn8q}8|}o(>&{=F_6Yjh;=8hz*m#(1+p|+PgMszkx!WBYj|R@xHgK)O@@iaGkd37Vc0ng^riEjyhgz zn^kw&g!&U=zpc*1#O3?w)t30}d;mR3Ed!_SQ5yUTKf)FG7QTi{Fc0V8EPM`C_ykU2 uCHKN!(p@G)m`)RV9w7jsl!UHJm`V{QlZ1{#Cf`NTiKKIi;7Z{OC~?yaA4Bq vpKQn@Gr5{mfKgy`8P_D%%^VIt 1; } - // ToDo - bool addToDB() { - return false; + + bool addToDB(int topic_id) { + int id = sqlAddNewQuestion(topic_id, this.number, this.text); + + if (!id) + return false; + + foreach (answer; answers.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + if (!answer.value.addToDB(id)) + return false; + } + + return true; } } @@ -81,7 +98,7 @@ int main(string[] args) string text; Question[int] questions; - ulong getCount() { + ulong count() { return questions.length; } @@ -89,25 +106,69 @@ int main(string[] args) return questions.length > 1; } - // ToDo bool addToDB() { - return false; + if (!check()) + return false; + + int id = sqlAddNewTheme(this.text); + + if (!id) + return false; + + foreach (question; this.questions.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + if (!question.value.addToDB(id)) + return false; + } + + return true; } void print() { - writeln("Количество вопросов: %s".format(this.getCount())); + writeln("Количество вопросов: %s".format(this.count())); foreach (question; this.questions.byKeyValue.array.sort!((a, b) => a.key < b.key)) { writeln("\tВопрос №%d: %s".format(question.key, question.value.text)); - writeln("\tКоличество ответов: %d".format(question.value.getCount())); - if (question.value.isValid()) - foreach (answer; question.value.answers.byKeyValue.array.sort!((a, b) => a.key < b.key)) { - writeln("\t\tОтвет №%d: %s".format(answer.key, answer.value.text)); - } - else - writeln("\t\tНедостаточно количества ответов"); + writeln("\tКоличество ответов: %d".format(question.value.count())); + foreach (answer; question.value.answers.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + writeln("\t\tОтвет №%d: %s".format(answer.key, answer.value.text)); + } } } + + bool check() { + bool error = false; + + if (!this.isValid()) { + writeln("Недостаточное количество вопросов: %d".format(this.count())); + error = true; + } + + if (!error) { + foreach (question; this.questions.byKeyValue.array.sort!((a, b) => a.key < b.key)) { + if (!question.value.isValid()) { + writeln("Вопрос №%d \"%s\" содержит недостаточное количество ответов: %d".format( + question.key, question.value.text, question.value.count() + )); + error = true; + } + + int truth; + + foreach (answer; question.value.answers) { + if (answer.truth) + ++truth; + } + + if (truth > 1) { + writeln("Вопрос №%d \"%s\" содержит более одного правильного ответа".format( + question.key, question.value.text + )); + error = true; + } + } + } + return !error; + } } Theme theme; @@ -132,24 +193,26 @@ int main(string[] args) if (match[GROUP_QUESTION].length) { numAnswer = 0; - theme.questions[++numQuestion] = Question(match[GROUP_QUESTION_TEXT]); + theme.questions[++numQuestion] = Question(numQuestion, match[GROUP_QUESTION_TEXT]); continue; } if (match[GROUP_ANSWER].length) { - theme.questions[numQuestion].answers[++numAnswer] = Answer(match[GROUP_ANSWER_TEXT], false); + theme.questions[numQuestion].answers[++numAnswer] = Answer(numAnswer, match[GROUP_ANSWER_TEXT], false); continue; } if (match[GROUP_ANSWER_RIGHT].length) { - theme.questions[numQuestion].answers[++numAnswer] = Answer(match[GROUP_ANSWER_RIGHT_TEXT], true); + theme.questions[numQuestion].answers[++numAnswer] = Answer(numAnswer, match[GROUP_ANSWER_RIGHT_TEXT], true); continue; } } - theme.print(); + // theme.print(); + // theme.check(); + theme.addToDB(); return EXIT_SUCCESS; } diff --git a/source/sql.d b/source/sql.d new file mode 100644 index 0000000..c30ce93 --- /dev/null +++ b/source/sql.d @@ -0,0 +1,51 @@ +module source.sql; + +import singlog; +import std.conv; + +import source.dblite; + +int sqlAddNewTheme(string topic) { + try { + auto queryResult = dblite.sql( + "insert into topics (name) values (?) returning id", + topic + ); + if (!queryResult.empty()) + return queryResult.front()["id"].to!int; + } catch (Exception e) { + log.e("Не удалось выполнить запрос к БД. " ~ e.msg); + } + + return 0; +} + +int sqlAddNewQuestion(int topic_id, int question_number, string question_text) { + try { + auto queryResult = dblite.sql( + "insert into questions (topic_id, number, text) values (?, ?, ?) returning id", + topic_id, question_number, question_text + ); + if (!queryResult.empty()) + return queryResult.front()["id"].to!int; + } catch (Exception e) { + log.e("Не удалось выполнить запрос к БД. " ~ e.msg); + } + + return 0; +} + +int sqlAddNewAnswer(int question_id, int answer_number, string answer_text, int answer_truth) { + try { + auto queryResult = dblite.sql( + "insert into answers (question_id, number, text, truth) values (?, ?, ?, ?) returning id", + question_id, answer_number, answer_text, answer_truth + ); + if (!queryResult.empty()) + return queryResult.front()["id"].to!int; + } catch (Exception e) { + log.e("Не удалось выполнить запрос к БД. " ~ e.msg); + } + + return 0; +}