from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List import secrets from .. import database, models, schemas, auth_utils router = APIRouter( prefix="/apps", tags=["Applications"] ) @router.post("/", response_model=schemas.ApplicationOut, dependencies=[Depends(auth_utils.get_current_admin_user)]) async def create_application(app: schemas.ApplicationCreate, db: Session = Depends(database.get_db)): db_app = db.query(models.Application).filter(models.Application.name == app.name).first() if db_app: raise HTTPException(status_code=400, detail="Application already exists") api_key = secrets.token_urlsafe(32) db_app = models.Application( name=app.name, url=app.url, api_key=api_key ) db.add(db_app) db.commit() db.refresh(db_app) return db_app @router.get("/", response_model=List[schemas.ApplicationOut], dependencies=[Depends(auth_utils.get_current_admin_user)]) async def read_applications(skip: int = 0, limit: int = 100, db: Session = Depends(database.get_db)): apps = db.query(models.Application).offset(skip).limit(limit).all() return apps @router.get("/public", response_model=List[schemas.ApplicationOut]) async def read_public_applications(db: Session = Depends(database.get_db)): apps = db.query(models.Application).all() return apps