Connected frontend new contact and new address to backend
This commit is contained in:
@ -3,6 +3,9 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi import HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@ -28,6 +31,9 @@ class Address(BaseModel):
|
||||
zipcode: str
|
||||
country: str
|
||||
|
||||
class Proposal(BaseModel):
|
||||
id: int
|
||||
number: str
|
||||
|
||||
class Dewar(BaseModel):
|
||||
id: str
|
||||
@ -52,7 +58,7 @@ class Shipment(BaseModel):
|
||||
shipment_date: str
|
||||
shipment_status: str
|
||||
contact_person: List[ContactPerson]
|
||||
proposal_number: Optional[str] = None
|
||||
proposal_number: List[Proposal]
|
||||
return_address: List[Address]
|
||||
comments: Optional[str] = None
|
||||
dewars: List[Dewar]
|
||||
@ -60,9 +66,15 @@ class Shipment(BaseModel):
|
||||
def get_number_of_dewars(self) -> int:
|
||||
return len(self.dewars)
|
||||
|
||||
def get_shipment_contact_persons(self) -> str:
|
||||
def get_shipment_contact_persons(self) -> List[ContactPerson]:
|
||||
return self.contact_person
|
||||
|
||||
def get_shipment_return_addresses(self) -> List[Address]:
|
||||
return self.return_address
|
||||
|
||||
def get_proposals(self) -> List[Proposal]:
|
||||
return self.proposal_number
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
@ -143,33 +155,62 @@ dewars = [
|
||||
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)
|
||||
# Proposal data inspired by the Lord of the Rings
|
||||
proposals = [
|
||||
Proposal(id=1, number="PROPOSAL-FRODO-001"), # "The Quest for the Ring"
|
||||
Proposal(id=2, number="PROPOSAL-GANDALF-002"), # "The Fellowship's Journey"
|
||||
Proposal(id=3, number="PROPOSAL-ARAGORN-003"), # "Return of the King"
|
||||
Proposal(id=4, number="PROPOSAL-SAURON-004"), # "The Dark Lord's Plot"
|
||||
Proposal(id=5, number="PROPOSAL-MORDOR-005"), # "The Road to Mount Doom"
|
||||
]
|
||||
|
||||
# Since shipments need dewars, define them afterward
|
||||
# Example: Attach specific Dewars by their ids to shipments
|
||||
specific_dewar_ids1 = ['DEWAR003'] # The IDs of the Dewars you want to attach to the first shipment
|
||||
specific_dewar_ids2 = ['DEWAR001', 'DEWAR002'] # The IDs of the Dewars you want to attach to the second shipment
|
||||
|
||||
# Find the Dewars with the matching ids
|
||||
specific_dewars1 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids1]
|
||||
specific_dewars2 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids2]
|
||||
|
||||
# Define shipments with the selected Dewars
|
||||
shipments = [
|
||||
Shipment(
|
||||
shipment_id='SHIPMORDOR',
|
||||
shipment_date='2024-10-10',
|
||||
shipment_name='Shipment example test',
|
||||
shipment_name='Shipment from Mordor',
|
||||
shipment_status='Delivered',
|
||||
contact_person=[contacts[1]],
|
||||
proposal_number='PROJ001',
|
||||
proposal_number=[proposals[1]],
|
||||
return_address=[return_addresses[0]],
|
||||
comments='Handle with care',
|
||||
dewars=[specific_dewar] # Taking all dewars as an example
|
||||
dewars=specific_dewars1 # Attach specific Dewars for this shipment
|
||||
),
|
||||
Shipment(
|
||||
shipment_id='SHIPMORDOR2',
|
||||
shipment_date='2024-10-24',
|
||||
shipment_name='Shipment from Mordor',
|
||||
shipment_status='In Transit',
|
||||
contact_person=[contacts[3]],
|
||||
proposal_number=[proposals[2]],
|
||||
return_address=[return_addresses[1]], # Changed index to a valid one
|
||||
comments='Contains the one ring',
|
||||
dewars=specific_dewars2 # Attach specific Dewars for this shipment
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
|
||||
@app.get("/contacts", response_model=List[ContactPerson])
|
||||
async def get_contacts():
|
||||
return contacts
|
||||
|
||||
@app.get("/return_addresses", response_model=List[Address])
|
||||
async def get_return_addresses():
|
||||
return return_addresses
|
||||
|
||||
@app.get("/proposals", response_model=List[Proposal])
|
||||
async def get_proposals():
|
||||
return proposals
|
||||
|
||||
|
||||
@app.get("/shipments", response_model=List[Shipment])
|
||||
async def get_shipments():
|
||||
@ -204,3 +245,31 @@ async def create_shipment(shipment: Shipment):
|
||||
|
||||
shipments.append(shipment)
|
||||
return shipment
|
||||
|
||||
# Creation of a new contact
|
||||
@app.post("/contacts", response_model=ContactPerson, status_code=status.HTTP_201_CREATED)
|
||||
async def create_contact(contact: ContactPerson):
|
||||
logging.info(f"Received contact creation request: {contact}")
|
||||
# Check for duplicate contact by email (or other unique fields)
|
||||
if any(c.email == contact.email for c in contacts):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Contact with this email already exists."
|
||||
)
|
||||
|
||||
contacts.append(contact)
|
||||
return contact
|
||||
|
||||
# Creation of a return address
|
||||
@app.post("/return_addresses", response_model=Address, status_code=status.HTTP_201_CREATED)
|
||||
async def create_return_address(address: Address):
|
||||
logging.info(f"Received contact creation request: {address}")
|
||||
# Check for duplicate address by city
|
||||
if any(a.city == address.city for a in return_addresses):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Address in this city already exists."
|
||||
)
|
||||
|
||||
return_addresses.append(address)
|
||||
return address
|
||||
|
Reference in New Issue
Block a user