eishit32 commited on
Commit
5e5973c
·
verified ·
1 Parent(s): a8df853

Create community.py

Browse files
Files changed (1) hide show
  1. community.py +110 -0
community.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, HTTPException, Depends
2
+ from firebase_admin import firestore
3
+ from pydantic import BaseModel, Field
4
+ from typing import List
5
+ import time
6
+
7
+ router = APIRouter(prefix="/v1/community", tags=["Community"])
8
+
9
+
10
+ # ---------- DB Helper (lazy init, safe) ----------
11
+ def get_db():
12
+ try:
13
+ return firestore.client()
14
+ except Exception:
15
+ raise HTTPException(status_code=500, detail="Database not initialized")
16
+
17
+
18
+ # ---------- Schemas (important for API selling & docs) ----------
19
+ class BrandBriefCreate(BaseModel):
20
+ brand_id: str = Field(..., min_length=1)
21
+ budget: int = Field(..., gt=0)
22
+ requirements: str = Field(..., min_length=5)
23
+
24
+
25
+ class ReviewCreate(BaseModel):
26
+ from_id: str
27
+ to_id: str
28
+ rating: int = Field(..., ge=1, le=5)
29
+ comment: str = Field(..., min_length=3)
30
+
31
+
32
+ # ---------- 1. GET ALL BRIEFS (Marketplace feed) ----------
33
+ @router.get("/brand/briefs")
34
+ async def get_all_briefs():
35
+ db = get_db()
36
+ try:
37
+ docs = (
38
+ db.collection("community_board")
39
+ .order_by("timestamp", direction=firestore.Query.DESCENDING)
40
+ .limit(50)
41
+ .stream()
42
+ )
43
+
44
+ briefs = []
45
+ for doc in docs:
46
+ data = doc.to_dict()
47
+ data["id"] = doc.id
48
+ briefs.append(data)
49
+
50
+ return {
51
+ "status": "success",
52
+ "count": len(briefs),
53
+ "data": briefs,
54
+ }
55
+
56
+ except Exception as e:
57
+ raise HTTPException(status_code=500, detail=str(e))
58
+
59
+
60
+ # ---------- 2. BRAND POSTING (Paid action) ----------
61
+ @router.post("/brand/post-brief")
62
+ async def post_brief(payload: BrandBriefCreate):
63
+ db = get_db()
64
+
65
+ FIXED_FEE_USD = 2.00 # 🔒 business rule (easy to change later)
66
+
67
+ brief_data = {
68
+ "brand_id": payload.brand_id,
69
+ "budget": payload.budget,
70
+ "requirements": payload.requirements,
71
+ "fee_paid": FIXED_FEE_USD,
72
+ "status": "active",
73
+ "timestamp": firestore.SERVER_TIMESTAMP,
74
+ }
75
+
76
+ try:
77
+ doc_ref = db.collection("community_board").add(brief_data)
78
+
79
+ return {
80
+ "status": "success",
81
+ "message": "Brief posted successfully. Processing fee applied.",
82
+ "doc_id": doc_ref[1].id,
83
+ }
84
+
85
+ except Exception as e:
86
+ raise HTTPException(status_code=500, detail=str(e))
87
+
88
+
89
+ # ---------- 3. FEEDBACK & REVIEW SYSTEM ----------
90
+ @router.post("/review")
91
+ async def leave_review(payload: ReviewCreate):
92
+ db = get_db()
93
+
94
+ review_data = {
95
+ "reviewer": payload.from_id,
96
+ "target": payload.to_id,
97
+ "rating": payload.rating,
98
+ "comment": payload.comment,
99
+ "timestamp": firestore.SERVER_TIMESTAMP,
100
+ }
101
+
102
+ try:
103
+ db.collection("reviews").add(review_data)
104
+ return {
105
+ "status": "success",
106
+ "message": "Review added successfully",
107
+ }
108
+
109
+ except Exception as e:
110
+ raise HTTPException(status_code=500, detail=str(e))