Chore: Pushing changes to migrate from windows/wsl to fedora
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
"""
|
||||
The Client table
|
||||
The Clients endpoint
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from sqlmodel import Session, select
|
||||
from sqlmodel import select
|
||||
from app.api.deps import SessionDep, exists
|
||||
from app.schemas.models import Client
|
||||
from app.schemas.schemas import ClientCreate
|
||||
from typing import Sequence
|
||||
from app.schemas.schemas import ClientCreate, ClientUpdate
|
||||
from pydantic import ValidationError
|
||||
from typing import Sequence, List, Optional
|
||||
|
||||
router = APIRouter(prefix="/clients", tags=["clients"])
|
||||
|
||||
|
||||
@router.get("/", response_model=Client)
|
||||
def fetch_clients(client: Client, session: SessionDep) -> Sequence[Client]:
|
||||
@router.get("/", response_model=List[Client])
|
||||
def fetch_clients(session: SessionDep) -> Sequence[Client]:
|
||||
"""Fetch client list
|
||||
"""
|
||||
clients = session.exec(select(Client)).all()
|
||||
@@ -21,11 +22,66 @@ def fetch_clients(client: Client, session: SessionDep) -> Sequence[Client]:
|
||||
|
||||
@router.post("/", response_model=ClientCreate)
|
||||
def create_client(client_data: ClientCreate, session: SessionDep) -> Client:
|
||||
"""Create a client
|
||||
"""
|
||||
existing = exists(session, Client, tin_number=client_data.tin_number)
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Client with this tin number already exists")
|
||||
client = Client.model_validate(client_data)
|
||||
|
||||
try:
|
||||
client = Client.model_validate(client_data)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=e.errors())
|
||||
|
||||
session.add(client)
|
||||
session.commit()
|
||||
session.refresh(client)
|
||||
return client
|
||||
|
||||
|
||||
@router.get("/{client_id}", response_model=Client)
|
||||
def get_client(client_id: int, session: SessionDep) -> Optional[Client]:
|
||||
"""Returns a client by its ID
|
||||
"""
|
||||
stmt = select(Client).where(Client.id == client_id)
|
||||
result: Optional[Client] = session.exec(stmt).first()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/{client_id}", response_model=ClientCreate)
|
||||
def update_client(
|
||||
client_id: int,
|
||||
client_data: ClientUpdate,
|
||||
session: SessionDep) -> Optional[Client]:
|
||||
"""Updates a client using its ID
|
||||
"""
|
||||
client = session.get(Client, client_id)
|
||||
if not client:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
update_fields = client_data.model_dump(exclude_unset=True)
|
||||
|
||||
for key, value in update_fields.items():
|
||||
setattr(client, key, value)
|
||||
|
||||
session.add(client)
|
||||
session.commit()
|
||||
session.refresh(client)
|
||||
return client
|
||||
|
||||
|
||||
@router.delete("/{client_id}", status_code=204)
|
||||
def delete_client(client_id: int, session: SessionDep):
|
||||
"""Deletes a client
|
||||
"""
|
||||
client = session.get(Client, client_id)
|
||||
|
||||
if not client:
|
||||
raise HTTPException(status_code=404, detail="Client not found")
|
||||
|
||||
session.delete(client)
|
||||
session.commit()
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
"""
|
||||
API Home
|
||||
"""
|
||||
from fastapi import APIRouter
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
#if settings.environment == "local":
|
||||
# api_router.include_router()
|
||||
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
The products endpoint
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from sqlmodel import select
|
||||
from app.api.deps import SessionDep, exists
|
||||
from app.schemas.models import Product
|
||||
from app.schemas.schemas import ProductCreate, ProductUpdate
|
||||
from pydantic import ValidationError
|
||||
from typing import Sequence, List, Optional
|
||||
|
||||
|
||||
router = APIRouter(prefix="/products", tags=["products"])
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Product])
|
||||
def fetch_products(session: SessionDep) -> Sequence[Product]:
|
||||
"""Fetch product list
|
||||
"""
|
||||
products = session.exec(select(Product)).all()
|
||||
return products
|
||||
|
||||
|
||||
@router.post("/", response_model=ProductCreate)
|
||||
def create_product(product_data: ProductCreate, session: SessionDep) -> Product:
|
||||
"""Create a product
|
||||
"""
|
||||
name_exists = exists(session, Product, product_name=product_data.product_name)
|
||||
if name_exists:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Product with this product_name exists"
|
||||
)
|
||||
|
||||
existing = exists(session, Product, product_code=product_data.product_code)
|
||||
if existing:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Product with this product_code exists"
|
||||
)
|
||||
|
||||
try:
|
||||
product = Product.model_validate(product_data)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=e.errors())
|
||||
|
||||
session.add(product)
|
||||
session.commit()
|
||||
session.refresh(product)
|
||||
return product
|
||||
|
||||
|
||||
@router.get("/{product_id}", response_model=Product)
|
||||
def get_product(product_id: int, session: SessionDep) -> Optional[Product]:
|
||||
"""Returns a product by its id
|
||||
"""
|
||||
stmt = select(Product).where(Product.id == product_id)
|
||||
result: Optional[Product] = session.exec(stmt).first()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/{product_id}",response_model=ProductUpdate)
|
||||
def update_product(
|
||||
product_id: int,
|
||||
product_data: ProductUpdate,
|
||||
session: SessionDep
|
||||
) -> Optional[Product]:
|
||||
"""Updates a product
|
||||
"""
|
||||
product = session.get(Product, product_id)
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
updated_fields = product_data.model_dump(exclude_unset=True)
|
||||
|
||||
for key, value in updated_fields.items():
|
||||
setattr(product, key, value)
|
||||
|
||||
session.add(product)
|
||||
session.commit()
|
||||
session.refresh(product)
|
||||
return product
|
||||
|
||||
|
||||
@router.delete("/{product_id}", status_code=204)
|
||||
def delete_product(product_id: int, session: SessionDep):
|
||||
"""Deletes a product
|
||||
"""
|
||||
product = session.get(Product, product_id)
|
||||
|
||||
if not product:
|
||||
raise HTTPException(status_code=404, detail="Product not found")
|
||||
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
@@ -0,0 +1,88 @@
|
||||
"""The suppliers endpoint
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from sqlmodel import select
|
||||
from app.api.deps import SessionDep, exists
|
||||
from app.schemas.models import Supplier
|
||||
from app.schemas.schemas import SupplierCreate, SupplierUpdate
|
||||
from pydantic import ValidationError
|
||||
from typing import Sequence, List, Optional
|
||||
|
||||
|
||||
router = APIRouter(prefix="/suppliers", tags=["suppliers"])
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Supplier])
|
||||
def fetch_suppliers(session: SessionDep) -> Sequence[Supplier]:
|
||||
"""Fetch supplier list
|
||||
"""
|
||||
suppliers = session.exec(select(Supplier)).all()
|
||||
return suppliers
|
||||
|
||||
|
||||
@router.post("/", response_model=SupplierCreate)
|
||||
def create_supplier(supplier_data: SupplierCreate, session: SessionDep) -> Supplier:
|
||||
"""Create a supplier
|
||||
"""
|
||||
existing = exists(session, Supplier, tin_number=supplier_data.tin_number)
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Supplier with this tin_number already exists")
|
||||
|
||||
try:
|
||||
supplier = Supplier.model_validate(supplier_data)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=400, detail=e.errors())
|
||||
|
||||
session.add(supplier)
|
||||
session.commit()
|
||||
session.refresh(supplier)
|
||||
return supplier
|
||||
|
||||
|
||||
@router.get("/{supplier_id}", response_model=Supplier)
|
||||
def get_supplier(supplier_id: int, session: SessionDep) -> Optional[Supplier]:
|
||||
"""Returns a supplier by its ID
|
||||
"""
|
||||
stmt = select(Supplier).where(Supplier.id == supplier_id)
|
||||
result: Optional[Supplier] = session.exec(stmt).first()
|
||||
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Supplier not found")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/{supplier_id}", response_model=SupplierUpdate)
|
||||
def update_supplier(
|
||||
supplier_id: int,
|
||||
supplier_data: SupplierUpdate,
|
||||
session: SessionDep
|
||||
) -> Optional[Supplier]:
|
||||
"""Updates a supplier's details
|
||||
"""
|
||||
supplier = session.get(Supplier, supplier_id)
|
||||
if not supplier:
|
||||
raise HTTPException(status_code=404, detail="Supplier not found")
|
||||
|
||||
updated_fields = supplier_data.model_dump(exclude_unset=True)
|
||||
|
||||
for key, value in updated_fields.items():
|
||||
setattr(supplier, key, value)
|
||||
|
||||
session.add(supplier)
|
||||
session.commit()
|
||||
session.refresh(supplier)
|
||||
return supplier
|
||||
|
||||
|
||||
@router.delete("/{supplier_id}", status_code=204)
|
||||
def delete_supplier(supplier_id: int, session: SessionDep):
|
||||
"""Deletes a supplier
|
||||
"""
|
||||
supplier = session.get(Supplier, supplier_id)
|
||||
|
||||
if not supplier:
|
||||
raise HTTPException(status_code=404, detail="Supplier not found")
|
||||
|
||||
session.delete(supplier)
|
||||
session.commit()
|
||||
Reference in New Issue
Block a user