|
19 | 19 |
|
20 | 20 | from fastapi import APIRouter, Depends, Form, UploadFile, File |
21 | 21 | from fastapi_pagination.ext.sqlalchemy import paginate |
22 | | -from sqlalchemy import select |
| 22 | +from sqlalchemy import delete, select |
23 | 23 | from sqlalchemy.exc import ProgrammingError |
24 | 24 | from starlette.concurrency import run_in_threadpool |
25 | 25 | from starlette.status import ( |
|
38 | 38 | ) |
39 | 39 | from db import Thing |
40 | 40 | from db.asset import Asset, AssetThingAssociation |
41 | | -from schemas.asset import AssetResponse, CreateAsset, UpdateAsset |
| 41 | +from schemas.asset import ( |
| 42 | + AssetAssociationResponse, |
| 43 | + AssetAssociationUpdate, |
| 44 | + AssetResponse, |
| 45 | + CreateAsset, |
| 46 | + UpdateAsset, |
| 47 | +) |
42 | 48 | from services.audit_helper import audit_add |
43 | 49 | from services.crud_helper import model_patcher, model_deleter |
44 | 50 | from services.edit_notification_helper import ( |
@@ -481,6 +487,53 @@ async def get_asset( |
481 | 487 |
|
482 | 488 |
|
483 | 489 | # PATCH ====================================================================== |
| 490 | +@router.patch("/{asset_id}/association") |
| 491 | +async def update_asset_thing_association( |
| 492 | + asset_id: int, |
| 493 | + association_data: AssetAssociationUpdate, |
| 494 | + session: session_dependency, |
| 495 | + user: editor_dependency, |
| 496 | +) -> AssetAssociationResponse: |
| 497 | + """ |
| 498 | + Move an asset to another Thing or remove its Thing association. |
| 499 | +
|
| 500 | + Passing a `thing_id` replaces any existing Thing links for the asset with |
| 501 | + that one Thing. Passing `thing_id: null` leaves the Asset record in place |
| 502 | + and removes all Thing links. |
| 503 | + """ |
| 504 | + simple_get_by_id(session, Asset, asset_id) |
| 505 | + |
| 506 | + thing_id = association_data.thing_id |
| 507 | + thing = None |
| 508 | + if thing_id is not None: |
| 509 | + thing = session.get(Thing, thing_id) |
| 510 | + if thing is None: |
| 511 | + raise PydanticStyleException( |
| 512 | + status_code=HTTP_409_CONFLICT, |
| 513 | + detail=[ |
| 514 | + { |
| 515 | + "loc": ["body", "thing_id"], |
| 516 | + "msg": f"Thing with ID {thing_id} not found.", |
| 517 | + "type": "value_error", |
| 518 | + "input": {"thing_id": thing_id}, |
| 519 | + } |
| 520 | + ], |
| 521 | + ) |
| 522 | + |
| 523 | + association_delete = delete(AssetThingAssociation).where( |
| 524 | + AssetThingAssociation.asset_id == asset_id |
| 525 | + ) |
| 526 | + session.execute(association_delete) |
| 527 | + |
| 528 | + if thing is not None: |
| 529 | + assoc = AssetThingAssociation(asset_id=asset_id, thing_id=thing.id) |
| 530 | + audit_add(user, assoc) |
| 531 | + session.add(assoc) |
| 532 | + |
| 533 | + session.commit() |
| 534 | + return AssetAssociationResponse(asset_id=asset_id, thing_id=thing_id) |
| 535 | + |
| 536 | + |
484 | 537 | @router.patch("/{asset_id}") |
485 | 538 | async def update_asset( |
486 | 539 | asset_id: int, |
|
0 commit comments