forked from hasslesstech/edu-dis-labs
lab 6 update
This commit is contained in:
@@ -110,4 +110,147 @@ ENGINE = InnoDB;
|
||||
SET SQL_MODE=@OLD_SQL_MODE;
|
||||
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
|
||||
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
|
||||
```
|
||||
```
|
||||
|
||||
## Програмний код RESTful-сервісу
|
||||
|
||||
```python
|
||||
from flask import Flask, request, jsonify
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
from db_config import db_config
|
||||
|
||||
app = Flask(name)
|
||||
|
||||
|
||||
def get_db_connection():
|
||||
try:
|
||||
conn = mysql.connector.connect(**db_config)
|
||||
return conn
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error in connection to database: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/links', methods=['GET'])
|
||||
def get_links():
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
if isinstance(conn, tuple):
|
||||
return conn
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute('SELECT * FROM link')
|
||||
links = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return jsonify(links)
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error getting data: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/link/<int:id>', methods=['GET'])
|
||||
def get_link():
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
if isinstance(conn, tuple):
|
||||
return conn
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute('SELECT * FROM link WHERE id = %s', [1])
|
||||
link = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
if link is None:
|
||||
return jsonify({"error": "Link is not found"}), 404
|
||||
return jsonify(link)
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error getting data: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/link', methods=['POST'])
|
||||
def create_link():
|
||||
try:
|
||||
new_link = request.json
|
||||
conn = get_db_connection()
|
||||
if isinstance(conn, tuple):
|
||||
return conn
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
INSERT INTO link (uses, responses, usageLimit, responseLimit, path, Survey_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
''', (
|
||||
new_link.get('uses'),
|
||||
new_link.get('responses'),
|
||||
new_link.get('usageLimit'),
|
||||
new_link.get('responseLimit'),
|
||||
new_link.get('path'),
|
||||
new_link.get('Survey_id')
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return jsonify({"success": "Link created successfully", "data": new_link}), 201
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error creating link: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/link/<int:id>', methods=['PUT'])
|
||||
def update_link():
|
||||
try:
|
||||
update_link = request.json
|
||||
conn = get_db_connection()
|
||||
if isinstance(conn, tuple):
|
||||
return conn
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
UPDATE link SET
|
||||
uses = %s,
|
||||
responses = %s,
|
||||
usageLimit = %s,
|
||||
responseLimit = %s,
|
||||
path = %s,
|
||||
Survey_id = %s
|
||||
WHERE id = %s
|
||||
''', (
|
||||
update_link.get('uses'),
|
||||
update_link.get('responses'),
|
||||
update_link.get('usageLimit'),
|
||||
update_link.get('responseLimit'),
|
||||
update_link.get('path'),
|
||||
update_link.get('Survey_id'),
|
||||
id
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
if cursor.rowcount == 0:
|
||||
return jsonify({"error": f"Update error: link is not found"}), 404
|
||||
else:
|
||||
return jsonify({"success": f"Link from ID {id} updated successfully", "data": update_link}), 202
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error updating link: {str(e)}"}), 500
|
||||
|
||||
@app.route('/link/<int:id>', methods=['DELETE'])
|
||||
def delete_link():
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
if isinstance(conn, tuple):
|
||||
return conn
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM Link WHERE id = %s', [1])
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
if cursor.rowcount == 0:
|
||||
return jsonify({"error": f"Delete error: link is not found"}), 404
|
||||
else:
|
||||
return jsonify({"success": f"Link from ID {id} deleted successfully"}), 200
|
||||
except Error as e:
|
||||
return jsonify({"error": f"Error deleting link: {str(e)}"}), 500
|
||||
|
||||
|
||||
if name == 'main':
|
||||
app.run(debug=False)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user