-
Notifications
You must be signed in to change notification settings - Fork 79
/
account.py
71 lines (52 loc) · 1.81 KB
/
account.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
from flask import Blueprint, current_app, request, jsonify
from flask_jwt_extended import current_user, jwt_required
from api.dao.favorites import FavoriteDAO
from api.dao.ratings import RatingDAO
account_routes = Blueprint("account", __name__, url_prefix="/api/account")
@account_routes.route('/', methods=['GET'])
@jwt_required()
def get_profile():
return jsonify(current_user)
@account_routes.route('/favorites', methods=['GET'])
@jwt_required()
def get_favorites():
# Get user ID from JWT
user_id = current_user["sub"]
# Get search parameters
sort = request.args.get("sort", "title")
order = request.args.get("order", "ASC")
limit = request.args.get("limit", 6, type=int)
skip = request.args.get("skip", 0, type=int)
# Create the DAO
dao = FavoriteDAO(current_app.driver)
output = dao.all(user_id, sort, order, limit, skip)
return jsonify(output)
@account_routes.route('/favorites/<movie_id>', methods=['POST', 'DELETE'])
@jwt_required()
def add_favorite(movie_id):
# Get user ID from JWT
user_id = current_user["sub"]
# Create the DAO
dao = FavoriteDAO(current_app.driver)
if request.method == "POST":
# Save the favorite
output = dao.add(user_id, movie_id)
else:
# Remove the favorite
output = dao.remove(user_id, movie_id)
# Return the output
return jsonify(output)
@account_routes.route('/ratings/<movie_id>', methods=['POST'])
@jwt_required()
def save_rating(movie_id):
# Get user ID from JWT
user_id = current_user["sub"]
# Get rating from Request
form_data = request.get_json()
rating = int(form_data["rating"])
# Create the DAO
dao = RatingDAO(current_app.driver)
# Save the rating
output = dao.add(user_id, movie_id, rating)
# Return the output
return jsonify(output)