85 lines
4.4 KiB
Python
85 lines
4.4 KiB
Python
from fastapi import APIRouter, HTTPException, status, Query
|
|
from typing import List, Optional
|
|
import uuid
|
|
from app.data.data import shipments, dewars
|
|
from app.models import Shipment, Dewar, ContactPerson, Address, Proposal
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Shipment])
|
|
async def get_shipments(shipment_id: Optional[str] = Query(None, description="ID of the specific shipment to retrieve")):
|
|
if shipment_id:
|
|
shipment = next((sh for sh in shipments if sh.shipment_id == shipment_id), None)
|
|
if not shipment:
|
|
raise HTTPException(status_code=404, detail="Shipment not found")
|
|
return [shipment]
|
|
return shipments
|
|
|
|
@router.delete("/{shipment_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_shipment(shipment_id: str):
|
|
global shipments
|
|
shipments = [shipment for shipment in shipments if shipment.shipment_id != shipment_id]
|
|
|
|
@router.post("/{shipment_id}/add_dewar", response_model=Shipment)
|
|
async def add_dewar_to_shipment(shipment_id: str, dewar_id: str):
|
|
shipment = next((sh for sh in shipments if sh.shipment_id == shipment_id), None)
|
|
if not shipment:
|
|
raise HTTPException(status_code=404, detail="Shipment not found")
|
|
dewar = next((dw for dw in dewars if dw.id == dewar_id), None)
|
|
if not dewar:
|
|
raise HTTPException(status_code=404, detail="Dewar not found")
|
|
if dewar not in shipment.dewars:
|
|
shipment.dewars.append(dewar)
|
|
return shipment
|
|
|
|
@router.put("/{shipment_id}", response_model=Shipment)
|
|
async def update_shipment(shipment_id: str, updated_shipment: Shipment):
|
|
global shipments
|
|
shipment = next((sh for sh in shipments if sh.shipment_id == shipment_id), None)
|
|
if not shipment:
|
|
raise HTTPException(status_code=404, detail="Shipment not found")
|
|
shipment.shipment_name = updated_shipment.shipment_name
|
|
shipment.shipment_date = updated_shipment.shipment_date
|
|
shipment.shipment_status = updated_shipment.shipment_status
|
|
shipment.contact_person = updated_shipment.contact_person
|
|
shipment.proposal_number = updated_shipment.proposal_number
|
|
shipment.return_address = updated_shipment.return_address
|
|
shipment.comments = updated_shipment.comments
|
|
|
|
existing_dewar_dict = {dewar.id: dewar for dewar in shipment.dewars}
|
|
for updated_dewar in updated_shipment.dewars:
|
|
if updated_dewar.id in existing_dewar_dict:
|
|
existing_dewar_dict[updated_dewar.id].dewar_name = updated_dewar.dewar_name
|
|
existing_dewar_dict[updated_dewar.id].tracking_number = updated_dewar.tracking_number
|
|
existing_dewar_dict[updated_dewar.id].number_of_pucks = updated_dewar.number_of_pucks
|
|
existing_dewar_dict[updated_dewar.id].number_of_samples = updated_dewar.number_of_samples
|
|
existing_dewar_dict[updated_dewar.id].return_address = updated_dewar.return_address
|
|
existing_dewar_dict[updated_dewar.id].contact_person = updated_dewar.contact_person
|
|
existing_dewar_dict[updated_dewar.id].status = updated_dewar.status
|
|
existing_dewar_dict[updated_dewar.id].ready_date = updated_dewar.ready_date
|
|
existing_dewar_dict[updated_dewar.id].shipping_date = updated_dewar.shipping_date
|
|
existing_dewar_dict[updated_dewar.id].arrival_date = updated_dewar.arrival_date
|
|
existing_dewar_dict[updated_dewar.id].returning_date = updated_dewar.returning_date
|
|
existing_dewar_dict[updated_dewar.id].qrcode = updated_dewar.qrcode
|
|
else:
|
|
shipment.dewars.append(updated_dewar)
|
|
return shipment
|
|
|
|
@router.post("/", response_model=Shipment, status_code=status.HTTP_201_CREATED)
|
|
async def create_shipment(shipment: Shipment):
|
|
shipment_id = f'SHIP-{uuid.uuid4().hex[:8].upper()}'
|
|
shipment.shipment_id = shipment_id
|
|
shipments.append(shipment)
|
|
return shipment
|
|
|
|
@router.delete("/{shipment_id}/remove_dewar/{dewar_id}", response_model=Shipment)
|
|
async def remove_dewar_from_shipment(shipment_id: str, dewar_id: str):
|
|
shipment = next((sh for sh in shipments if sh.shipment_id == shipment_id), None)
|
|
if not shipment:
|
|
raise HTTPException(status_code=404, detail="Shipment not found")
|
|
shipment.dewars = [dw for dw in shipment.dewars if dw.id != dewar_id]
|
|
return shipment
|
|
|
|
@router.get("/contact_persons", response_model=List[ContactPerson])
|
|
async def get_shipment_contact_persons():
|
|
return [{"shipment_id": shipment.shipment_id, "contact_person": shipment.get_shipment_contact_persons()} for shipment in shipments] |