-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (72 loc) · 2.42 KB
/
app.py
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
94
95
96
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_restful import Resource, Api
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///offers.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Offer(db.Model):
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(80), unique=True)
when = db.Column(db.String(80))
where = db.Column(db.String(80))
note = db.Column(db.String(80))
def __init__(self, category, when, where, note):
self.category = category
self.when = when
self.where = where
self.note = note
class OfferSchema(ma.Schema):
class Meta:
fields = ('id', 'category', 'when', 'where', 'note')
offer_schema = OfferSchema()
offers_schema = OfferSchema(many=True)
class OfferManager(Resource):
@staticmethod
def get():
try:
id = request.args['id']
except Exception as _:
id = None
if not id:
offers = Offer.query.all()
return jsonify(offers_schema.dump(offers))
offer = Offer.query.get(id)
response = jsonify(offer_schema.dump(offer))
return response
@staticmethod
def post():
category = request.json['category']
when = request.json['when']
where = request.json['where']
note = request.json['note']
offer = Offer(category, when, where, note)
db.session.add(offer)
db.session.commit()
response = jsonify({'Message': f'New Offer insterted to database.'})
return response
@staticmethod
def delete():
try:
id = request.args['id']
except Exception as _:
id = None
if not id:
last_offer = db.session.query(
Offer).order_by(Offer.id.desc()).first()
db.session.delete(last_offer)
db.session.commit()
return jsonify({'Message': f'Last offer deleted.'})
offer = Offer.query.get(id)
db.session.delete(offer)
db.session.commit()
return jsonify({'Message': f'Offer {str(id)} deleted.'})
api.add_resource(OfferManager, '/api/offers')
if __name__ == '__main__':
app.run(debug=True)
# Command to start server: python3 app.py