-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from DhafinFK/feat/review
feat: create review
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import html | ||
from flask import ( | ||
Blueprint, | ||
jsonify, | ||
request | ||
) | ||
|
||
from datetime import datetime | ||
from app.decorators import require_jwt_token, require_same_user_id | ||
from app.jwt_utils import decode_token | ||
from models.user import User | ||
from models.review import Review | ||
from app.utils import get_user_id, get_app_config | ||
|
||
router_review = Blueprint('router_review', __name__) | ||
|
||
""" | ||
Basic ping / status check | ||
""" | ||
@router_review.route('/review', methods=['GET']) | ||
def status(): | ||
return (jsonify({ | ||
"message": "review feature is up", | ||
}), 200) | ||
|
||
|
||
@router_review.route('/review/<user_id>', methods=['POST']) | ||
@require_jwt_token | ||
@require_same_user_id | ||
def create_review(user_id): | ||
data = request.json | ||
rating = data.get('rating') | ||
comment = data.get('comment') | ||
|
||
if not(1 <= rating <= 5): | ||
return (jsonify({'message': 'Rating must be between 1 and 5.'}), 400) | ||
|
||
user = User.objects(id=user_id).first() | ||
if not user: | ||
return (jsonify({'message': 'User not found.'}), 400) | ||
|
||
review = Review( | ||
user=user, | ||
rating=rating, | ||
comment=comment | ||
) | ||
review.save() | ||
|
||
return (jsonify({ | ||
'message': 'Review saved' | ||
}), 201) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoengine as mongo | ||
from datetime import datetime | ||
from .user import User | ||
|
||
class Review(mongo.Document): | ||
user = mongo.ReferenceField("User", required=True) | ||
rating = mongo.IntField(min_value=1, max_value=5, required=True) | ||
comment = mongo.StringField(max_length=1000) | ||
created_at = mongo.DateTimeField(default=datetime.utcnow) | ||
reviewed = mongo.BooleanField(default=False) | ||
|
||
|
||
def serialize(self): | ||
return { | ||
"user_id": str(self.user.id), | ||
"user_name": str(self.user.name), | ||
"rating": self.rating, | ||
"comment": self.comment, | ||
"created_at": self.created_at.isoformat(), | ||
"reviewed": self.reviewed | ||
} |