-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
58 lines (40 loc) · 1.58 KB
/
Copy pathapplication.py
File metadata and controls
58 lines (40 loc) · 1.58 KB
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
from flask import Flask, request
app = Flask(__name__)
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
class User(db.Model): #creating a model with sql alchemy
id = db.Column(db.Integer, primary_key=True) #creating a coloumn with ID integer where primary key exists
name = db.Column(db.String(80), unique=True, nullable=False) #creating a coloumn with name as a string with max 80 chars, always unique name and no null values
description = db.Column(db.String(120))
def __repr__(self) :
return f"{self.name} - {self.description}"
@app.route('/')
def index():
return 'Hello Pussy'
@app.route('/users') #writing a get method to return users
def get_users():
users = User.query.all()
output = []
for user in users:
user_Data={'name': user.name,'description':user.description}
output.append(user_Data)
return {"users": output} #returning a dictionary with some dummy data
@app.route('/users/<id>')
def get_user(id):
user=User.query.get_or_404(id)
return {'name': user.name,'description':user.description}
@app.route('/users',methods=['POST'])
def add_user():
user = User(name=request.json['name'],description=request.json['description'])
db.session.add(user)
db.session.commit()
return {'id': user.id}
@app.route('/users/<id>', methods=['DELETE'])
def delete_user(id):
user = User.query.get(id)
if user is None:
return {"404"}
db.session.delete(user)
db.session.commit()
return {"message": "Hell yeah BITCH"}