mirror of
https://gitlab.aachen.ccc.de/inventory/in.git
synced 2024-11-25 16:53:59 +01:00
make return value of crud.put_item easier to understand
This commit is contained in:
parent
eba9b410c6
commit
99df802a40
2 changed files with 11 additions and 2 deletions
11
crud.py
11
crud.py
|
@ -1,3 +1,4 @@
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
from sqlalchemy import DECIMAL, cast
|
from sqlalchemy import DECIMAL, cast
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
@ -17,7 +18,15 @@ def put_item(db: Session, id: str, item: schemas.Item):
|
||||||
if not updated:
|
if not updated:
|
||||||
db.add(models.Item(**item.dict()))
|
db.add(models.Item(**item.dict()))
|
||||||
db.commit()
|
db.commit()
|
||||||
return updated
|
if updated:
|
||||||
|
return PutItemResult.UPDATED
|
||||||
|
else:
|
||||||
|
return PutItemResult.ADDED
|
||||||
|
|
||||||
|
|
||||||
|
class PutItemResult(Enum):
|
||||||
|
ADDED = 1
|
||||||
|
UPDATED = 2
|
||||||
|
|
||||||
|
|
||||||
def delete_item(db: Session, id: str):
|
def delete_item(db: Session, id: str):
|
||||||
|
|
2
main.py
2
main.py
|
@ -44,7 +44,7 @@ async def list_items(db: Session = Depends(get_db)):
|
||||||
|
|
||||||
@app.put("/api/items/{id}")
|
@app.put("/api/items/{id}")
|
||||||
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
|
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
|
||||||
if crud.put_item(db, id, item):
|
if crud.put_item(db, id, item) == crud.PutItemResult.UPDATED:
|
||||||
return Response(b"", status_code=204)
|
return Response(b"", status_code=204)
|
||||||
return Response(b"", status_code=201)
|
return Response(b"", status_code=201)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue