backend
This commit is contained in:
162
deck_endpoints.py
Normal file
162
deck_endpoints.py
Normal file
@@ -0,0 +1,162 @@
|
||||
# deck_endpoints.py
|
||||
|
||||
from flask import Blueprint, request, jsonify
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
deck_bp = Blueprint('deck_bp', __name__)
|
||||
|
||||
DATABASE = 'mydatabase.db'
|
||||
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect(DATABASE)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
# Erstellen der Tabellen, falls sie nicht existieren
|
||||
def init_db():
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
# Tabelle Deck erstellen
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS Deck (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
deckname TEXT UNIQUE NOT NULL
|
||||
)
|
||||
''')
|
||||
# Tabelle Image erstellen
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS Image (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
deckid INTEGER,
|
||||
bildname TEXT,
|
||||
iconindex INTEGER,
|
||||
x1 REAL,
|
||||
x2 REAL,
|
||||
y1 REAL,
|
||||
y2 REAL,
|
||||
FOREIGN KEY(deckid) REFERENCES Deck(id)
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@deck_bp.route('/api/decks', methods=['POST'])
|
||||
def create_deck():
|
||||
data = request.get_json()
|
||||
if not data or 'deckname' not in data:
|
||||
return jsonify({'error': 'No deckname provided'}), 400
|
||||
|
||||
deckname = data['deckname']
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute('INSERT INTO Deck (deckname) VALUES (?)', (deckname,))
|
||||
conn.commit()
|
||||
deck_id = cursor.lastrowid
|
||||
conn.close()
|
||||
return jsonify({'status': 'success', 'deck_id': deck_id}), 201
|
||||
except sqlite3.IntegrityError:
|
||||
conn.close()
|
||||
return jsonify({'error': 'Deckname already exists'}), 400
|
||||
|
||||
@deck_bp.route('/api/decks', methods=['GET'])
|
||||
def get_decks():
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
decks = cursor.execute('SELECT * FROM Deck').fetchall()
|
||||
deck_list = []
|
||||
for deck in decks:
|
||||
deck_id = deck['id']
|
||||
deck_name = deck['deckname']
|
||||
# Alle Images für dieses Deck abrufen
|
||||
images = cursor.execute('''
|
||||
SELECT
|
||||
bildname AS name,
|
||||
iconindex,
|
||||
x1,
|
||||
x2,
|
||||
y1,
|
||||
y2
|
||||
FROM Image
|
||||
WHERE deckid = ?
|
||||
''', (deck_id,)).fetchall()
|
||||
images_list = [dict(image) for image in images]
|
||||
# Deck mit Namen und zugehörigen Images hinzufügen
|
||||
deck_dict = {
|
||||
'name': deck_name,
|
||||
'images': images_list
|
||||
}
|
||||
deck_list.append(deck_dict)
|
||||
conn.close()
|
||||
return jsonify(deck_list)
|
||||
|
||||
@deck_bp.route('/api/decks/<deckname>', methods=['DELETE'])
|
||||
def delete_deck(deckname):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
# Zuerst die Images löschen, die zu diesem Deck gehören
|
||||
cursor.execute('SELECT id FROM Deck WHERE deckname = ?', (deckname,))
|
||||
deck = cursor.fetchone()
|
||||
if deck:
|
||||
deck_id = deck['id']
|
||||
cursor.execute('DELETE FROM Image WHERE deckid = ?', (deck_id,))
|
||||
# Dann das Deck löschen
|
||||
cursor.execute('DELETE FROM Deck WHERE id = ?', (deck_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({'status': 'success'}), 200
|
||||
else:
|
||||
conn.close()
|
||||
return jsonify({'error': 'Deck not found'}), 404
|
||||
|
||||
@deck_bp.route('/image', methods=['PUT'])
|
||||
def update_image():
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({'error': 'No data provided'}), 400
|
||||
|
||||
required_fields = ['deckid', 'bildname', 'iconindex', 'x1', 'x2', 'y1', 'y2']
|
||||
if not all(field in data for field in required_fields):
|
||||
return jsonify({'error': 'Missing fields in data'}), 400
|
||||
|
||||
deckid = data['deckid']
|
||||
bildname = data['bildname']
|
||||
iconindex = data['iconindex']
|
||||
x1 = data['x1']
|
||||
x2 = data['x2']
|
||||
y1 = data['y1']
|
||||
y2 = data['y2']
|
||||
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO Image (deckid, bildname, iconindex, x1, x2, y1, y2)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
''', (deckid, bildname, iconindex, x1, x2, y1, y2))
|
||||
conn.commit()
|
||||
image_id = cursor.lastrowid
|
||||
conn.close()
|
||||
return jsonify({'status': 'success', 'image_id': image_id}), 201
|
||||
|
||||
@deck_bp.route('/image/<bildname>', methods=['GET'])
|
||||
def get_images_by_bildname(bildname):
|
||||
conn = get_db_connection()
|
||||
images = conn.execute('SELECT * FROM Image WHERE bildname = ?', (bildname,)).fetchall()
|
||||
conn.close()
|
||||
image_list = [dict(image) for image in images]
|
||||
return jsonify(image_list)
|
||||
|
||||
@deck_bp.route('/image/<bildname>/<int:iconindex>', methods=['GET'])
|
||||
def get_image_by_bildname_and_index(bildname, iconindex):
|
||||
conn = get_db_connection()
|
||||
image = conn.execute('SELECT * FROM Image WHERE bildname = ? AND iconindex = ?', (bildname, iconindex)).fetchone()
|
||||
conn.close()
|
||||
if image is None:
|
||||
return jsonify({'error': 'Image not found'}), 404
|
||||
else:
|
||||
return jsonify(dict(image)), 200
|
||||
|
||||
# Sicherstellen, dass die Datenbank existiert
|
||||
if not os.path.exists(DATABASE):
|
||||
init_db()
|
||||
Reference in New Issue
Block a user