From 816c9fe5c9369e8bafa13ac8c8730791bc215b8d Mon Sep 17 00:00:00 2001 From: DhafinFK Date: Thu, 1 Aug 2024 21:18:06 +0700 Subject: [PATCH] feat: create review --- app/__init__.py | 4 +++- app/views/review.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ models/review.py | 21 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 app/views/review.py create mode 100644 models/review.py diff --git a/app/__init__.py b/app/__init__.py index a75eb38..1407059 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 @@ -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) diff --git a/app/views/review.py b/app/views/review.py new file mode 100644 index 0000000..bc3e545 --- /dev/null +++ b/app/views/review.py @@ -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/', 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) \ No newline at end of file diff --git a/models/review.py b/models/review.py new file mode 100644 index 0000000..955480b --- /dev/null +++ b/models/review.py @@ -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 + } \ No newline at end of file