changed models and schemasa
This commit is contained in:
@ -1,24 +1,27 @@
|
||||
# app/routers/shipment.py
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status, Query, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
import uuid
|
||||
import json
|
||||
from datetime import date
|
||||
import logging
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.models import Shipment as ShipmentModel, ContactPerson as ContactPersonModel, Address as AddressModel, Proposal as ProposalModel, Dewar as DewarModel
|
||||
from app.schemas import ShipmentCreate, UpdateShipmentComments, Shipment as ShipmentSchema, DewarUpdate, ContactPerson as ContactPersonSchema
|
||||
from app.schemas import Sample as SampleSchema
|
||||
from app.models import Shipment as ShipmentModel, ContactPerson as ContactPersonModel, Address as AddressModel, \
|
||||
Proposal as ProposalModel, Dewar as DewarModel
|
||||
from app.schemas import ShipmentCreate, UpdateShipmentComments, Shipment as ShipmentSchema, DewarUpdate, \
|
||||
ContactPerson as ContactPersonSchema, Sample as SampleSchema, DewarCreate, PuckCreate, SampleCreate
|
||||
from app.database import get_db
|
||||
from app.crud import get_shipments, get_shipment_by_id
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def default_serializer(obj):
|
||||
if isinstance(obj, date):
|
||||
return obj.isoformat()
|
||||
raise TypeError(f"Type {type(obj)} not serializable")
|
||||
|
||||
|
||||
@router.get("", response_model=List[ShipmentSchema])
|
||||
async def fetch_shipments(id: Optional[int] = Query(None), db: Session = Depends(get_db)):
|
||||
if id:
|
||||
@ -35,6 +38,7 @@ async def fetch_shipments(id: Optional[int] = Query(None), db: Session = Depends
|
||||
logging.info(f"Shipment ID: {shipment.id}, Shipment Name: {shipment.shipment_name}")
|
||||
return shipments
|
||||
|
||||
|
||||
@router.post("", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
contact_person = db.query(ContactPersonModel).filter(ContactPersonModel.id == shipment.contact_person_id).first()
|
||||
@ -68,6 +72,7 @@ async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db
|
||||
|
||||
return db_shipment
|
||||
|
||||
|
||||
@router.delete("/{shipment_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_shipment(id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
@ -77,6 +82,7 @@ async def delete_shipment(id: int, db: Session = Depends(get_db)):
|
||||
db.commit()
|
||||
return
|
||||
|
||||
|
||||
@router.put("/{shipment_id}", response_model=ShipmentSchema)
|
||||
async def update_shipment(id: int, updated_shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
print("Received payload:", json.dumps(updated_shipment.dict(), indent=2, default=default_serializer))
|
||||
@ -145,6 +151,7 @@ async def add_dewar_to_shipment(id: int, dewar_id: int, db: Session = Depends(ge
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
|
||||
|
||||
@router.delete("/{shipment_id}/remove_dewar/{dewar_id}", response_model=ShipmentSchema)
|
||||
async def remove_dewar_from_shipment(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
@ -160,11 +167,13 @@ async def remove_dewar_from_shipment(shipment_id: int, dewar_id: int, db: Sessio
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
|
||||
|
||||
@router.get("/contact_persons", response_model=List[ContactPersonSchema])
|
||||
async def get_shipment_contact_persons(db: Session = Depends(get_db)):
|
||||
contact_persons = db.query(ContactPersonModel).all()
|
||||
return contact_persons
|
||||
|
||||
|
||||
@router.get("/{shipment_id}/samples", response_model=List[SampleSchema])
|
||||
def get_samples_in_shipment(id: int, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == id).first()
|
||||
@ -174,27 +183,25 @@ def get_samples_in_shipment(id: int, db: Session = Depends(get_db)):
|
||||
samples = []
|
||||
for dewar in shipment.dewars:
|
||||
for puck in dewar.pucks:
|
||||
samples.extend(puck.positions)
|
||||
samples.extend(puck.samples)
|
||||
|
||||
return samples
|
||||
|
||||
@router.get("/{shipment_id}/dewars/{dewar_id}/samples", response_model=List[SampleSchema])
|
||||
def get_samples_in_dewar(
|
||||
shipment_id: int, dewar_id: int, db: Session = Depends(get_db)
|
||||
):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
if shipment is None:
|
||||
|
||||
@router.get("/shipments/{shipment_id}/dewars/{dewar_id}/samples", response_model=List[SampleSchema])
|
||||
def get_samples_in_dewar(shipment_id: int, dewar_id: int, db: Session = Depends(get_db)):
|
||||
shipment = get_shipment_by_id(db, shipment_id)
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
dewar = db.query(DewarModel).filter(
|
||||
DewarModel.id == dewar_id, DewarModel.shipment_id == shipment_id
|
||||
).first()
|
||||
if dewar is None:
|
||||
raise HTTPException(status_code=404, detail="Dewar not found in shipment")
|
||||
dewar = next((d for d in shipment.dewars if d.id == dewar_id), None)
|
||||
if not dewar:
|
||||
raise HTTPException(status_code=404, detail="Dewar not found")
|
||||
|
||||
samples = []
|
||||
for puck in dewar.pucks:
|
||||
samples.extend(puck.positions)
|
||||
for sample in puck.samples:
|
||||
samples.append(sample)
|
||||
|
||||
return samples
|
||||
|
||||
@ -208,4 +215,51 @@ async def update_shipment_comments(id: int, comments_data: UpdateShipmentComment
|
||||
shipment.comments = comments_data.comments
|
||||
db.commit()
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
return shipment
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar_puck_sample", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def add_dewar_puck_sample_to_shipment(shipment_id: int, payload: DewarCreate, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
for dewar_data in payload.dewars:
|
||||
dewar = Dewar(
|
||||
shipment_id=shipment_id,
|
||||
dewar_name=dewar_data.dewar_name,
|
||||
tracking_number=dewar_data.tracking_number,
|
||||
status=dewar_data.status,
|
||||
contact_person_id=dewar_data.contact_person_id,
|
||||
return_address_id=dewar_data.return_address_id,
|
||||
)
|
||||
db.add(dewar)
|
||||
db.commit()
|
||||
db.refresh(dewar)
|
||||
|
||||
for puck_data in dewar_data.pucks:
|
||||
puck = Puck(
|
||||
dewar_id=dewar.id,
|
||||
puck_name=puck_data.puck_name,
|
||||
puck_type=puck_data.puck_type,
|
||||
puck_location_in_dewar=puck_data.puck_location_in_dewar,
|
||||
)
|
||||
db.add(puck)
|
||||
db.commit()
|
||||
db.refresh(puck)
|
||||
|
||||
for sample_data in puck_data.samples:
|
||||
sample = Sample(
|
||||
puck_id=puck.id,
|
||||
sample_name=sample_data.sample_name,
|
||||
position=sample_data.position,
|
||||
data_collection_parameters=DataCollectionParameters(
|
||||
**sample_data.data_collection_parameters
|
||||
),
|
||||
)
|
||||
db.add(sample)
|
||||
db.commit()
|
||||
db.refresh(sample)
|
||||
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
|
Reference in New Issue
Block a user