changed models and schemasa
This commit is contained in:
@ -1,31 +1,27 @@
|
||||
# app/services/shipment_processor.py
|
||||
# Adjusting the ShipmentProcessor for better error handling and alignment
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models import Shipment, Dewar, Puck, Sample
|
||||
from app.models import Shipment, Dewar, Puck, Sample, DataCollectionParameters
|
||||
from app.schemas import ShipmentCreate, ShipmentResponse
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ShipmentProcessor:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def process_shipment(self, shipment: ShipmentCreate) -> ShipmentResponse:
|
||||
logger = logging.getLogger(__name__)
|
||||
try:
|
||||
# Creating new Shipment
|
||||
new_shipment = Shipment(
|
||||
shipment_name=shipment.shipment_name,
|
||||
shipment_date=shipment.shipment_date,
|
||||
shipment_status=shipment.shipment_status, # Adjusted to match model
|
||||
# other shipment-related fields if any
|
||||
shipment_status=shipment.shipment_status,
|
||||
)
|
||||
self.db.add(new_shipment)
|
||||
self.db.commit()
|
||||
self.db.refresh(new_shipment)
|
||||
|
||||
# Link Dewars
|
||||
for dewar_data in shipment.dewars:
|
||||
dewar = Dewar(
|
||||
shipment_id=new_shipment.id,
|
||||
@ -39,7 +35,6 @@ class ShipmentProcessor:
|
||||
self.db.commit()
|
||||
self.db.refresh(dewar)
|
||||
|
||||
# Link Pucks
|
||||
for puck_data in dewar_data.pucks:
|
||||
puck = Puck(
|
||||
dewar_id=dewar.id,
|
||||
@ -51,18 +46,19 @@ class ShipmentProcessor:
|
||||
self.db.commit()
|
||||
self.db.refresh(puck)
|
||||
|
||||
# Link Samples
|
||||
for sample_data in puck_data.samples:
|
||||
data_collection_params = DataCollectionParameters(
|
||||
**sample_data.data_collection_parameters.dict(by_alias=True))
|
||||
sample = Sample(
|
||||
puck_id=puck.id,
|
||||
sample_name=sample_data.sample_name,
|
||||
position=sample_data.position,
|
||||
data_collection_parameters=data_collection_params
|
||||
)
|
||||
self.db.add(sample)
|
||||
self.db.commit()
|
||||
self.db.refresh(sample)
|
||||
|
||||
# Return a response wrapped in the ShipmentResponse schema
|
||||
return ShipmentResponse(
|
||||
shipment_id=new_shipment.id,
|
||||
status="success",
|
||||
|
Reference in New Issue
Block a user