Skip to content

Commit

Permalink
Merge pull request #37 from DhafinFK/feat/review
Browse files Browse the repository at this point in the history
feat: create review
  • Loading branch information
Veivel authored Aug 2, 2024
2 parents 28f69b8 + 816c9fe commit ce42e79
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
from app.services.scrapper.schedule_scrapper import ScheduleScrapperServices
from app.views.auth import router_auth
from app.views.main import router_main
from app.views.review import router_review
from app.cron import cron
from uploader.views import router_uploader

from pathlib import Path

load_dotenv()
load_dotenv(override=True)

base_dir = Path(__file__).resolve().parent.parent

Expand Down Expand Up @@ -84,6 +85,7 @@
app.register_blueprint(router_auth, url_prefix=app.config["BASE_PATH"])
app.register_blueprint(router_main, url_prefix=app.config["BASE_PATH"])
app.register_blueprint(router_uploader, url_prefix=app.config["BASE_PATH"])
app.register_blueprint(router_review, url_prefix=app.config["BASE_PATH"])
app.register_blueprint(cron)

CORS(app)
Expand Down
51 changes: 51 additions & 0 deletions app/views/review.py
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)
21 changes: 21 additions & 0 deletions models/review.py
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
}

0 comments on commit ce42e79

Please sign in to comment.