Connected frontend shipments to backend
This commit is contained in:
206
backend/main.py
Normal file
206
backend/main.py
Normal file
@ -0,0 +1,206 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi import HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
class ContactPerson(BaseModel):
|
||||
firstname: str
|
||||
lastname: str
|
||||
phone_number: str
|
||||
email: str
|
||||
|
||||
|
||||
class Address(BaseModel):
|
||||
street: str
|
||||
city: str
|
||||
zipcode: str
|
||||
country: str
|
||||
|
||||
|
||||
class Dewar(BaseModel):
|
||||
id: str
|
||||
dewar_name: str
|
||||
tracking_number: str
|
||||
number_of_pucks: int
|
||||
number_of_samples: int
|
||||
return_address: List[Address]
|
||||
contact_person: List[ContactPerson]
|
||||
status: str
|
||||
ready_date: Optional[str] = None
|
||||
shipping_date: Optional[str] = None
|
||||
arrival_date: Optional[str] = None
|
||||
shippingStatus: str
|
||||
arrivalStatus: str
|
||||
qrcode: str
|
||||
|
||||
|
||||
class Shipment(BaseModel):
|
||||
shipment_id: str
|
||||
shipment_name: str
|
||||
shipment_date: str
|
||||
shipment_status: str
|
||||
contact_person: List[ContactPerson]
|
||||
proposal_number: Optional[str] = None
|
||||
return_address: List[Address]
|
||||
comments: Optional[str] = None
|
||||
dewars: List[Dewar]
|
||||
|
||||
def get_number_of_dewars(self) -> int:
|
||||
return len(self.dewars)
|
||||
|
||||
def get_shipment_contact_persons(self) -> str:
|
||||
return self.contact_person
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
# Example data for contacts
|
||||
contacts = [
|
||||
ContactPerson(firstname="Frodo", lastname="Baggins", phone_number="123-456-7890", email="frodo.baggins@lotr.com"),
|
||||
ContactPerson(firstname="Samwise", lastname="Gamgee", phone_number="987-654-3210", email="samwise.gamgee@lotr.com"),
|
||||
ContactPerson(firstname="Aragorn", lastname="Elessar", phone_number="123-333-4444",
|
||||
email="aragorn.elessar@lotr.com"),
|
||||
ContactPerson(firstname="Legolas", lastname="Greenleaf", phone_number="555-666-7777",
|
||||
email="legolas.greenleaf@lotr.com"),
|
||||
ContactPerson(firstname="Gimli", lastname="Son of Gloin", phone_number="888-999-0000",
|
||||
email="gimli.sonofgloin@lotr.com"),
|
||||
ContactPerson(firstname="Gandalf", lastname="The Grey", phone_number="222-333-4444",
|
||||
email="gandalf.thegrey@lotr.com"),
|
||||
ContactPerson(firstname="Boromir", lastname="Son of Denethor", phone_number="111-222-3333",
|
||||
email="boromir.sonofdenethor@lotr.com"),
|
||||
ContactPerson(firstname="Galadriel", lastname="Lady of Lothlórien", phone_number="444-555-6666",
|
||||
email="galadriel.lothlorien@lotr.com"),
|
||||
ContactPerson(firstname="Elrond", lastname="Half-elven", phone_number="777-888-9999",
|
||||
email="elrond.halfelven@lotr.com"),
|
||||
ContactPerson(firstname="Eowyn", lastname="Shieldmaiden of Rohan", phone_number="000-111-2222",
|
||||
email="eowyn.rohan@lotr.com")
|
||||
]
|
||||
|
||||
# Example data for return addresses
|
||||
return_addresses = [
|
||||
Address(street='123 Hobbiton St', city='Shire', zipcode='12345', country='Middle Earth'),
|
||||
Address(street='456 Rohan Rd', city='Edoras', zipcode='67890', country='Middle Earth')
|
||||
]
|
||||
|
||||
# Example data for dewars
|
||||
dewars = [
|
||||
Dewar(
|
||||
id='DEWAR001',
|
||||
dewar_name='Dewar One',
|
||||
tracking_number='TRACK123',
|
||||
number_of_pucks=7,
|
||||
number_of_samples=70,
|
||||
return_address=[return_addresses[0]],
|
||||
contact_person=[contacts[0]],
|
||||
status='Ready',
|
||||
ready_date='2023-09-30',
|
||||
shipping_date='2023-10-01',
|
||||
arrival_date='2023-10-02',
|
||||
shippingStatus='Shipped',
|
||||
arrivalStatus='Arrived',
|
||||
qrcode='QR123DEWAR001'
|
||||
),
|
||||
Dewar(
|
||||
id='DEWAR002',
|
||||
dewar_name='Dewar Two',
|
||||
tracking_number='TRACK124',
|
||||
number_of_pucks=3,
|
||||
number_of_samples=33,
|
||||
return_address=[return_addresses[1]],
|
||||
contact_person=[contacts[1]],
|
||||
status='In Transit',
|
||||
ready_date='2023-10-01',
|
||||
shipping_date='2023-10-02',
|
||||
arrival_date='2023-10-04',
|
||||
shippingStatus='In Transit',
|
||||
arrivalStatus='Pending',
|
||||
qrcode='QR123DEWAR002'
|
||||
),
|
||||
Dewar(
|
||||
id='DEWAR003',
|
||||
dewar_name='Dewar Three',
|
||||
tracking_number='TRACK125',
|
||||
number_of_pucks=4,
|
||||
number_of_samples=47,
|
||||
return_address=[return_addresses[0]],
|
||||
contact_person=[contacts[2]],
|
||||
status='Pending',
|
||||
shippingStatus='Ready for Shipping',
|
||||
arrivalStatus='Pending',
|
||||
qrcode='QR123DEWAR003'
|
||||
),
|
||||
]
|
||||
# Example: Attach a specific Dewar by its id to a shipment
|
||||
specific_dewar_id = 'DEWAR003' # The ID of the Dewar you want to attach
|
||||
|
||||
# Find the Dewar with the matching id
|
||||
specific_dewar = next((dewar for dewar in dewars if dewar.id == specific_dewar_id), None)
|
||||
|
||||
# Since shipments need dewars, define them afterward
|
||||
shipments = [
|
||||
Shipment(
|
||||
shipment_id='SHIPMORDOR',
|
||||
shipment_date='2024-10-10',
|
||||
shipment_name='Shipment example test',
|
||||
shipment_status='Delivered',
|
||||
contact_person=[contacts[1]],
|
||||
proposal_number='PROJ001',
|
||||
return_address=[return_addresses[0]],
|
||||
comments='Handle with care',
|
||||
dewars=[specific_dewar] # Taking all dewars as an example
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
|
||||
@app.get("/contacts", response_model=List[ContactPerson])
|
||||
async def get_contacts():
|
||||
return contacts
|
||||
|
||||
|
||||
@app.get("/shipments", response_model=List[Shipment])
|
||||
async def get_shipments():
|
||||
return shipments
|
||||
|
||||
|
||||
@app.get("/dewars", response_model=List[Dewar])
|
||||
async def get_dewars():
|
||||
return dewars
|
||||
|
||||
|
||||
# Endpoint to get the number of dewars in each shipment
|
||||
@app.get("/shipment_dewars")
|
||||
async def get_shipment_dewars():
|
||||
return [{"shipment_id": shipment.shipment_id, "number_of_dewars": shipment.get_number_of_dewars()} for shipment in
|
||||
shipments]
|
||||
|
||||
@app.get("/shipment_contact_persons")
|
||||
async def get_shipment_contact_persons():
|
||||
return [{"shipment_id": shipment.shipment_id, "contact_person": shipment.get_shipment_contact_persons()} for shipment in
|
||||
shipments]
|
||||
|
||||
# Creation of a new shipment
|
||||
@app.post("/shipments", response_model=Shipment, status_code=status.HTTP_201_CREATED)
|
||||
async def create_shipment(shipment: Shipment):
|
||||
# Check for duplicate shipment_id
|
||||
if any(s.shipment_id == shipment.shipment_id for s in shipments):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Shipment with this ID already exists."
|
||||
)
|
||||
|
||||
shipments.append(shipment)
|
||||
return shipment
|
Reference in New Issue
Block a user