add: optional category filter for items endpoint

v0.2
rrr-marble 4 years ago
parent 7866e245dc
commit 2ef6b3227d

@ -30,7 +30,9 @@ def get_item_by_description(db: Session, needle: str, skip: int = 0, limit: int
result = ( result = (
db.query(models.Item) db.query(models.Item)
.filter( .filter(
items_table.c.geodata_search_ts.op("@@")(func.websearch_to_tsquery('"russian"', needle)) items_table.c.geodata_search_ts.op("@@")(
func.websearch_to_tsquery('"russian"', needle)
)
) )
.order_by(items_table.c.id) .order_by(items_table.c.id)
.offset(skip) .offset(skip)
@ -45,6 +47,17 @@ def get_items(db: Session, skip: int = 0, limit: int = 20):
return db.query(models.Item).offset(skip).limit(limit).all() return db.query(models.Item).offset(skip).limit(limit).all()
def get_items_by_category(category: str, db: Session, skip: int = 0, limit: int = 20):
"""список доступных в категории Описаний наборов данных"""
return (
db.query(models.Item)
.filter(models.Item.category == category)
.offset(skip)
.limit(limit)
.all()
)
def insert_items(db: Session, items: List[schemas.ItemCreate]): def insert_items(db: Session, items: List[schemas.ItemCreate]):
"""импорт в базу данных Описаний наборов данных""" """импорт в базу данных Описаний наборов данных"""
before = db.query(models.Item).count() before = db.query(models.Item).count()

@ -1,6 +1,6 @@
from datetime import datetime from datetime import datetime
from re import IGNORECASE, sub as substitute from re import IGNORECASE, sub as substitute
from typing import List from typing import List, Optional
from urllib.parse import unquote_plus from urllib.parse import unquote_plus
from fastapi import Depends, FastAPI, File, HTTPException, UploadFile from fastapi import Depends, FastAPI, File, HTTPException, UploadFile
@ -106,9 +106,25 @@ def read_item(item_id: int, db: Session = Depends(get_db)):
@app.get("/items/", response_model=List[schemas.Item]) @app.get("/items/", response_model=List[schemas.Item])
def read_items(skip: int = 0, limit: int = 20, db: Session = Depends(get_db)): def read_items(
"""список доступных в системе Описаний наборов данных""" categ: Optional[str] = None,
return crud.get_items(db=db, skip=skip, limit=limit) skip: int = 0,
limit: int = 20,
db: Session = Depends(get_db),
):
"""список доступных в системе Описаний наборов данных
опционально, с указанием конкретной категории
"""
if categ is None or str(categ).strip().lower() in ("any", "all"):
resp = crud.get_items(db=db, skip=skip, limit=limit)
else:
# heuristically adjust query capitalization to fit the spreadsheet form (e.g "Geology")
category = str(categ).strip().capitalize()
resp = crud.get_items_by_category(
category=category, db=db, skip=skip, limit=limit
)
return resp
@app.get( @app.get(

Loading…
Cancel
Save