Skip to content

Commit 843a6bf

Browse files
author
Shafaf
committed
Improve: Plan api added
1 parent 09eb00b commit 843a6bf

3 files changed

Lines changed: 50 additions & 23 deletions

File tree

api/v1/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
from .views import *
77
from .plan import *
8+
from .subscription import *

api/v1/plan.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,54 @@
22
from flask.views import MethodView
33

44
from . import v1
5-
# from model.subscription import Plan
5+
from model.subscription import Plan
6+
67
from extension import db
78

8-
# test v1 api endpoint.
9-
class Plan(MethodView):
9+
class Plans(MethodView):
1010
def get(self):
11-
return jsonify({"test": "This is plan api"}), 200
11+
plans = Plan.query.all()
12+
plans_data = [
13+
{
14+
"id": plan.id,
15+
"name": plan.name,
16+
"description": plan.description,
17+
"amount": plan.amount,
18+
"intervals": plan.intervals,
19+
"plan_id": plan.plan_id
20+
}
21+
for plan in plans
22+
]
23+
24+
return jsonify(plans_data), 200
1225

1326
def post(self):
1427
req :dict = request.get_json()
15-
username = req.get("name")
16-
if username is None:
17-
return jsonify({"message": "name is required"}), 400
1828

19-
email = req.get("email")
20-
if email is None:
21-
return jsonify({"message": "email is required"}), 400
22-
# user = Plan(
23-
# username=username,
24-
# email=email
25-
# )
29+
plan_name = req.get("name")
30+
plan_amount = req.get("amount")
31+
plan_interval = req.get("interval")
32+
33+
if plan_name is None:
34+
return jsonify({"message": "name is required"}), 400
2635

27-
print("start")
28-
print(username)
29-
print(email)
30-
print("end")
36+
if plan_amount is None:
37+
return jsonify({"message": "amount is required"}), 400
38+
39+
if plan_interval is None:
40+
return jsonify({"message": "interval is required"}), 400
3141

32-
# db.session.add(user)
33-
# db.session.commit()
42+
plan = Plan(
43+
name=plan_name,
44+
description=req.get("description"),
45+
amount=plan_amount,
46+
intervals=plan_interval,
47+
plan_id=req.get("plan_id")
48+
)
3449

35-
print(user.id)
50+
db.session.add(plan)
51+
db.session.commit()
3652

37-
return jsonify({"status": True})
53+
return jsonify({"id": plan.id})
3854

39-
v1.add_url_rule("/plan", view_func=Plan.as_view("plan"))
55+
v1.add_url_rule("/plan", view_func=Plans.as_view("plan"))

api/v1/subscription.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import jsonify
2+
from flask.views import MethodView
3+
4+
from . import v1
5+
6+
class Subscription(MethodView):
7+
def get(self):
8+
return jsonify({"value": "yes, test is working....:)"})
9+
10+
v1.add_url_rule("/subscription", view_func=Subscription.as_view("subscription"))

0 commit comments

Comments
 (0)