commit ae4f5c746c1111776e18584ecc3f0f226d054e2d Author: arthur Date: Sun Jan 8 16:41:02 2023 +0100 first commit diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc new file mode 100644 index 0000000..f0f2c4f Binary files /dev/null and b/__pycache__/app.cpython-310.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..2b5c6c2 --- /dev/null +++ b/app.py @@ -0,0 +1,53 @@ +from flask import Flask, jsonify, request +app = Flask(__name__) +from flask_sqlalchemy import SQLAlchemy + +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' +db = SQLAlchemy(app) + +app.app_context().push() + +class Drink(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(80), unique=True, nullable=False) + description = db.Column(db.String(120)) + + def __repr__(self) -> str: + return f"{self.name} - {self.description}" + + +@app.route('/') +def index(): + return 'Hello World !' + +@app.route('/drinks') +def get_drinks(): + drinks = Drink.query.all() + + output = [] + for drink in drinks: + drink_data = {'name': drink.name, 'description': drink.description} + output.append(drink_data) + + return {"drinks": output} + +@app.route('/drinks/') +def get_drink(id): + drink = Drink.query.get_or_404(id) + return jsonify({'name': drink.name, "description": drink.description}) + +@app.route('/drinks', methods=['POST']) +def add_drink(): + drink = Drink(name=request.json['name'], description=request.json['description']) + db.session.add(drink) + db.session.commit() + return {'id': drink.id} + +@app.route('/drinks/',methods=['DELETE']) +def delete_drink(id): + drink = Drink.query.get(id) + if drink is None: + return {'error': "not found"} + db.session.delete(drink) + db.session.commit() + return {"message": '{} succesfuly deleted'.format(drink.name)} \ No newline at end of file diff --git a/instance/data.db b/instance/data.db new file mode 100644 index 0000000..e76d311 Binary files /dev/null and b/instance/data.db differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29