feat: implement backend with Flask for game collection management using SQLite database
This commit is contained in:
		
							
								
								
									
										5
									
								
								Verzeichnisstruktur
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Verzeichnisstruktur
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
/game_collection
 | 
			
		||||
    ├── app.py
 | 
			
		||||
    ├── models.py
 | 
			
		||||
    ├── database.py
 | 
			
		||||
    └── requirements.txt
 | 
			
		||||
							
								
								
									
										41
									
								
								game_collection/app.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								game_collection/app.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
from flask import Flask, request, jsonify
 | 
			
		||||
from flask_sqlalchemy import SQLAlchemy
 | 
			
		||||
from database import init_db
 | 
			
		||||
from models import db, Game
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///games.db'
 | 
			
		||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
 | 
			
		||||
db.init_app(app)
 | 
			
		||||
 | 
			
		||||
with app.app_context():
 | 
			
		||||
    init_db()
 | 
			
		||||
 | 
			
		||||
@app.route('/games', methods=['POST'])
 | 
			
		||||
def add_game():
 | 
			
		||||
    data = request.json
 | 
			
		||||
    new_game = Game(
 | 
			
		||||
        image=data['image'],
 | 
			
		||||
        title=data['title'],
 | 
			
		||||
        date=data['date'],
 | 
			
		||||
        buyer=data['buyer'],
 | 
			
		||||
        owned=data['owned']
 | 
			
		||||
    )
 | 
			
		||||
    db.session.add(new_game)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    return jsonify({'message': 'Game added!'}), 201
 | 
			
		||||
 | 
			
		||||
@app.route('/games', methods=['GET'])
 | 
			
		||||
def get_games():
 | 
			
		||||
    games = Game.query.all()
 | 
			
		||||
    return jsonify([{
 | 
			
		||||
        'id': game.id,
 | 
			
		||||
        'image': game.image,
 | 
			
		||||
        'title': game.title,
 | 
			
		||||
        'date': game.date,
 | 
			
		||||
        'buyer': game.buyer,
 | 
			
		||||
        'owned': game.owned
 | 
			
		||||
    } for game in games])
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    app.run(debug=True)
 | 
			
		||||
							
								
								
									
										17
									
								
								game_collection/database.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								game_collection/database.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
import sqlite3
 | 
			
		||||
 | 
			
		||||
def init_db():
 | 
			
		||||
    conn = sqlite3.connect('games.db')
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
    cursor.execute('''
 | 
			
		||||
        CREATE TABLE IF NOT EXISTS games (
 | 
			
		||||
            id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
            image TEXT,
 | 
			
		||||
            title TEXT NOT NULL,
 | 
			
		||||
            date TEXT,
 | 
			
		||||
            buyer TEXT,
 | 
			
		||||
            owned BOOLEAN NOT NULL
 | 
			
		||||
        )
 | 
			
		||||
    ''')
 | 
			
		||||
    conn.commit()
 | 
			
		||||
    conn.close()
 | 
			
		||||
							
								
								
									
										11
									
								
								game_collection/models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								game_collection/models.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
from flask_sqlalchemy import SQLAlchemy
 | 
			
		||||
 | 
			
		||||
db = SQLAlchemy()
 | 
			
		||||
 | 
			
		||||
class Game(db.Model):
 | 
			
		||||
    id = db.Column(db.Integer, primary_key=True)
 | 
			
		||||
    image = db.Column(db.String(255))
 | 
			
		||||
    title = db.Column(db.String(100), nullable=False)
 | 
			
		||||
    date = db.Column(db.String(10))
 | 
			
		||||
    buyer = db.Column(db.String(100))
 | 
			
		||||
    owned = db.Column(db.Boolean, nullable=False)
 | 
			
		||||
							
								
								
									
										2
									
								
								game_collection/requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								game_collection/requirements.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
Flask
 | 
			
		||||
Flask-SQLAlchemy
 | 
			
		||||
		Reference in New Issue
	
	Block a user