bookmyservice-ums / app /models /review_model.py
GayathriArjun's picture
review api changes
aedd440
from fastapi import HTTPException
from app.core.nosql_client import db
import logging
from datetime import datetime
from app.utils.db import prepare_for_db
logger = logging.getLogger(__name__)
class ReviewModel:
collection = db["merchant_reviews"]
@staticmethod
async def create_review(
review_data:dict
)->dict:
logger.info(f"Creating review for merchant {review_data['merchant_id']}")
try:
# Creating review document
review_doc = {
"merchant_id":review_data["merchant_id"],
"location_id": review_data["location_id"],
"user_name":review_data["user_name"],
"rating": review_data[ "rating"],
"review_text":review_data["review_text"],
"review_date": datetime.utcnow(),
"verified_purchase":review_data["verified_purchase"]
}
sanitized_review = prepare_for_db(review_doc)
result = await ReviewModel.collection.insert_one(sanitized_review)
logger.info(f"review data inserted successfully: {result}")
if result.inserted_id:
return review_doc
except HTTPException:
raise
except Exception as e:
logger.error(f"Error while adding review: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to add review details")