bookmyservice-ums / app /services /favorite_service.py
GayathriArjun's picture
updated schema for adding favorite
f275e24
from fastapi import HTTPException, Depends
from typing import Optional
from app.models.favorite_model import BookMyServiceFavoriteModel
from app.schemas.favorite_schema import (
FavoriteCreateRequest,
FavoriteUpdateRequest,
FavoriteResponse,
FavoritesListResponse,
FavoriteStatusResponse,
FavoriteSuccessResponse,
FavoriteDataResponse
)
from app.services.user_service import UserService
import logging
logger = logging.getLogger("favorite_service")
class FavoriteService:
@staticmethod
async def add_favorite(
customer_id: str,
favorite_data: FavoriteCreateRequest
) -> FavoriteSuccessResponse:
"""Add a merchant to user's favorites"""
try:
# Create favorite in database
favorite_merchant_id= await BookMyServiceFavoriteModel.create_favorite(
customer_id,
favorite_data.dict()
)
return FavoriteSuccessResponse(
success=True,
message="Merchant added to favorites successfully",
merchant_id=favorite_merchant_id
)
except HTTPException as e:
raise e
except Exception as e:
logger.error(f"Error adding favorite: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to add favorite")
@staticmethod
async def remove_favorite(customer_id: str, merchant_id: str) -> FavoriteSuccessResponse:
"""Remove a merchant from user's favorites"""
try:
await BookMyServiceFavoriteModel.delete_favorite(customer_id, merchant_id)
return FavoriteSuccessResponse(
success=True,
message="Merchant removed from favorites successfully",
merchant_id=None
)
except HTTPException as e:
raise e
except Exception as e:
logger.error(f"Error removing favorite: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to remove favorite")
@staticmethod
async def get_favorites(
customer_id: str,
limit: int = 50
) -> FavoritesListResponse:
"""Get user's favorite merchants"""
try:
result = await BookMyServiceFavoriteModel.get_favorites(customer_id,limit )
# Convert MongoDB documents to response format
favorites = []
for fav in result["favorites"]:
favorites.append(FavoriteResponse(
merchant_id=fav["merchant_id"],
merchant_category=fav["merchant_category"],
merchant_name=fav["merchant_name"],
source=fav["source"],
added_at=fav["added_at"],
notes=fav.get("notes")
))
return FavoritesListResponse(
favorites=favorites,
total_count=result["total_count"],
limit=result["limit"]
)
except Exception as e:
logger.error(f"Error getting favorites: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to get favorites")
@staticmethod
async def check_favorite_status(customer_id: str, merchant_id: str) -> FavoriteStatusResponse:
"""Check if a merchant is in user's favorites"""
try:
favorite_data = await BookMyServiceFavoriteModel.get_favorite(customer_id, merchant_id)
return FavoriteStatusResponse(
is_favorite=bool(favorite_data),
merchant_id=merchant_id
)
except Exception as e:
logger.error(f"Error checking favorite status: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to check favorite status")
@staticmethod
async def update_favorite_notes(
customer_id: str,
merchant_id: str,
notes_data: FavoriteUpdateRequest
) -> FavoriteSuccessResponse:
"""Update notes for a favorite merchant"""
try:
await BookMyServiceFavoriteModel.update_favorite_notes(
customer_id=customer_id,
merchant_id=merchant_id,
notes=notes_data.notes
)
return FavoriteSuccessResponse(
success=True,
message="Favorite notes updated successfully",
merchant_id=merchant_id
)
except HTTPException as e:
raise e
except Exception as e:
logger.error(f"Error updating favorite notes: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to update favorite notes")
@staticmethod
async def get_favorite_details(customer_id: str, merchant_id: str)->FavoriteDataResponse:
"""Get detailed information about a specific favorite"""
try:
favorite = await BookMyServiceFavoriteModel.get_favorite(customer_id, merchant_id)
if favorite:
return FavoriteDataResponse(
success=True,
message="Favorite found successfully",
favorite_data=favorite
)
else:
return FavoriteDataResponse(
success=False,
message="Favorite not found",
favorite_data=None
)
except HTTPException as e:
raise e
except Exception as e:
logger.error(f"Error getting favorite details: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to get favorite details")