-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.py
96 lines (75 loc) · 2.39 KB
/
api.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
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""
from flask import Blueprint, request
from flask_restx import Api, Resource, fields
from models import db, MonthlyCustomers, MonthlySales, ProductSales
blueprint = Blueprint('api', __name__)
api = Api(blueprint)
customer_model = api.model("Monthly Customers", {
"month_name": fields.String,
"customer_count": fields.Integer
})
@api.route("/MonthlyCustomers")
class MonthlyCustomersAPI(Resource):
@api.marshal_with(customer_model, envelope='data')
def get(self, **kwargs):
"""Monthly Customer Count
Returns monthwise customer count
"""
return MonthlyCustomers.query.all()
@api.expect(customer_model, validate=True)
def post(self, **kwargs):
"""Monthly Customer Count
Creates an entry in DB of customer count for a month
"""
data = request.get_json()
row = MonthlyCustomers(**data)
db.session.add(row)
db.session.commit()
return {"status": "success"}
sales_model = api.model("Monthly Sales", {
"month_name": fields.String,
"sale": fields.Integer
})
@api.route("/MonthlySales")
class MonthlySalesAPI(Resource):
@api.marshal_with(sales_model, envelope='data')
def get(self, **kwargs):
"""Monthly Sales
Returns monthwise sales
"""
return MonthlySales.query.all()
@api.expect(sales_model, validate=True)
def post(self, **kwargs):
"""Monthly Sales
Creates an entry in DB of a sale for a month
"""
data = request.get_json()
row = MonthlySales(**data)
db.session.add(row)
db.session.commit()
return {"status": "success"}
product_model = api.model("Product Sales", {
"product": fields.String,
"sale": fields.Integer
})
@api.route("/ProductSales")
class SalesByCategoryAPI(Resource):
@api.marshal_with(product_model, envelope='data')
def get(self, **kwargs):
"""Product Sale
Returns sale by product
"""
return ProductSales.query.all()
@api.expect(product_model, validate=True)
def post(self, **kwargs):
"""Product Sale
Creates an entry in DB of sale for a product
"""
data = request.get_json()
row = ProductSales(**data)
db.session.add(row)
db.session.commit()
return {"status": "success"}