Spaces:
Sleeping
Sleeping
Commit ·
aedd440
1
Parent(s): f5367b2
review api changes
Browse files- app/app.py +2 -1
- app/models/review_model.py +45 -0
- app/routers/review_router.py +48 -0
- app/schemas/review_schema.py +24 -0
app/app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from app.routers import user_router, profile_router, account_router, wallet_router, address_router, pet_router, guest_router, favorite_router
|
| 6 |
from app.middleware.rate_limiter import RateLimitMiddleware
|
| 7 |
from app.middleware.security_middleware import SecurityMiddleware
|
| 8 |
import logging
|
|
@@ -54,6 +54,7 @@ app.include_router(address_router.router, prefix="/addresses", tags=["address_ma
|
|
| 54 |
app.include_router(pet_router.router, prefix="/api/v1/users", tags=["pet_management"])
|
| 55 |
app.include_router(guest_router.router, prefix="/api/v1/users", tags=["guest_management"])
|
| 56 |
app.include_router(favorite_router.router, prefix="/api/v1/users", tags=["favorites"])
|
|
|
|
| 57 |
|
| 58 |
@app.get("/")
|
| 59 |
def root():
|
|
|
|
| 2 |
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from app.routers import user_router, profile_router, account_router, wallet_router, address_router, pet_router, guest_router, favorite_router, review_router
|
| 6 |
from app.middleware.rate_limiter import RateLimitMiddleware
|
| 7 |
from app.middleware.security_middleware import SecurityMiddleware
|
| 8 |
import logging
|
|
|
|
| 54 |
app.include_router(pet_router.router, prefix="/api/v1/users", tags=["pet_management"])
|
| 55 |
app.include_router(guest_router.router, prefix="/api/v1/users", tags=["guest_management"])
|
| 56 |
app.include_router(favorite_router.router, prefix="/api/v1/users", tags=["favorites"])
|
| 57 |
+
app.include_router(review_router.router,prefix="/review",tags=["reviews"])
|
| 58 |
|
| 59 |
@app.get("/")
|
| 60 |
def root():
|
app/models/review_model.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import HTTPException
|
| 2 |
+
from app.core.nosql_client import db
|
| 3 |
+
import logging
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
from app.utils.db import prepare_for_db
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class ReviewModel:
|
| 11 |
+
collection = db["merchant_reviews"]
|
| 12 |
+
|
| 13 |
+
@staticmethod
|
| 14 |
+
async def create_review(
|
| 15 |
+
review_data:dict
|
| 16 |
+
)->dict:
|
| 17 |
+
logger.info(f"Creating review for merchant {review_data['merchant_id']}")
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
|
| 21 |
+
# Creating review document
|
| 22 |
+
review_doc = {
|
| 23 |
+
"merchant_id":review_data["merchant_id"],
|
| 24 |
+
"location_id": review_data["location_id"],
|
| 25 |
+
"user_name":review_data["user_name"],
|
| 26 |
+
"rating": review_data[ "rating"],
|
| 27 |
+
"review_text":review_data["review_text"],
|
| 28 |
+
"review_date": datetime.utcnow(),
|
| 29 |
+
"verified_purchase":review_data["verified_purchase"]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
sanitized_review = prepare_for_db(review_doc)
|
| 33 |
+
|
| 34 |
+
result = await ReviewModel.collection.insert_one(sanitized_review)
|
| 35 |
+
|
| 36 |
+
logger.info(f"review data inserted successfully: {result}")
|
| 37 |
+
|
| 38 |
+
if result.inserted_id:
|
| 39 |
+
return review_doc
|
| 40 |
+
|
| 41 |
+
except HTTPException:
|
| 42 |
+
raise
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.error(f"Error while adding review: {str(e)}", exc_info=True)
|
| 45 |
+
raise HTTPException(status_code=500, detail="Failed to add review details")
|
app/routers/review_router.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
import logging
|
| 3 |
+
from app.utils.jwt import get_current_user
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
|
| 6 |
+
from app.schemas.review_schema import ReviewCreateRequest,ReviewResponse
|
| 7 |
+
from app.models.review_model import ReviewModel
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
router = APIRouter()
|
| 12 |
+
|
| 13 |
+
@router.post("/add", response_model=ReviewResponse,
|
| 14 |
+
status_code=status.HTTP_201_CREATED)
|
| 15 |
+
async def create_review(
|
| 16 |
+
review_data: ReviewCreateRequest,
|
| 17 |
+
current_user: Dict[str, Any] = Depends(get_current_user)
|
| 18 |
+
):
|
| 19 |
+
try:
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
review_reponse=await ReviewModel.create_review(review_data.dict())
|
| 23 |
+
|
| 24 |
+
if not review_reponse:
|
| 25 |
+
raise HTTPException(
|
| 26 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 27 |
+
detail="failed to add review details"
|
| 28 |
+
)
|
| 29 |
+
return ReviewResponse(
|
| 30 |
+
merchant_id=review_reponse["merchant_id"],
|
| 31 |
+
location_id=review_reponse["location_id"],
|
| 32 |
+
user_name=review_reponse["user_name"],
|
| 33 |
+
rating=review_reponse["rating"],
|
| 34 |
+
review_text=review_reponse["review_text"],
|
| 35 |
+
review_date=review_reponse["review_date"],
|
| 36 |
+
verified_purchase=review_reponse["verified_purchase"]
|
| 37 |
+
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
except HTTPException:
|
| 41 |
+
raise
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Error while adding review: {str(e)}")
|
| 44 |
+
raise HTTPException(
|
| 45 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 46 |
+
detail="Failed to add review"
|
| 47 |
+
)
|
| 48 |
+
|
app/schemas/review_schema.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from typing import Optional, List
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
class ReviewCreateRequest(BaseModel):
|
| 6 |
+
"""Request schema for creating a review"""
|
| 7 |
+
merchant_id: str = Field(..., description="Unique ID of the merchant")
|
| 8 |
+
location_id: str = Field(..., description="ID of the merchant location")
|
| 9 |
+
user_name: str = Field(..., description="Name of the user submitting the review")
|
| 10 |
+
rating: float = Field(..., description="Rating given by the user")
|
| 11 |
+
review_text: str = Field(..., description="review text provided by the user")
|
| 12 |
+
verified_purchase: bool = Field(..., description="Indicates if the review is from a verified purchase")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class ReviewResponse(BaseModel):
|
| 16 |
+
"""Response schema for creating a review"""
|
| 17 |
+
merchant_id: str = Field(..., description="Unique ID of the merchant")
|
| 18 |
+
location_id: str = Field(..., description="ID of the merchant location")
|
| 19 |
+
user_name: str = Field(..., description="Name of the user submitting the review")
|
| 20 |
+
rating: float = Field(..., description="Rating given by the user")
|
| 21 |
+
review_text: str = Field(..., description="review text provided by the user")
|
| 22 |
+
review_date: datetime = Field(..., description="Date and time when the review was created")
|
| 23 |
+
verified_purchase: bool = Field(..., description="Indicates if the review is from a verified purchase")
|
| 24 |
+
|