regirstation works but shared links broken
This commit is contained in:
@@ -6,11 +6,13 @@ from fastapi import HTTPException, status
|
||||
from geoalchemy2.shape import from_shape, to_shape
|
||||
from shapely.geometry import shape, Point, LineString
|
||||
import json
|
||||
import asyncio
|
||||
|
||||
from app.models.map_item import MapItem
|
||||
from app.models.user import User
|
||||
from app.schemas.map_item import MapItemCreate, MapItemUpdate
|
||||
from app.services.map_service import get_map_by_id
|
||||
from app.websocket.connection_manager import manager
|
||||
|
||||
|
||||
def get_map_items(db: Session, map_id: UUID, user: Optional[User] = None) -> List[MapItem]:
|
||||
@@ -58,6 +60,19 @@ def geography_to_geojson(geography) -> dict:
|
||||
return json.loads(json.dumps(geom.__geo_interface__))
|
||||
|
||||
|
||||
def item_to_dict(item: MapItem) -> dict:
|
||||
"""Convert MapItem to JSON-serializable dict for WebSocket broadcast."""
|
||||
return {
|
||||
"id": str(item.id),
|
||||
"map_id": str(item.map_id),
|
||||
"type": item.type,
|
||||
"geometry": geography_to_geojson(item.geometry),
|
||||
"properties": item.properties,
|
||||
"created_at": item.created_at.isoformat(),
|
||||
"updated_at": item.updated_at.isoformat()
|
||||
}
|
||||
|
||||
|
||||
def create_map_item(db: Session, map_id: UUID, item_data: MapItemCreate, user: User) -> MapItem:
|
||||
"""Create a new map item."""
|
||||
# Verify user has access to the map
|
||||
@@ -93,6 +108,14 @@ def create_map_item(db: Session, map_id: UUID, item_data: MapItemCreate, user: U
|
||||
print(f"Updating port connections for end device: {end_device_id}")
|
||||
update_device_connections(db, UUID(end_device_id), item.id)
|
||||
|
||||
# Broadcast item creation to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_created(map_id, item_to_dict(item)))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
return item
|
||||
|
||||
|
||||
@@ -155,6 +178,14 @@ def update_map_item(db: Session, item_id: UUID, item_data: MapItemUpdate, user:
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
|
||||
# Broadcast item update to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_updated(item.map_id, item_to_dict(item)))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
return item
|
||||
|
||||
|
||||
@@ -162,6 +193,10 @@ def delete_map_item(db: Session, item_id: UUID, user: User) -> None:
|
||||
"""Delete a map item."""
|
||||
item = get_map_item_by_id(db, item_id, user)
|
||||
|
||||
# Capture map_id and item_id before deletion for WebSocket broadcast
|
||||
map_id = item.map_id
|
||||
deleted_item_id = str(item.id)
|
||||
|
||||
# If deleting a cable, remove it from device connections
|
||||
if item.type == 'cable':
|
||||
start_device_id = item.properties.get('start_device_id')
|
||||
@@ -192,6 +227,14 @@ def delete_map_item(db: Session, item_id: UUID, user: User) -> None:
|
||||
db.delete(item)
|
||||
db.commit()
|
||||
|
||||
# Broadcast item deletion to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_deleted(map_id, deleted_item_id))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
|
||||
def remove_device_connection(db: Session, device_id: UUID, cable_id: UUID) -> None:
|
||||
"""Remove cable connection from device's connections array."""
|
||||
|
||||
Reference in New Issue
Block a user