-
Notifications
You must be signed in to change notification settings - Fork 0
add CRUD operations for routes (DEV-58) #24
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
Open
ivymxu
wants to merge
2
commits into
main
Choose a base branch
from
feature/DEV-58/routes-crud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,88 @@ | ||
| """Routes REST API. | ||
|
|
||
| Endpoints for dispatch routes (pickup/dropoff furniture lists). | ||
| Full CRUD implementation for the Routes resource. | ||
| """ | ||
|
|
||
| from fastapi import APIRouter, Response, status | ||
| from fastapi import APIRouter, Depends, HTTPException, Response, status | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from ..database import get_db | ||
| from ..schemas import Route as RouteSchema | ||
| from ..schemas import RouteCreate, RouteUpdate | ||
| from ..services import route_service | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.get("") | ||
| async def list_routes(): | ||
| """List routes. Placeholder — implement with Route model and schemas.""" | ||
| return Response( | ||
| content="Not implemented — see docs/STARTER_BACKEND_GUIDE.md", | ||
| status_code=status.HTTP_501_NOT_IMPLEMENTED, | ||
| ) | ||
| @router.get("", response_model=list[RouteSchema]) | ||
| async def list_routes(db: AsyncSession = Depends(get_db)): | ||
| """List all routes.""" | ||
| return await route_service.list_routes(db) | ||
|
|
||
|
|
||
| @router.post("", response_model=RouteSchema, status_code=status.HTTP_201_CREATED) | ||
| async def create_route(payload: RouteCreate, db: AsyncSession = Depends(get_db)): | ||
| """Create a new route.""" | ||
| try: | ||
| return await route_service.create_route(db, payload) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e), | ||
| ) from e | ||
|
|
||
|
|
||
| @router.get("/{route_id}", response_model=RouteSchema) | ||
| async def get_route(route_id: str, db: AsyncSession = Depends(get_db)): | ||
| """Get a single route by ID.""" | ||
| route = await route_service.get_route(db, route_id) | ||
| if not route: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Route not found", | ||
| ) | ||
| return route | ||
|
|
||
|
|
||
| @router.put("/{route_id}", response_model=RouteSchema) | ||
| async def update_route( | ||
| route_id: str, | ||
| payload: RouteUpdate, | ||
| db: AsyncSession = Depends(get_db), | ||
| ): | ||
| """Update a route.""" | ||
| route = await route_service.get_route(db, route_id) | ||
| if not route: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Route not found", | ||
| ) | ||
|
|
||
| try: | ||
| return await route_service.update_route(db, route, payload) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e), | ||
| ) from e | ||
|
|
||
|
|
||
| @router.delete("/{route_id}", status_code=status.HTTP_204_NO_CONTENT) | ||
| async def delete_route(route_id: str, db: AsyncSession = Depends(get_db)): | ||
| """Delete a route.""" | ||
| route = await route_service.get_route(db, route_id) | ||
| if not route: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Route not found", | ||
| ) | ||
|
|
||
| try: | ||
| await route_service.delete_route(db, route) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e), | ||
| ) from e | ||
|
|
||
| return Response(status_code=status.HTTP_204_NO_CONTENT) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Agency services module. | ||
|
|
||
| Contains business logic for agency-related operations. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.exc import IntegrityError | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| from ..models import Agency | ||
| from ..schemas import AgencyCreate, AgencyUpdate | ||
|
|
||
|
|
||
| async def list_agencies(db: AsyncSession) -> list[Agency]: | ||
| """List all agencies ordered by name.""" | ||
| result = await db.execute(select(Agency).order_by(Agency.name)) | ||
| return list(result.scalars().all()) | ||
|
|
||
|
|
||
| async def get_agency(db: AsyncSession, agency_id: str) -> Agency | None: | ||
| """Get a single agency by ID, or None when not found.""" | ||
| result = await db.execute(select(Agency).where(Agency.id == agency_id)) | ||
| return result.scalar_one_or_none() | ||
|
|
||
|
|
||
| async def create_agency(db: AsyncSession, payload: AgencyCreate) -> Agency: | ||
| """Create a new agency.""" | ||
| db_agency = Agency(**payload.model_dump()) | ||
| db.add(db_agency) | ||
| try: | ||
| await db.commit() | ||
| except IntegrityError as e: | ||
| await db.rollback() | ||
| logger.exception("IntegrityError creating agency: %s", e.orig) | ||
| raise ValueError("Unable to create agency due to a data conflict.") from e | ||
| await db.refresh(db_agency) | ||
ivymxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return db_agency | ||
|
|
||
|
|
||
| async def update_agency( | ||
| db: AsyncSession, agency: Agency, payload: AgencyUpdate | ||
| ) -> Agency: | ||
| """Update an existing agency.""" | ||
| data = payload.model_dump(exclude_unset=True) | ||
|
|
||
| if not data: | ||
| raise ValueError("No update fields were provided.") | ||
|
|
||
| for key, value in data.items(): | ||
| setattr(agency, key, value) | ||
|
|
||
| try: | ||
| await db.commit() | ||
| except IntegrityError as e: | ||
| await db.rollback() | ||
| logger.exception("IntegrityError updating agency %s: %s", agency.id, e.orig) | ||
| raise ValueError("Update failed due to a data conflict.") from e | ||
| await db.refresh(agency) | ||
| return agency | ||
|
|
||
|
|
||
| async def delete_agency(db: AsyncSession, agency: Agency) -> None: | ||
| """Delete an existing agency.""" | ||
| await db.delete(agency) | ||
| try: | ||
| await db.commit() | ||
| except IntegrityError as e: | ||
| await db.rollback() | ||
| logger.exception("IntegrityError deleting agency %s: %s", agency.id, e.orig) | ||
| raise ValueError("Unable to delete agency due to a data conflict.") from e | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.