Skip to content

Commit 289d23e

Browse files
committed
Add POST /location-groups endpoint
- todo: link locations to group, blocked on location model
1 parent 743ca23 commit 289d23e

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

backend/python/app/models/location_group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LocationGroup(LocationGroupBase, BaseModel, table=True):
2424
class LocationGroupCreate(LocationGroupBase):
2525
"""Location group creation request"""
2626

27-
pass
27+
location_ids: list[int] = Field(min_length=1)
2828

2929

3030
class LocationGroupRead(LocationGroupBase):

backend/python/app/routers/location_group_routes.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
from app.dependencies.auth import require_user_or_admin
88
from app.models import get_session
9-
from app.models.location_group import LocationGroupRead, LocationGroupUpdate
9+
from app.models.location_group import (
10+
LocationGroupCreate,
11+
LocationGroupRead,
12+
LocationGroupUpdate,
13+
)
1014
from app.services.implementations.location_group_service import LocationGroupService
1115

1216
# Initialize service
@@ -16,6 +20,26 @@
1620
router = APIRouter(prefix="/location-groups", tags=["location-groups"])
1721

1822

23+
@router.post("/", response_model=LocationGroupRead, status_code=status.HTTP_201_CREATED)
24+
async def create_location_group(
25+
location_group: LocationGroupCreate,
26+
session: AsyncSession = Depends(get_session),
27+
_: bool = Depends(require_user_or_admin),
28+
) -> LocationGroupRead:
29+
"""
30+
Create a new location group
31+
"""
32+
try:
33+
new_location_group = await location_group_service.create_location_group(
34+
session, location_group
35+
)
36+
return LocationGroupRead.model_validate(new_location_group)
37+
except Exception as e:
38+
raise HTTPException(
39+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
40+
) from e
41+
42+
1943
@router.patch("/{location_group_id}", response_model=LocationGroupRead)
2044
async def update_location_group(
2145
location_group_id: UUID,

backend/python/app/services/implementations/location_group_service.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,41 @@
44
from sqlalchemy.ext.asyncio import AsyncSession
55
from sqlmodel import select
66

7-
from app.models.location_group import LocationGroup, LocationGroupUpdate
7+
from app.models.location_group import (
8+
LocationGroup,
9+
LocationGroupCreate,
10+
LocationGroupUpdate,
11+
)
812

913

1014
class LocationGroupService:
1115
def __init__(self, logger: logging.Logger):
1216
self.logger = logger
1317

18+
async def create_location_group(
19+
self,
20+
session: AsyncSession,
21+
location_group_data: LocationGroupCreate,
22+
) -> LocationGroup:
23+
"""Create a new location group"""
24+
try:
25+
data = location_group_data.model_dump()
26+
location_ids = data.pop("location_ids")
27+
28+
new_location_group = LocationGroup(**data, num_locations=len(location_ids))
29+
session.add(new_location_group)
30+
await session.commit()
31+
await session.refresh(new_location_group)
32+
33+
# blocked on location model
34+
# TODO: update each locations location_group_id foreign key using location_ids
35+
36+
return new_location_group
37+
except Exception as error:
38+
self.logger.error(f"Failed to create location group: {error!s}")
39+
await session.rollback()
40+
raise error
41+
1442
async def update_location_group(
1543
self,
1644
session: AsyncSession,

0 commit comments

Comments
 (0)