Files
Brancheneinstufung2/company-explorer/backend/app.py

65 lines
2.3 KiB
Python

from fastapi import FastAPI, Depends, HTTPException, Query, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session, joinedload
from typing import List, Optional
from .config import settings
from .lib.logging_setup import setup_logging
setup_logging()
import logging
logger = logging.getLogger(__name__)
from .database import init_db, get_db, Company, Industry, RoboticsCategory
# App Init
app = FastAPI(title=settings.APP_NAME, version=settings.VERSION, root_path="/ce")
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
@app.on_event("startup")
def on_startup():
init_db()
@app.get("/api/health")
def health_check():
return {"status": "ok"}
@app.get("/api/companies/{company_id}")
def get_company(company_id: int, db: Session = Depends(get_db)):
company = db.query(Company).options(
joinedload(Company.enrichment_data),
joinedload(Company.contacts)
).filter(Company.id == company_id).first()
if not company:
raise HTTPException(404, detail="Company not found")
industry_details = None
if company.industry_ai:
ind = db.query(Industry).options(
joinedload(Industry.primary_category),
joinedload(Industry.secondary_category)
).filter(Industry.name == company.industry_ai).first()
if ind:
industry_details = {
"pains": ind.pains,
"gains": ind.gains,
"priority": ind.priority,
"notes": ind.notes,
"ops_focus_secondary": ind.ops_focus_secondary,
"primary_product_name": ind.primary_category.name if ind.primary_category else None,
"secondary_product_name": ind.secondary_category.name if ind.secondary_category else None
}
# Manually build response to include extra field
company_data = {c.name: getattr(company, c.name) for c in company.__table__.columns}
company_data['enrichment_data'] = company.enrichment_data
company_data['contacts'] = company.contacts
company_data['industry_details'] = industry_details
return company_data
# Other routes omitted for brevity in this restore
@app.get("/api/industries")
def list_industries(db: Session = Depends(get_db)):
return db.query(Industry).all()