Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GET /products/id endpoint to get product details #46

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def main_page(request: Request):


@app.post("/auth")
async def authentication(
def authentication(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
db: Session = Depends(get_db),
):
Expand Down Expand Up @@ -134,15 +134,15 @@ async def authentication(


@app.get("/prices", response_model=Page[schemas.PriceBase])
async def get_price(
def get_price(
filters: schemas.PriceFilter = FilterDepends(schemas.PriceFilter),
db: Session = Depends(get_db),
):
return paginate(db, crud.get_prices_query(filters=filters))


@app.post("/prices", response_model=schemas.PriceBase)
async def create_price(
def create_price(
price: schemas.PriceCreate,
background_tasks: BackgroundTasks,
current_user: schemas.UserBase = Depends(get_current_user),
Expand Down Expand Up @@ -208,8 +208,19 @@ def get_user_proofs(
return crud.get_user_proofs(db, user=current_user)


@app.get("/products/{product_id}", response_model=schemas.ProductBase)
def get_product(product_id: int, db: Session = Depends(get_db)):
db_product = crud.get_product_by_id(db, id=product_id)
if not db_product:
raise HTTPException(
status_code=404,
detail=f"Product with id {product_id} not found",
)
return db_product


@app.get("/locations/{location_id}", response_model=schemas.LocationBase)
async def get_location(location_id: int, db: Session = Depends(get_db)):
def get_location(location_id: int, db: Session = Depends(get_db)):
db_location = crud.get_location_by_id(db, id=location_id)
if not db_location:
raise HTTPException(
Expand All @@ -220,7 +231,7 @@ async def get_location(location_id: int, db: Session = Depends(get_db)):


@app.get("/status")
async def status_endpoint():
def status_endpoint():
return {"status": "running"}


Expand Down
12 changes: 8 additions & 4 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def delete_user(db: Session, user_id: UserBase):

# Products
# ------------------------------------------------------------------------------
def get_product_by_id(db: Session, id: int):
return db.query(Product).filter(Product.id == id).first()


def get_product_by_code(db: Session, code: str):
return db.query(Product).filter(Product.code == code).first()

Expand Down Expand Up @@ -195,6 +199,10 @@ def create_proof_file(file: UploadFile) -> tuple[str, str]:

# Locations
# ------------------------------------------------------------------------------
def get_location_by_id(db: Session, id: int):
return db.query(Location).filter(Location.id == id).first()


def get_location_by_osm_id_and_type(
db: Session, osm_id: int, osm_type: LocationOSMType
):
Expand All @@ -206,10 +214,6 @@ def get_location_by_osm_id_and_type(
)


def get_location_by_id(db: Session, id: int):
return db.query(Location).filter(Location.id == id).first()


def create_location(db: Session, location: LocationCreate):
db_location = Location(**location.model_dump())
db.add(db_location)
Expand Down
18 changes: 17 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app import crud
from app.api import app, get_db
from app.db import Base
from app.schemas import LocationCreate, PriceCreate, UserBase
from app.schemas import LocationCreate, PriceCreate, ProductCreate, UserBase

# database setup
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -40,6 +40,7 @@ def override_get_db():
client = TestClient(app)

USER = UserBase(user_id="user1", token="user1__Utoken")
PRODUCT = ProductCreate(code="8001505005707")
LOCATION = LocationCreate(osm_id=3344841823, osm_type="NODE")
PRICE_1 = PriceCreate(
product_code="1111111111111",
Expand All @@ -57,6 +58,12 @@ def user(db=override_get_db()):
return db_user


@pytest.fixture(scope="module")
def product(db=override_get_db()):
db_product = crud.create_product(next(db), PRODUCT)
return db_product


@pytest.fixture(scope="module")
def location(db=override_get_db()):
db_location = crud.create_location(next(db), LOCATION)
Expand Down Expand Up @@ -148,6 +155,15 @@ def test_get_proofs(user):
assert response.status_code == 200


def test_get_product(product):
# product exists
response = client.get(f"/products/{product.id}")
assert response.status_code == 200
# product does not exist
response = client.get(f"/products/{product.id+1}")
assert response.status_code == 404


def test_get_location(location):
# location exists
response = client.get(f"/locations/{location.id}")
Expand Down
Loading