Skip to content

Commit 8647191

Browse files
committed
Update location's location_group fk on location_group create
1 parent 289d23e commit 8647191

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-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-
location_ids: list[int] = Field(min_length=1)
27+
location_ids: list[UUID] = Field(min_length=1)
2828

2929

3030
class LocationGroupRead(LocationGroupBase):

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
LocationGroupCreate,
1010
LocationGroupUpdate,
1111
)
12+
from app.models.location import Location
1213

1314

1415
class LocationGroupService:
@@ -30,8 +31,18 @@ async def create_location_group(
3031
await session.commit()
3132
await session.refresh(new_location_group)
3233

33-
# blocked on location model
34-
# TODO: update each locations location_group_id foreign key using location_ids
34+
# Update each locations location_group_id foreign key
35+
for location_id in location_ids:
36+
statement = select(Location).where(Location.location_id == location_id)
37+
result = await session.execute(statement)
38+
location = result.scalars().first()
39+
40+
if location:
41+
location.location_group_id = new_location_group.location_group_id
42+
else:
43+
self.logger.warning(f"Location with id {location_id} not found")
44+
45+
await session.commit()
3546

3647
return new_location_group
3748
except Exception as error:

0 commit comments

Comments
 (0)