File size: 2,119 Bytes
18a7173
 
 
f1e3968
18a7173
cb0dd3a
18a7173
 
 
4aa1519
 
 
 
18a7173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c9b453
 
 
 
 
 
 
18a7173
6c9b453
 
18a7173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, request
import os
from flask_sqlalchemy import SQLAlchemy
import logging


app = Flask(__name__)


# Log every request
@app.before_request
def log_request_info():
    logging.info(f"Request URL: {request.url} | Method: {request.method} | Body: {request.get_data()}")








base_dir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(base_dir, "data.db")}'
db = SQLAlchemy(app)



class Drink(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.String(120))


    def __repr__(self):
        return f"{self.name} - {self.description}"


@app.route('/')
def index():
    drinks = Drink.query.all()
    print(drinks)
    output = []
    for drink in drinks:
        drink_data = {'name':drink.name, 'description':drink.description}

        output.append(drink_data)

    return {'drinks': output }
   

@app.route('/drinks')
def getdrinks():
    drinks = Drink.query.all()
    print(drinks)
    output = []
    for drink in drinks:
        drink_data = {'name':drink.name, 'description':drink.description}

        output.append(drink_data)

    return {'drinks': output }

@app.route('/drinks/<id>')
def getdrink(id):
    drink = Drink.query.get_or_404(id)
    return {"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/<id>', methods=['DELETE'])
def deletedrink(id):
    drink = Drink.query.get(id)
    if drink is None:
        return {"error":"not found"}
    db.session.delete(drink)
    db.session.commit()
    return {"message":"deleted lol"}


# Ensure the table is created when the app is run
with app.app_context():
    db.create_all()

# Required to run the app on Hugging Face Spaces
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)