|
| 1 | +from typing import List |
| 2 | +from uuid import UUID |
| 3 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 4 | +from pydantic import BaseModel |
| 5 | + |
| 6 | +from helpers.jwt import auth_required |
| 7 | +from models.entity.user_entity import User |
| 8 | +from models.service.store_service import StoreService |
| 9 | + |
| 10 | +store_apis = APIRouter(prefix="/stores", tags=["stores"]) |
| 11 | + |
| 12 | + |
| 13 | +class StoreCreate(BaseModel): |
| 14 | + name: str |
| 15 | + address: str |
| 16 | + phone: str |
| 17 | + email: str |
| 18 | + website: str = None |
| 19 | + |
| 20 | + |
| 21 | +class StoreUpdate(BaseModel): |
| 22 | + name: str = None |
| 23 | + address: str = None |
| 24 | + phone: str = None |
| 25 | + email: str = None |
| 26 | + website: str = None |
| 27 | + |
| 28 | + |
| 29 | +@store_apis.post("/", status_code=status.HTTP_201_CREATED) |
| 30 | +async def create_store( |
| 31 | + store_data: StoreCreate, current_user: User = Depends(auth_required) |
| 32 | +): |
| 33 | + store_service = StoreService() |
| 34 | + return store_service.create_store(user_id=current_user.id, **store_data.dict()) |
| 35 | + |
| 36 | + |
| 37 | +@store_apis.get("/{store_id}") |
| 38 | +async def get_store(store_id: UUID, current_user: User = Depends(auth_required)): |
| 39 | + store_service = StoreService() |
| 40 | + return store_service.get_store(store_id) |
| 41 | + |
| 42 | + |
| 43 | +@store_apis.get("/") |
| 44 | +async def get_user_stores(current_user: User = Depends(auth_required)): |
| 45 | + store_service = StoreService() |
| 46 | + return store_service.get_stores_by_user(current_user.id) |
| 47 | + |
| 48 | + |
| 49 | +@store_apis.put("/{store_id}") |
| 50 | +async def update_store( |
| 51 | + store_id: UUID, store_data: StoreUpdate, current_user: User = Depends(auth_required) |
| 52 | +): |
| 53 | + store_service = StoreService() |
| 54 | + store = store_service.get_store(store_id) |
| 55 | + if store.user_id != current_user.id: |
| 56 | + raise HTTPException( |
| 57 | + status_code=status.HTTP_403_FORBIDDEN, |
| 58 | + detail="Not authorized to update this store", |
| 59 | + ) |
| 60 | + return store_service.update_store(store_id, **store_data.dict(exclude_unset=True)) |
| 61 | + |
| 62 | + |
| 63 | +@store_apis.delete("/{store_id}") |
| 64 | +async def delete_store(store_id: UUID, current_user: User = Depends(auth_required)): |
| 65 | + store_service = StoreService() |
| 66 | + store = store_service.get_store(store_id) |
| 67 | + if store.user_id != current_user.id: |
| 68 | + raise HTTPException( |
| 69 | + status_code=status.HTTP_403_FORBIDDEN, |
| 70 | + detail="Not authorized to delete this store", |
| 71 | + ) |
| 72 | + return store_service.delete_store(store_id) |
| 73 | + |
| 74 | + |
| 75 | +@store_apis.post("/{store_id}/deactivate") |
| 76 | +async def deactivate_store(store_id: UUID, current_user: User = Depends(auth_required)): |
| 77 | + store_service = StoreService() |
| 78 | + store = store_service.get_store(store_id) |
| 79 | + if store.user_id != current_user.id: |
| 80 | + raise HTTPException( |
| 81 | + status_code=status.HTTP_403_FORBIDDEN, |
| 82 | + detail="Not authorized to deactivate this store", |
| 83 | + ) |
| 84 | + return store_service.deactivate_store(store_id) |
| 85 | + |
| 86 | + |
| 87 | +@store_apis.post("/{store_id}/activate") |
| 88 | +async def activate_store(store_id: UUID, current_user: User = Depends(auth_required)): |
| 89 | + store_service = StoreService() |
| 90 | + store = store_service.get_store(store_id) |
| 91 | + if store.user_id != current_user.id: |
| 92 | + raise HTTPException( |
| 93 | + status_code=status.HTTP_403_FORBIDDEN, |
| 94 | + detail="Not authorized to activate this store", |
| 95 | + ) |
| 96 | + return store_service.activate_store(store_id) |
| 97 | + |
| 98 | + |
| 99 | +@store_apis.get("/search/{query}") |
| 100 | +async def search_stores(query: str, current_user: User = Depends(auth_required)): |
| 101 | + store_service = StoreService() |
| 102 | + return store_service.search_stores(query) |
0 commit comments