|
|
|
|
|
from fastapi import FastAPI, HTTPException, APIRouter |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
import crud |
|
|
import schemas |
|
|
from Idiom_Id_Generator import generate_id |
|
|
from dotenv import load_dotenv |
|
|
import os |
|
|
from pydantic import BaseModel |
|
|
from typing import List, Dict, Optional |
|
|
|
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
app = FastAPI(title="Idioms API - Supabase REST") |
|
|
|
|
|
allowed_origins = [ |
|
|
"http://localhost", |
|
|
"http://localhost:3000", |
|
|
"http://localhost:8000", |
|
|
"https://idiomator.vercel.app", |
|
|
"https://www.idiomator.vercel.app", |
|
|
"https://idiomator.com", |
|
|
"https://www.idiomator.com", |
|
|
] |
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins= allowed_origins, |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
@app.get("/idioms", response_model=list[schemas.IdiomResponse]) |
|
|
async def read_idioms(skip: int = 0, limit: int = 50): |
|
|
try: |
|
|
data = await crud.get_idioms(skip=skip, limit=limit) |
|
|
return data |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.post("/idioms", response_model=schemas.IdiomResponse) |
|
|
async def create_idiom(idiom: schemas.IdiomCreate): |
|
|
try: |
|
|
|
|
|
idiom_id = generate_id(idiom.language, idiom.dialect) |
|
|
idiom_dict = idiom.dict() |
|
|
idiom_dict["id"] = idiom_id |
|
|
created = await crud.create_idiom(idiom_dict) |
|
|
return created |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/idioms/all_ids", response_model=List[Dict[str, str]]) |
|
|
async def get_all_idioms_route(): |
|
|
idioms = await crud.get_all_idioms() |
|
|
print(f"Fetched {len(idioms)} idioms") |
|
|
print(f"Sample idioms: {idioms[:3]}") |
|
|
return [ |
|
|
{"id": idiom["id"], "idiom": idiom["idiom"], "language": idiom["language"]} |
|
|
for idiom in idioms |
|
|
] |
|
|
|
|
|
@app.get("/idioms/search", response_model=List[schemas.IdiomResponse]) |
|
|
async def search_idioms( |
|
|
q: Optional[str] = None, |
|
|
language: Optional[str] = None, |
|
|
skip: int = 0, |
|
|
limit: int = 50 |
|
|
): |
|
|
|
|
|
try: |
|
|
results = await crud.search_idioms(query=q, language=language, skip=skip, limit=limit) |
|
|
return results |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
|
|
|
@app.get("/idioms/{idiom_id}", response_model=schemas.IdiomResponse) |
|
|
async def read_idiom(idiom_id: str): |
|
|
try: |
|
|
db_idiom = await crud.get_idiom(idiom_id) |
|
|
if not db_idiom: |
|
|
raise HTTPException(status_code=404, detail="Idiom not found") |
|
|
|
|
|
if isinstance(db_idiom.get("validation_count"), int): |
|
|
db_idiom["validation_count"] = {"count": db_idiom["validation_count"]} |
|
|
return db_idiom |
|
|
except HTTPException: |
|
|
raise |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
|
|
|
|
|
|
@app.patch("/idioms/{idiom_id}", response_model=schemas.IdiomResponse) |
|
|
async def patch_idiom(idiom_id: str, idiom_update: schemas.IdiomBase): |
|
|
try: |
|
|
updated = await crud.update_idiom(idiom_id, idiom_update.dict(exclude_unset=True)) |
|
|
if not updated: |
|
|
raise HTTPException(status_code=404, detail="Idiom not found") |
|
|
return updated |
|
|
except HTTPException: |
|
|
raise |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.delete("/idioms/{idiom_id}") |
|
|
async def delete_idiom(idiom_id: str): |
|
|
try: |
|
|
return await crud.delete_idiom(idiom_id) |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/ping") |
|
|
async def ping(): |
|
|
return {"status": "ok"} |
|
|
|
|
|
|