|
31 | 31 | from feeds_gen.models.operation_create_request_gtfs_feed import ( |
32 | 32 | OperationCreateRequestGtfsFeed, |
33 | 33 | ) |
| 34 | +from feeds_gen.models.operation_create_request_gtfs_rt_feed import ( |
| 35 | + OperationCreateRequestGtfsRtFeed, |
| 36 | +) |
34 | 37 | from feeds_gen.models.operation_gtfs_feed import OperationGtfsFeed |
35 | 38 | from feeds_gen.models.operation_gtfs_rt_feed import OperationGtfsRtFeed |
36 | 39 | from feeds_operations.impl.models.update_request_gtfs_feed_impl import ( |
|
59 | 62 | from .models.operation_create_request_gtfs_feed import ( |
60 | 63 | OperationCreateRequestGtfsFeedImpl, |
61 | 64 | ) |
| 65 | +from .models.operation_create_request_gtfs_rt_feed import ( |
| 66 | + OperationCreateRequestGtfsRtFeedImpl, |
| 67 | +) |
62 | 68 | from .models.operation_feed_impl import OperationFeedImpl |
63 | 69 | from .models.operation_gtfs_feed_impl import OperationGtfsFeedImpl |
64 | 70 | from .models.operation_gtfs_rt_feed_impl import OperationGtfsRtFeedImpl |
|
68 | 74 | class OperationsApiImpl(BaseOperationsApi): |
69 | 75 | """Implementation of the operations API.""" |
70 | 76 |
|
| 77 | + @staticmethod |
| 78 | + def assign_feed_id(new_feed: Gtfsfeed | Gtfsrealtimefeed): |
| 79 | + client_provided_id = bool(getattr(new_feed, "id", None)) |
| 80 | + if not client_provided_id: |
| 81 | + new_feed.id = new_feed.stable_id |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def assign_stable_id(new_feed: Gtfsfeed | Gtfsrealtimefeed, db_session: Session): |
| 85 | + client_provided_stable_id = bool(getattr(new_feed, "stable_id", None)) |
| 86 | + if not client_provided_stable_id: |
| 87 | + next_val = db_session.execute( |
| 88 | + text("SELECT nextval('md_sequence')") |
| 89 | + ).scalar_one() |
| 90 | + new_feed.stable_id = f"md-{next_val}" |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def assert_no_existing_feed_url(producer_url: str, db_session: Session): |
| 94 | + existing_feed = get_feed_by_normalized_url(producer_url, db_session) |
| 95 | + if existing_feed: |
| 96 | + message = ( |
| 97 | + f"A published feed with url " |
| 98 | + f"{producer_url} already exists." |
| 99 | + f"Existing feed ID: {existing_feed.stable_id}, " |
| 100 | + f"URL: {existing_feed.producer_url}" |
| 101 | + ) |
| 102 | + logging.error(message) |
| 103 | + raise HTTPException( |
| 104 | + status_code=400, |
| 105 | + detail=message, |
| 106 | + ) |
| 107 | + |
71 | 108 | @with_db_session |
72 | 109 | async def get_feeds( |
73 | 110 | self, |
@@ -314,38 +351,48 @@ async def create_gtfs_feed( |
314 | 351 | ) -> OperationGtfsFeed: |
315 | 352 | """Create a GTFS feed in the Mobility Database.""" |
316 | 353 | # Check if the provider_url already exists in an active feed |
317 | | - existing_feed = get_feed_by_normalized_url( |
318 | | - operation_create_request_gtfs_feed.source_info.producer_url, db_session |
| 354 | + OperationsApiImpl.assert_no_existing_feed_url( |
| 355 | + operation_create_request_gtfs_feed.source_info.producer_url, |
| 356 | + db_session, |
319 | 357 | ) |
320 | | - if existing_feed: |
321 | | - message = ( |
322 | | - f"A published feed with url " |
323 | | - f"{operation_create_request_gtfs_feed.source_info.producer_url} already exists." |
324 | | - f"Existing feed ID: {existing_feed.stable_id}, " |
325 | | - f"URL: {existing_feed.producer_url}" |
326 | | - ) |
327 | | - logging.error(message) |
328 | | - raise HTTPException( |
329 | | - status_code=400, |
330 | | - detail=message, |
331 | | - ) |
332 | 358 | # Proceed with feed creation |
333 | 359 | new_feed = OperationCreateRequestGtfsFeedImpl.to_orm( |
334 | 360 | operation_create_request_gtfs_feed |
335 | 361 | ) |
336 | 362 | new_feed.data_type = DataType.GTFS.value |
337 | | - client_provided_stable_id = bool(getattr(new_feed, "stable_id", None)) |
338 | | - if not client_provided_stable_id: |
339 | | - next_val = db_session.execute( |
340 | | - text("SELECT nextval('md_sequence')") |
341 | | - ).scalar_one() |
342 | | - new_feed.stable_id = f"md-{next_val}" |
343 | | - client_provided_id = bool(getattr(new_feed, "id", None)) |
344 | | - if not client_provided_id: |
345 | | - new_feed.id = new_feed.stable_id |
| 363 | + OperationsApiImpl.assign_stable_id(new_feed, db_session) |
| 364 | + OperationsApiImpl.assign_feed_id(new_feed) |
346 | 365 | db_session.add(new_feed) |
347 | 366 | db_session.commit() |
348 | 367 | created_feed = db_session.get(Gtfsfeed, new_feed.id) |
349 | 368 | logging.info("Created new GTFS feed with ID: %s", new_feed.stable_id) |
350 | 369 | payload = OperationGtfsFeedImpl.from_orm(created_feed).model_dump() |
351 | 370 | return JSONResponse(status_code=201, content=jsonable_encoder(payload)) |
| 371 | + |
| 372 | + @with_db_session |
| 373 | + async def create_gtfs_rt_feed( |
| 374 | + self, |
| 375 | + operation_create_request_gtfs_rt_feed: Annotated[ |
| 376 | + OperationCreateRequestGtfsRtFeed, |
| 377 | + Field(description="Payload to create the specified GTF-RT feed."), |
| 378 | + ], |
| 379 | + db_session: Session = None, |
| 380 | + ) -> OperationGtfsRtFeed: |
| 381 | + """Create a GTFS-RT feed in the Mobility Database.""" |
| 382 | + OperationsApiImpl.assert_no_existing_feed_url( |
| 383 | + operation_create_request_gtfs_rt_feed.source_info.producer_url, |
| 384 | + db_session, |
| 385 | + ) |
| 386 | + # Proceed with feed creation |
| 387 | + new_feed = OperationCreateRequestGtfsRtFeedImpl.to_orm( |
| 388 | + operation_create_request_gtfs_rt_feed |
| 389 | + ) |
| 390 | + new_feed.data_type = DataType.GTFS_RT.value |
| 391 | + OperationsApiImpl.assign_stable_id(new_feed, db_session) |
| 392 | + OperationsApiImpl.assign_feed_id(new_feed) |
| 393 | + db_session.add(new_feed) |
| 394 | + db_session.commit() |
| 395 | + created_feed = db_session.get(Gtfsrealtimefeed, new_feed.id) |
| 396 | + logging.info("Created new GTFS-RT feed with ID: %s", new_feed.stable_id) |
| 397 | + payload = OperationGtfsRtFeedImpl.from_orm(created_feed).model_dump() |
| 398 | + return JSONResponse(status_code=201, content=jsonable_encoder(payload)) |
0 commit comments