Álvaro Valenzuela Valdes commited on
Commit ·
0700cbe
1
Parent(s): 5a69335
🐛 Fix: Persistent Company Profile via Database to resolve 404 in production
Browse files- backend/app/routers/company.py +54 -16
backend/app/routers/company.py
CHANGED
|
@@ -1,25 +1,63 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException
|
|
|
|
| 2 |
from app.schemas.company import CompanyProfile
|
| 3 |
-
from app.
|
|
|
|
|
|
|
| 4 |
|
| 5 |
router = APIRouter()
|
| 6 |
|
| 7 |
-
# Load initial profile from disk
|
| 8 |
-
_profiles = load_from_json(CompanyProfile, "company_profile.json")
|
| 9 |
-
company_profile_cache: CompanyProfile | None = _profiles[0] if _profiles else None
|
| 10 |
-
|
| 11 |
-
|
| 12 |
@router.post("/company-profile", response_model=CompanyProfile)
|
| 13 |
-
def save_company_profile(profile: CompanyProfile):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return profile
|
| 19 |
|
| 20 |
-
|
| 21 |
@router.get("/company-profile", response_model=CompanyProfile)
|
| 22 |
-
def get_company_profile():
|
| 23 |
-
|
|
|
|
| 24 |
raise HTTPException(status_code=404, detail="No company profile saved")
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Depends
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
from app.schemas.company import CompanyProfile
|
| 4 |
+
from app.database import get_db
|
| 5 |
+
from app.models.company import CompanyProfileModel
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@router.post("/company-profile", response_model=CompanyProfile)
|
| 11 |
+
def save_company_profile(profile: CompanyProfile, db: Session = Depends(get_db)):
|
| 12 |
+
# Try to find existing profile (assume only one for now)
|
| 13 |
+
db_profile = db.query(CompanyProfileModel).first()
|
| 14 |
+
|
| 15 |
+
if not db_profile:
|
| 16 |
+
db_profile = CompanyProfileModel()
|
| 17 |
+
db.add(db_profile)
|
| 18 |
+
|
| 19 |
+
db_profile.name = profile.name
|
| 20 |
+
db_profile.industry = profile.industry
|
| 21 |
+
db_profile.services = json.dumps(profile.services)
|
| 22 |
+
db_profile.experience = profile.experience
|
| 23 |
+
db_profile.certifications = json.dumps(profile.certifications)
|
| 24 |
+
db_profile.regions = json.dumps(profile.regions)
|
| 25 |
+
db_profile.documents_available = json.dumps(profile.documents_available)
|
| 26 |
+
|
| 27 |
+
db.commit()
|
| 28 |
+
db.refresh(db_profile)
|
| 29 |
return profile
|
| 30 |
|
|
|
|
| 31 |
@router.get("/company-profile", response_model=CompanyProfile)
|
| 32 |
+
def get_company_profile(db: Session = Depends(get_db)):
|
| 33 |
+
db_profile = db.query(CompanyProfileModel).first()
|
| 34 |
+
if not db_profile:
|
| 35 |
raise HTTPException(status_code=404, detail="No company profile saved")
|
| 36 |
+
|
| 37 |
+
# Handle list fields that are stored as JSON strings
|
| 38 |
+
services = []
|
| 39 |
+
certifications = []
|
| 40 |
+
regions = []
|
| 41 |
+
docs = []
|
| 42 |
+
|
| 43 |
+
try: services = json.loads(db_profile.services) if db_profile.services else []
|
| 44 |
+
except: services = [db_profile.services] if db_profile.services else []
|
| 45 |
+
|
| 46 |
+
try: certifications = json.loads(db_profile.certifications) if db_profile.certifications else []
|
| 47 |
+
except: certifications = []
|
| 48 |
+
|
| 49 |
+
try: regions = json.loads(db_profile.regions) if db_profile.regions else []
|
| 50 |
+
except: regions = [db_profile.regions] if db_profile.regions else []
|
| 51 |
+
|
| 52 |
+
try: docs = json.loads(db_profile.documents_available) if db_profile.documents_available else []
|
| 53 |
+
except: docs = [db_profile.documents_available] if db_profile.documents_available else []
|
| 54 |
+
|
| 55 |
+
return CompanyProfile(
|
| 56 |
+
name=db_profile.name,
|
| 57 |
+
industry=db_profile.industry,
|
| 58 |
+
services=services,
|
| 59 |
+
experience=db_profile.experience,
|
| 60 |
+
certifications=certifications,
|
| 61 |
+
regions=regions,
|
| 62 |
+
documents_available=docs
|
| 63 |
+
)
|