Compare commits

..

No commits in common. "ce31822f97b84476400300a0c53323b392fea054" and "ba81f91189d92ef36c6bc2118f882c11fa79b9fa" have entirely different histories.

7 changed files with 29 additions and 641 deletions

View File

@ -67,6 +67,11 @@ module.exports = {
path:"/conclusion/"
},
{
title: 'API',
path:"/api/"
}
],
sidebarDepth: 2,
displayAllHeaders: true, // Default: false

View File

@ -6,11 +6,23 @@ actionLink: /intro/
footer: "ECL 2.0 Licensed | Copyright © 2024 [Your Name]"
---
**Виконав:**
**Виконали:**
*студент 2-го курсу, групи ІО-23:*
*студенти 2-го курсу, групи ІО-23:*
*Олег ШМУЛЯР [shmuliar1@ukr.net], [@dmytrofiot23](https://t.me/dmytrofiot23)*
*Олег ШМУЛЯР [shmuliar1@ukr.net, [@dmytrofiot23](https://t.me/dmytrofiot23)]*
*Андрій БОДНАР [bodnarandrew123@gmail.com, [@andrux4](https://t.me/andrux4)]*
*Андрій ШВЕД [andreyfrog26@gmail.com, [@Rhinemann](https://t.me/Rhinemann)]*
*Євгеній ГОЛОВАТЕНКО [ievgeniigol@gmail.com, [@yevholova](https://t.me/yevholova)]*
*Вікторія ВОДЯНА [vodyanayaviktoria@gmail.com, [@victoriavodyana](https://t.me/victoriavodyana)]*
*Михайло КОРБУТ [korbutmykhailo@gmail.com, [@misha1tigr](https://t.me/misha1tigr)]*
*Олександр ГУРАНЕЦЬ [bacant150@gmail.com, [@Bacant150](https://t.me/Bacant150)]*
**Керівник**

View File

@ -1,13 +1,12 @@
# Висновки
В результаті проведеної роботи ми розробили модель бази даних, яку можна використовувати для реалізації систем проведення експертних опитувань. В рамках цього відгалуження розробив серверну частину, що надає RESTFUL-інтерфейс для доступу до бази даних. Вона може використовуватися для взаємодії з будь-якою з таблиць, проте я приділив окрему увагу розширенню гнучкості інтерфейсу взаємодії з таблицею опитувань (survey).
У висновках наводять оцінку отриманих результатів, можливі галузі його використання. Висновки повинні містити в собі коротку узагальнену оцінку результатів розробки, у
тому числі і з погляду на їх технічно-економічну ефективність. Необхідно порівняти
отримані результати усіх характеристик об’єкта проєктування із завданням і з основними показниками сучасних аналогічних об’єктів.
Проєкт мав на меті реалізацію двох ключових особливостей:
- оптимальність використання мережі
- гнучкість у застосуванні
Необхідно вказати яке нове технічне рішення покладене в основу проєкту і у чому її
переваги, що нового було запропоновано самим студентом.
Оптимальність використання мережі досягається використанням протоколу HTTP без домішок у вигляді додаткових бібліотек, фреймворків, аналітики, відстеження дій користувачів тощо. Завдяки цьому середній обсяг повідомлення становить до 100 байтів, тому сервер, за використання з'єднання з пропускною здатністю 1 Gb/s, зможе обробляти до 1.34 млн повідомлень щосекунди, а це дозволить обслуговувати близько 400 тис. користувачів одночасно. В свою чергу, користувачі зможуть отримувати обслуговування від сервера навіть за умов поганого мережевого з'єднання, що дуже актуально в сучасних реаліях (вимкнення електропостачання, перебої зі зв'язком тощо).
На базі отриманих висновків можуть надаватися рекомендації по використанню розробки. Вони повинні
мати конкретний характер і бути цілком підтверджені проєктом.
Гнучкість проєкту досягається завдяки простій архітектурі, що не прив'язана до визначених ролей. Натомість, кожен зареєстрований користувач може використовувати можливості системи в повному обсязі, що робить її універсальною для виконання багатьох завдань: від збору думок вузького кола співробітників до проведення масштабних статистичних досліджень із залученням мільйонів користувачів.
Отже, наша розробка досягла поставленої мети й є особливо актуальною в умовах сьогодення.

View File

@ -111,224 +111,3 @@ SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
```
## RESTfull-сервіс для керування опитуваннями
Код серверу (Python, Flask):
```python
from flask import Flask, request
import mariadb as mdb
import json
import sys
def init_db():
return mdb.connect(host="127.0.0.1", port=3306, user="user", password="testing432")
c = 0
app = Flask(__name__)
@app.route("/api/<endpoint>", methods = ["GET", "POST", "PUT", "DELETE"])
def api_test(endpoint):
se = endpoint
match se:
case "counter":
global c
c += 1
case "account":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, username FROM odb.account;")
r = [{"id": i, "username": j} for i, j in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.account (username, password) VALUES (\"{d['username']}\", \"{d['password']}\");")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.account SET username = \"{d['username']}\", password = \"{d['password']}\" WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, username FROM odb.account WHERE id = {d['id']};")
r = [{"id": i, "username": j} for i, j in cur]
cur.execute(f"DELETE FROM odb.account WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "survey":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey;")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.survey (isPaused, isNamed, name, duration, account_id) VALUES ({d['isPaused']}, {d['isNamed']}, \"{d['name']}\", \"{d['duration']}\", {d['account_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.survey SET isPaused = {d['isPaused']}, isNamed = {d['isNamed']}, name = \"{d['name']}\", duration = \"{d['duration']}\" WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {d['id']};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
cur.execute(f"DELETE FROM odb.survey WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "link":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, uses, responces, usageLimit, responceLimit, path, survey_id FROM odb.link;")
r = [{"id": i[0], "uses": i[1], "responces": i[2], "usageLimit": i[3], "responceLimit": i[4], "path": i[5], "survey_id": i[6]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.link (usageLimit, responceLimit, path, survey_id) VALUES ({d['usageLimit']}, {d['responceLimit']}, \"{d['path']}\", {d['survey_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.link SET uses = {d['uses']}, responces = {d['responces']}, usageLimit = \"{d['usageLimit']}\", responceLimit = \"{d['responceLimit']}\", path = \"{d['path']}\", survey_id = {d['survey_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, uses, responces, usageLimit, responceLimit, path, survey_id FROM odb.survey WHERE id = {d['id']};")
r = [{"id": i[0], "uses": i[1], "responces": i[2], "usageLimit": i[3], "responceLimit": i[4], "path": i[5], "survey_id": i[6]} for i in cur]
cur.execute(f"DELETE FROM odb.link WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "question":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, text, survey_id FROM odb.question;")
r = [{"id": i[0], "text": i[1], "survey_id": i[2]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.question (text, survey_id) VALUES (\"{d['text']}\", {d['survey_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.question SET text = \"{d['text']}\", survey_id = {d['survey_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, text, survey_id FROM odb.question WHERE id = {d['id']};")
r = [{"id": i[0], "text": i[1], "survey_id": i[2]} for i in cur]
cur.execute(f"DELETE FROM odb.question WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "responce":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, value, question_id, account_id FROM odb.responce;")
r = [{"id": i[0], "value": i[1], "question_id": i[2], "account_id": i[3]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.responce (text, question_id, account_id) VALUES (\"{d['value']}\", {d['question_id']}, {d['account_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.responce SET value = \"{d['value']}\", question_id = {d['question_id']}, account_id = {d['account_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, value, question_id, account_id FROM odb.responce WHERE id = {d['id']};")
r = [{"id": i[0], "value": i[1], "question_id": i[2], "account_id": i[3]} for i in cur]
cur.execute(f"DELETE FROM odb.responce WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case _:
return f"work {c}\n"
@app.route("/api/survey/<no>", methods = ["GET", "PUT", "DELETE"])
def api_numbered(no):
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {no};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
c.close()
return json.dumps(r)
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.survey SET isPaused = {d['isPaused']}, isNamed = {d['isNamed']}, name = \"{d['name']}\", duration = \"{d['duration']}\" WHERE id = {no};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {no};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
cur.execute(f"DELETE FROM odb.survey WHERE id = {no};")
c.commit()
c.close()
return f'{r}\n'
```

View File

@ -1,197 +1,4 @@
# Тестування працездатності системи
## Передумови
1. Налаштування віртуального середовища
```bash
python3 -m venv .
source bin/activate.fish
pip3 install flask mariadb
```
*В цьому розділі необхідно вказати засоби тестування, навести вихідні коди тестів та результати тестування.*
2. Запуск сервера:
```bash
flask --app server run
```
## Тестування компонентів
Система має два варіанти передачі індексів: у рядку шляху або в тілі запиту. Перевіримо, як працюють обидва варіанти.
### /api/survey
Цей шлях дозволяє використовувати всі 4 HTTP-методи, що використовуються в домовленості REST (GET, POST, PUT, DELETE).
Протестуємо кожен з них на практичному прикладі:
<pre style="
color: #ffffff;
font-size: 10pt;"
>
<span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 1&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>10<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 2&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-X</span><span style="color:dimgray;"></span> <span style="color:aqua;">POST</span><span style="color:dimgray;"></span> <span style="color:aqua;">-d</span><span style="color:dimgray;"></span> <span style="color:yellow;">'{&quot;isPaused&quot;: false, &quot;isNamed&quot;: true, &quot;name&quot;: &quot;Test 3&quot;, &quot;duration&quot;: &quot;1w&quot;, &quot;account_id&quot;: 2}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">&quot;Content-Type: application/json&quot;</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span>
{&quot;success&quot;: true}
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 1&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>10<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 2&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>12<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 3&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>2<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-X</span><span style="color:dimgray;"></span> <span style="color:aqua;">PUT</span><span style="color:dimgray;"></span> <span style="color:aqua;">-d</span><span style="color:dimgray;"></span> <span style="color:yellow;">'{&quot;id&quot;: 8, &quot;isPaused&quot;: true, &quot;isNamed&quot;: true, &quot;name&quot;: &quot;Test 3&quot;, &quot;duration&quot;: &quot;4w&quot;, &quot;account_id&quot;: 2}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">&quot;Content-Type: application/json&quot;</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span>
{&quot;success&quot;: true}
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 1&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>10<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 2&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>12<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 3&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;4w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>2<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-X</span><span style="color:dimgray;"></span> <span style="color:aqua;">DELETE</span><span style="color:dimgray;"></span> <span style="color:aqua;">-d</span><span style="color:dimgray;"></span> <span style="color:yellow;">'{&quot;id&quot;: 12}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">&quot;Content-Type: application/json&quot;</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span>
[{'id': 12, 'isPaused': 1, 'isNamed': 1, 'name': 'Test 3', 'duration': '4w', 'account_id': 2}]
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/demo</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 1&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>10<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 2&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
</pre>
### /api/survey/\<id\>
У цьому випадку доступні лише 3 методи (GET, PUT, DELETE), оскільки під час створення нового опитування його ідентифікатор визначається безпосередньо базою даних.
<pre style="
color: #ffffff;
font-size: 10pt;"
>
<span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/restful-server</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 1&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>10<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">,
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 3&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;1w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/restful-server</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-X</span><span style="color:dimgray;"></span> <span style="color:aqua;">PUT</span><span style="color:dimgray;"></span> \
<span style="color:aqua;">-d</span><span style="color:dimgray;"></span> <span style="color:yellow;">'{&quot;isPaused&quot;: true,
&quot;isNamed&quot;: true,
&quot;name&quot;: &quot;Test 4&quot;,
&quot;duration&quot;: &quot;2w&quot;}'</span><span style="color:dimgray;"></span> \
<span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">&quot;Content-Type: application/json&quot;</span><span style="color:dimgray;"></span> \
<span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey/4 </span>
{&quot;success&quot;: true}
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/restful-server</span> (master)&gt; </span><span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-s</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey/4</span><span style="color:dimgray;"></span> <span style="color:lime;">|</span><span style="color:dimgray;"></span> <span style="color:#3333FF;">jq</span><span style="color:dimgray;"></span> <span style="color:aqua;">.</span><span style="color:dimgray;"></span>
<span style="font-weight:bold;">[
{
</span><span style="font-weight:bold;color:#3333FF;">&quot;id&quot;</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isPaused&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;isNamed&quot;</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;name&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;Test 4&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;duration&quot;</span><span style="font-weight:bold;">: </span><span style="color:lime;">&quot;2w&quot;</span><span style="font-weight:bold;">,
</span><span style="font-weight:bold;color:#3333FF;">&quot;account_id&quot;</span><span style="font-weight:bold;">: </span>5<span style="font-weight:bold;">
}</span><span style="font-weight:bold;">
]</span>
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/restful-server</span> (master)&gt; <span style="color:#3333FF;">curl</span><span style="color:dimgray;"></span> <span style="color:aqua;">-v</span><span style="color:dimgray;"></span> <span style="color:aqua;">-X</span><span style="color:dimgray;"></span> <span style="color:aqua;">DELETE</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">&quot;Content-Type: application/json&quot;</span><span style="color:dimgray;"></span> <span style="color:aqua;">-d</span><span style="color:dimgray;"></span> <span style="color:yellow;">'{}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey/4 </span>
* Trying 127.0.0.1:5000...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
&gt; DELETE /api/survey/4 HTTP/1.1
&gt; Host: 127.0.0.1:5000
&gt; User-Agent: curl/7.88.1
&gt; Accept: */*
&gt; Content-Type: application/json
&gt; Content-Length: 2
&gt;
&lt; HTTP/1.1 200 OK
&lt; Server: Werkzeug/3.0.3 Python/3.11.2
&lt; Date: Tue, 21 May 2024 14:45:09 GMT
&lt; Content-Type: text/html; charset=utf-8
&lt; Content-Length: 94
&lt; Connection: close
&lt;
[{'id': 4, 'isPaused': 1, 'isNamed': 1, 'name': 'Test 4', 'duration': '2w', 'account_id': 5}]
* Closing connection 0
<span style="color:dimgray;"></span><span style="filter: contrast(70%) brightness(190%);color:lime;">user</span>@debian-laptop <span style="color:lime;">~/D/f/t/f/d/m/e/s/restful-server</span> (master)&gt;
</pre>
Бачимо, що ми можемо отримувати інформацію про окремі опитування, змінювати та видаляти їх.

View File

@ -20,6 +20,6 @@ git commit -m '[automated]: pushing regenerated documentation'
# git push -f git@github.com:boldak/<USERNAME>.github.io.git master
# if you are deploying to https://<USERNAME>.github.io/<REPO>
git push -f http://10.1.1.1:3000/hasslesstech/edu-dis-labs-private master:gh-pages
git push -f http://10.1.1.1:3000/hasslesstech/edu-dis-labs master:gh-pages
cd -

View File

@ -1,214 +0,0 @@
from flask import Flask, request
import mariadb as mdb
import json
import sys
def init_db():
return mdb.connect(host="127.0.0.1", port=3306, user="user", password="testing432")
c = 0
app = Flask(__name__)
@app.route("/api/<endpoint>", methods = ["GET", "POST", "PUT", "DELETE"])
def api_test(endpoint):
se = endpoint
match se:
case "counter":
global c
c += 1
case "account":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, username FROM odb.account;")
r = [{"id": i, "username": j} for i, j in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.account (username, password) VALUES (\"{d['username']}\", \"{d['password']}\");")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.account SET username = \"{d['username']}\", password = \"{d['password']}\" WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, username FROM odb.account WHERE id = {d['id']};")
r = [{"id": i, "username": j} for i, j in cur]
cur.execute(f"DELETE FROM odb.account WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "survey":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey;")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.survey (isPaused, isNamed, name, duration, account_id) VALUES ({d['isPaused']}, {d['isNamed']}, \"{d['name']}\", \"{d['duration']}\", {d['account_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.survey SET isPaused = {d['isPaused']}, isNamed = {d['isNamed']}, name = \"{d['name']}\", duration = \"{d['duration']}\" WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {d['id']};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
cur.execute(f"DELETE FROM odb.survey WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "link":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, uses, responces, usageLimit, responceLimit, path, survey_id FROM odb.link;")
r = [{"id": i[0], "uses": i[1], "responces": i[2], "usageLimit": i[3], "responceLimit": i[4], "path": i[5], "survey_id": i[6]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.link (usageLimit, responceLimit, path, survey_id) VALUES ({d['usageLimit']}, {d['responceLimit']}, \"{d['path']}\", {d['survey_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.link SET uses = {d['uses']}, responces = {d['responces']}, usageLimit = \"{d['usageLimit']}\", responceLimit = \"{d['responceLimit']}\", path = \"{d['path']}\", survey_id = {d['survey_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, uses, responces, usageLimit, responceLimit, path, survey_id FROM odb.survey WHERE id = {d['id']};")
r = [{"id": i[0], "uses": i[1], "responces": i[2], "usageLimit": i[3], "responceLimit": i[4], "path": i[5], "survey_id": i[6]} for i in cur]
cur.execute(f"DELETE FROM odb.link WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "question":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, text, survey_id FROM odb.question;")
r = [{"id": i[0], "text": i[1], "survey_id": i[2]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.question (text, survey_id) VALUES (\"{d['text']}\", {d['survey_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.question SET text = \"{d['text']}\", survey_id = {d['survey_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, text, survey_id FROM odb.question WHERE id = {d['id']};")
r = [{"id": i[0], "text": i[1], "survey_id": i[2]} for i in cur]
cur.execute(f"DELETE FROM odb.question WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case "responce":
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute("SELECT id, value, question_id, account_id FROM odb.responce;")
r = [{"id": i[0], "value": i[1], "question_id": i[2], "account_id": i[3]} for i in cur]
c.close()
return json.dumps(r)
case "POST":
d = request.get_json()
cur.execute(f"INSERT INTO odb.responce (text, question_id, account_id) VALUES (\"{d['value']}\", {d['question_id']}, {d['account_id']});")
c.commit()
c.close()
return '{"success": true}\n'
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.responce SET value = \"{d['value']}\", question_id = {d['question_id']}, account_id = {d['account_id']} WHERE id = {d['id']};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
d = request.get_json()
cur.execute(f"SELECT id, value, question_id, account_id FROM odb.responce WHERE id = {d['id']};")
r = [{"id": i[0], "value": i[1], "question_id": i[2], "account_id": i[3]} for i in cur]
cur.execute(f"DELETE FROM odb.responce WHERE id = {d['id']};")
c.commit()
c.close()
return f'{r}\n'
case _:
return f"work {c}\n"
@app.route("/api/survey/<no>", methods = ["GET", "PUT", "DELETE"])
def api_numbered(no):
c = init_db()
cur = c.cursor()
match request.method:
case "GET":
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {no};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
c.close()
return json.dumps(r)
case "PUT":
d = request.get_json()
cur.execute(f"UPDATE odb.survey SET isPaused = {d['isPaused']}, isNamed = {d['isNamed']}, name = \"{d['name']}\", duration = \"{d['duration']}\" WHERE id = {no};")
c.commit()
c.close()
return '{"success": true}\n'
case "DELETE":
cur.execute(f"SELECT id, isPaused, isNamed, name, duration, account_id FROM odb.survey WHERE id = {no};")
r = [{"id": i[0], "isPaused": i[1], "isNamed": i[2], "name": i[3], "duration": i[4], "account_id": i[5]} for i in cur]
cur.execute(f"DELETE FROM odb.survey WHERE id = {no};")
c.commit()
c.close()
return f'{r}\n'