Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, HTTPException, Depends | |
| from firebase_admin import firestore | |
| from pydantic import BaseModel, Field | |
| from typing import List | |
| import time | |
| router = APIRouter(prefix="/v1/community", tags=["Community"]) | |
| # ---------- DB Helper (lazy init, safe) ---------- | |
| def get_db(): | |
| try: | |
| return firestore.client() | |
| except Exception: | |
| raise HTTPException(status_code=500, detail="Database not initialized") | |
| # ---------- Schemas (important for API selling & docs) ---------- | |
| class BrandBriefCreate(BaseModel): | |
| brand_id: str = Field(..., min_length=1) | |
| budget: int = Field(..., gt=0) | |
| requirements: str = Field(..., min_length=5) | |
| class ReviewCreate(BaseModel): | |
| from_id: str | |
| to_id: str | |
| rating: int = Field(..., ge=1, le=5) | |
| comment: str = Field(..., min_length=3) | |
| # ---------- 1. GET ALL BRIEFS (Marketplace feed) ---------- | |
| async def get_all_briefs(): | |
| db = get_db() | |
| try: | |
| docs = ( | |
| db.collection("community_board") | |
| .order_by("timestamp", direction=firestore.Query.DESCENDING) | |
| .limit(50) | |
| .stream() | |
| ) | |
| briefs = [] | |
| for doc in docs: | |
| data = doc.to_dict() | |
| data["id"] = doc.id | |
| briefs.append(data) | |
| return { | |
| "status": "success", | |
| "count": len(briefs), | |
| "data": briefs, | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # ---------- 2. BRAND POSTING (Paid action) ---------- | |
| async def post_brief(payload: BrandBriefCreate): | |
| db = get_db() | |
| FIXED_FEE_USD = 2.00 # ๐ business rule (easy to change later) | |
| brief_data = { | |
| "brand_id": payload.brand_id, | |
| "budget": payload.budget, | |
| "requirements": payload.requirements, | |
| "fee_paid": FIXED_FEE_USD, | |
| "status": "active", | |
| "timestamp": firestore.SERVER_TIMESTAMP, | |
| } | |
| try: | |
| doc_ref = db.collection("community_board").add(brief_data) | |
| return { | |
| "status": "success", | |
| "message": "Brief posted successfully. Processing fee applied.", | |
| "doc_id": doc_ref[1].id, | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # ---------- 3. FEEDBACK & REVIEW SYSTEM ---------- | |
| async def leave_review(payload: ReviewCreate): | |
| db = get_db() | |
| review_data = { | |
| "reviewer": payload.from_id, | |
| "target": payload.to_id, | |
| "rating": payload.rating, | |
| "comment": payload.comment, | |
| "timestamp": firestore.SERVER_TIMESTAMP, | |
| } | |
| try: | |
| db.collection("reviews").add(review_data) | |
| return { | |
| "status": "success", | |
| "message": "Review added successfully", | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |