Spaces:
Sleeping
Sleeping
Create app/api/v1/endpoints/dictionary.py
Browse files
app/api/v1/endpoints/dictionary.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
from app.db.session import SessionLocal
|
| 6 |
+
from app.schemas.dictionary_entry import DictionaryEntry, DictionaryEntryCreate
|
| 7 |
+
from app.services.dictionary_service import search_entries, get_entry, create_entry
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_db():
|
| 13 |
+
db = SessionLocal()
|
| 14 |
+
try:
|
| 15 |
+
yield db
|
| 16 |
+
finally:
|
| 17 |
+
db.close()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@router.get("/entries", response_model=List[DictionaryEntry])
|
| 21 |
+
def search(query: str = "", db: Session = Depends(get_db)):
|
| 22 |
+
return search_entries(db, query)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@router.get("/entries/{entry_id}", response_model=DictionaryEntry)
|
| 26 |
+
def read_entry(entry_id: int, db: Session = Depends(get_db)):
|
| 27 |
+
entry = get_entry(db, entry_id)
|
| 28 |
+
if not entry:
|
| 29 |
+
raise HTTPException(status_code=404, detail="Entry not found")
|
| 30 |
+
return entry
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@router.post("/entries", response_model=DictionaryEntry, status_code=201)
|
| 34 |
+
def create(data: DictionaryEntryCreate, db: Session = Depends(get_db)):
|
| 35 |
+
return create_entry(db, data)
|