Skip to content

Commit

Permalink
Add get_chest_items and add_item_to_chest endpoints to chest router
Browse files Browse the repository at this point in the history
Implement CRUD functions for chest items in chest/crud.py
  • Loading branch information
skyface753 committed Jan 27, 2024
1 parent fc03bff commit 885af25
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 16 deletions.
Binary file added .coverage
Binary file not shown.
11 changes: 8 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
POSTGRES_USER=postgres1
POSTGRES_PASSWORD=postgres2
POSTGRES_DB=postgres3
DATABASE_HOST=db-dev
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=mysecretpassword
DATABASE_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_DB=postgres
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ migrate-db:
reinit-db: clear-db migrate-db

integration-test:
DATABASE_HOST=localhost \
DATABASE_PORT=5432 \
DATABASE_USERNAME=postgres \
DATABASE_PASSWORD=mysecretpassword \
DATABASE_NAME=postgres \
POSTGRES_USER=postgres \
POSTGRES_PASSWORD=mysecretpassword \
POSTGRES_DB=postgres \
PYTHONPATH=. pytest -x --junitxml=report_integration_tests.xml --cov=app --cov-config=.coveragerc --cov-report=xml:integration_coverage.xml tests/integration/



service-test:
API_SERVER=localhost API_PORT=8000 PYTHONPATH=. pytest --pspec --verbose --color=yes --junitxml=report_service_tests.xml tests/service/
36 changes: 30 additions & 6 deletions app/api/v1/endpoints/chest/crud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
import uuid

from sqlalchemy.orm import Session

from app.database.models import Kiste
from app.api.v1.endpoints.chest.schemas import ChestCreateSchema
from app.database.models import Kiste, ItemKiste, Item
from app.api.v1.endpoints.chest.schemas import ChestCreateSchema, ChestItemCreateSchema, JoinedChestItemSchema

def create_chest(schema: ChestCreateSchema, db: Session):
entity = Kiste(**schema.dict())
Expand All @@ -13,7 +12,7 @@ def create_chest(schema: ChestCreateSchema, db: Session):
logging.debug('Chest {} created'.format(entity.name))
return entity

def get_chest_by_id(chest_id: uuid.UUID, db: Session):
def get_chest_by_id(chest_id: int, db: Session):
entity = db.query(Kiste).filter(Kiste.id == chest_id).first()
return entity

Expand All @@ -33,11 +32,36 @@ def update_chest(chest: Kiste, changed_chest: ChestCreateSchema, db: Session):
logging.debug('Chest {} updated'.format(chest.name))
return chest

def delete_chest_by_id(chest_id: uuid.UUID, db: Session):
def delete_chest_by_id(chest_id: int, db: Session):
entity = get_chest_by_id(chest_id, db)
if entity:
db.delete(entity)
db.commit()
logging.debug('Chest {} deleted'.format(entity.name))



def get_items_in_chest(chest_id: int, db: Session):
items = db.query(Item).join(ItemKiste).filter(ItemKiste.kiste_id == chest_id).all()
return items

def get_specific_item_in_chest(chest_id: int, item_id: int, db: Session):
item = db.query(ItemKiste).filter(ItemKiste.kiste_id == chest_id, ItemKiste.item_id == item_id).first()
return item

def get_joined_items_by_chest_id(chest_id: int, db: Session):
items = db.query(ItemKiste).join(Item).filter(ItemKiste.kiste_id == chest_id).all()
return items

def add_item_to_chest(chest_id: int, item_id: int, quantity: int, db: Session):
item = get_specific_item_in_chest(chest_id, item_id, db)
if item:
item.anzahl += quantity
db.commit()
logging.debug('Item {} added to chest {}'.format(item_id, chest_id))
return item
else:
item = ItemKiste(kiste_id=chest_id, item_id=item_id, anzahl=quantity)
db.add(item)
db.commit()
logging.debug('Item {} added to chest {}'.format(item_id, chest_id))
return item
40 changes: 38 additions & 2 deletions app/api/v1/endpoints/chest/router.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import logging

from typing import List
from typing import List, TypeVar

from fastapi import APIRouter, Depends, Request, Response, status, HTTPException
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session

import app.api.v1.endpoints.chest.crud as chest_crud
from app.api.v1.endpoints.chest.schemas import ChestSchema, ChestCreateSchema, ChestListItemSchema
import app.api.v1.endpoints.item.crud as item_crud
from app.api.v1.endpoints.chest.schemas import ChestSchema, ChestCreateSchema, ChestListItemSchema, JoinedChestItemSchema, ChestItemCreateSchema
from app.database.connection import SessionLocal

router = APIRouter()
Expand Down Expand Up @@ -98,3 +99,38 @@ def delete_chest(chest_id: int, db: Session = Depends(get_db)):
chest_crud.delete_chest_by_id(chest_id, db)
logging.info('Chest {} deleted'.format(chest.name))
return Response(status_code=status.HTTP_204_NO_CONTENT)



@router.get('/{chest_id}/items',
# response_model=JoinedChestItemSchema,
tags=['chest'])
def get_chest_items(chest_id: int, db: Session = Depends(get_db)):
chest = chest_crud.get_chest_by_id(chest_id, db)

if not chest:
logging.error('Get: Chest {} not found'.format(chest_id))
raise HTTPException(status_code=404)

items = chest_crud.get_joined_items_by_chest_id(chest_id, db)
chest.items = items
return chest

@router.post('/{chest_id}/items', response_model=ChestItemCreateSchema,
status_code=status.HTTP_201_CREATED, tags=['chest'])
def add_item_to_chest(chest_id: int, item: ChestItemCreateSchema, db: Session = Depends(get_db)):
chest = chest_crud.get_chest_by_id(chest_id, db)

if not chest:
logging.error('Post: Chest {} not found'.format(chest_id))
raise HTTPException(status_code=404)


item_entity = item_crud.get_item_by_id(item.item_id, db)
if not item_entity:
logging.error('Post: Item {} not found'.format(item.item_id))
raise HTTPException(status_code=404)

chest_crud.add_item_to_chest(chest_id, item.item_id, item.anzahl, db)
logging.info('Item {} added to chest {}'.format(item.item_id, chest_id))
return item
16 changes: 14 additions & 2 deletions app/api/v1/endpoints/chest/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from pydantic import BaseModel

from app.api.v1.endpoints.item.schemas import ItemBaseSchema

class ChestBaseSchema(BaseModel):
name: str
Expand All @@ -22,4 +22,16 @@ class ChestListItemSchema(BaseModel):
name: str

class Config:
orm_mode = True
orm_mode = True

class ChestItemQuantityBaseSchema(BaseModel):
anzahl: int

class Config:
orm_mode = True

class ChestItemCreateSchema(ChestItemQuantityBaseSchema):
item_id: int

class JoinedChestItemSchema(ChestBaseSchema, ItemBaseSchema):
pass
Loading

0 comments on commit 885af25

Please sign in to comment.