Skip to content
Open
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
8 changes: 0 additions & 8 deletions .env.example

This file was deleted.

40 changes: 28 additions & 12 deletions app/routers/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
status_code=status.HTTP_200_OK
)
async def list_places(db: Session = Depends(get_db)):
"""Get all places."""
# TODO: Query DB and return list
return []
return db.query(Place).all()

@router.post(
"/",
Expand All @@ -25,33 +23,51 @@ async def list_places(db: Session = Depends(get_db)):
status_code=status.HTTP_201_CREATED
)
async def create_place(place: PlaceCreate, db: Session = Depends(get_db)):
"""Create a new place."""
# TODO: Create and save to DB
raise NotImplementedError("TODO: Implement place creation")
new_place = Place(**place.dict())
db.add(new_place)
db.commit()
db.refresh(new_place)
return new_place

@router.get(
"/{place_id}",
summary="Get a place by ID",
response_model=PlaceResponse
)
async def get_place(place_id: int, db: Session = Depends(get_db)):
# TODO
raise NotImplementedError("TODO: Implement get place")
place = db.query(Place).filter(Place.id == place_id).first()
if not place:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Place not found")
return place

@router.patch(
"/{place_id}",
summary="Update a place",
response_model=PlaceResponse
)
async def update_place(place_id: int, place: PlaceUpdate, db: Session = Depends(get_db)):
# TODO
raise NotImplementedError("TODO: Implement update place")
db_place = db.query(Place).filter(Place.id == place_id).first()
if not db_place:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Place not found")
update_data = place.dict(exclude_unset = True)

for key, value in update_data.items():
setattr(db_place, key, value)

db.add(db_place)
db.commit()
db.refresh(db_place)
return db_place

@router.delete(
"/{place_id}",
summary="Delete a place",
status_code=status.HTTP_204_NO_CONTENT
)
async def delete_place(place_id: int, db: Session = Depends(get_db)):
# TODO
raise NotImplementedError("TODO: Implement delete place")
place = db.query(Place).filter(Place.id == place.id).first()
if not place:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Place not found")
db.delete(place)
db.commit()
return
1 change: 1 addition & 0 deletions fastapi-community
Submodule fastapi-community added at 883128
Loading