лаб6: адаптував репозиторій та додав файли, що стосуються лаб. №6
This commit is contained in:
parent
ba81f91189
commit
f8a792ce7b
|
@ -66,11 +66,6 @@ module.exports = {
|
||||||
title: 'Висновки',
|
title: 'Висновки',
|
||||||
path:"/conclusion/"
|
path:"/conclusion/"
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
|
||||||
title: 'API',
|
|
||||||
path:"/api/"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
],
|
||||||
sidebarDepth: 2,
|
sidebarDepth: 2,
|
||||||
|
|
|
@ -6,23 +6,11 @@ actionLink: /intro/
|
||||||
footer: "ECL 2.0 Licensed | Copyright © 2024 [Your Name]"
|
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)]*
|
|
||||||
|
|
||||||
|
|
||||||
**Керівник**
|
**Керівник**
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
# Висновки
|
# Висновки
|
||||||
|
|
||||||
У висновках наводять оцінку отриманих результатів, можливі галузі його використання. Висновки повинні містити в собі коротку узагальнену оцінку результатів розробки, у
|
В результаті проведеної роботи ми розробили модель бази даних, яку можна використовувати для реалізації систем проведення експертних опитувань. В рамках цього відгалуження розробив серверну частину, що надає RESTFUL-інтерфейс для доступу до бази даних. Вона може використовуватися для взаємодії з будь-якою з таблиць, проте я приділив окрему увагу розширенню гнучкості інтерфейсу взаємодії з таблицею опитувань (survey).
|
||||||
тому числі і з погляду на їх технічно-економічну ефективність. Необхідно порівняти
|
|
||||||
отримані результати усіх характеристик об’єкта проєктування із завданням і з основними показниками сучасних аналогічних об’єктів.
|
|
||||||
|
|
||||||
Необхідно вказати яке нове технічне рішення покладене в основу проєкту і у чому її
|
Проєкт мав на меті реалізацію двох ключових особливостей:
|
||||||
переваги, що нового було запропоновано самим студентом.
|
- оптимальність використання мережі
|
||||||
|
- гнучкість у застосуванні
|
||||||
|
|
||||||
На базі отриманих висновків можуть надаватися рекомендації по використанню розробки. Вони повинні
|
Оптимальність використання мережі досягається використанням протоколу HTTP без домішок у вигляді додаткових бібліотек, фреймворків, аналітики, відстеження дій користувачів тощо. Завдяки цьому середній обсяг повідомлення становить до 100 байтів, тому сервер, за використання з'єднання з пропускною здатністю 1 Gb/s, зможе обробляти до 1.34 млн повідомлень щосекунди, а це дозволить обслуговувати близько 400 тис. користувачів одночасно. В свою чергу, користувачі зможуть отримувати обслуговування від сервера навіть за умов поганого мережевого з'єднання, що дуже актуально в сучасних реаліях (вимкнення електропостачання, перебої зі зв'язком тощо).
|
||||||
мати конкретний характер і бути цілком підтверджені проєктом.
|
|
||||||
|
|
||||||
|
Гнучкість проєкту досягається завдяки простій архітектурі, що не прив'язана до визначених ролей. Натомість, кожен зареєстрований користувач може використовувати можливості системи в повному обсязі, що робить її універсальною для виконання багатьох завдань: від збору думок вузького кола співробітників до проведення масштабних статистичних досліджень із залученням мільйонів користувачів.
|
||||||
|
|
||||||
|
Отже, наша розробка досягла поставленої мети й є особливо актуальною в умовах сьогодення.
|
|
@ -110,4 +110,225 @@ ENGINE = InnoDB;
|
||||||
SET SQL_MODE=@OLD_SQL_MODE;
|
SET SQL_MODE=@OLD_SQL_MODE;
|
||||||
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
|
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
|
||||||
SET UNIQUE_CHECKS=@OLD_UNIQUE_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'
|
||||||
|
|
||||||
```
|
```
|
|
@ -1,4 +1,197 @@
|
||||||
# Тестування працездатності системи
|
# Тестування працездатності системи
|
||||||
|
|
||||||
*В цьому розділі необхідно вказати засоби тестування, навести вихідні коди тестів та результати тестування.*
|
## Передумови
|
||||||
|
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)> <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;">"id"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 1"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 2"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">'{"isPaused": false, "isNamed": true, "name": "Test 3", "duration": "1w", "account_id": 2}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">"Content-Type: application/json"</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span>
|
||||||
|
{"success": 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)> <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;">"id"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 1"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 2"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>12<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 3"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">'{"id": 8, "isPaused": true, "isNamed": true, "name": "Test 3", "duration": "4w", "account_id": 2}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">"Content-Type: application/json"</span><span style="color:dimgray;"></span> <span style="color:aqua;">http://127.0.0.1:5000/api/survey</span>
|
||||||
|
{"success": 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)> <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;">"id"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 1"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 2"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>12<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 3"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"4w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">'{"id": 12}'</span><span style="color:dimgray;"></span> <span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">"Content-Type: application/json"</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)> <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;">"id"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 1"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 2"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">"id"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 1"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>0<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 3"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"1w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">'{"isPaused": true,
|
||||||
|
"isNamed": true,
|
||||||
|
"name": "Test 4",
|
||||||
|
"duration": "2w"}'</span><span style="color:dimgray;"></span> \
|
||||||
|
<span style="color:aqua;">--header</span><span style="color:dimgray;"></span> <span style="color:yellow;">"Content-Type: application/json"</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>
|
||||||
|
{"success": 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)> </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;">"id"</span><span style="font-weight:bold;">: </span>4<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isPaused"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"isNamed"</span><span style="font-weight:bold;">: </span>1<span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"name"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"Test 4"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"duration"</span><span style="font-weight:bold;">: </span><span style="color:lime;">"2w"</span><span style="font-weight:bold;">,
|
||||||
|
</span><span style="font-weight:bold;color:#3333FF;">"account_id"</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)> <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;">"Content-Type: application/json"</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)
|
||||||
|
> DELETE /api/survey/4 HTTP/1.1
|
||||||
|
> Host: 127.0.0.1:5000
|
||||||
|
> User-Agent: curl/7.88.1
|
||||||
|
> Accept: */*
|
||||||
|
> Content-Type: application/json
|
||||||
|
> Content-Length: 2
|
||||||
|
>
|
||||||
|
< HTTP/1.1 200 OK
|
||||||
|
< Server: Werkzeug/3.0.3 Python/3.11.2
|
||||||
|
< Date: Tue, 21 May 2024 14:45:09 GMT
|
||||||
|
< Content-Type: text/html; charset=utf-8
|
||||||
|
< Content-Length: 94
|
||||||
|
< Connection: close
|
||||||
|
<
|
||||||
|
[{'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)>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
Бачимо, що ми можемо отримувати інформацію про окремі опитування, змінювати та видаляти їх.
|
|
@ -0,0 +1,214 @@
|
||||||
|
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'
|
Loading…
Reference in New Issue