22from flask .views import MethodView
33
44from . import v1
5- # from model.subscription import Plan
5+ from model .subscription import Plan
6+
67from 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" ))
0 commit comments