add products
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
from enum import Enum
|
||||
from enum import StrEnum
|
||||
from typing import Optional
|
||||
import datetime
|
||||
|
||||
@@ -44,14 +44,14 @@ class ProductorUpdate(SQLModel):
|
||||
class ProductorCreate(ProductorBase):
|
||||
pass
|
||||
|
||||
class Unit(Enum):
|
||||
GRAMS = 1
|
||||
KILO = 2
|
||||
PIECE = 3
|
||||
class Unit(StrEnum):
|
||||
GRAMS = "1"
|
||||
KILO = "2"
|
||||
PIECE = "3"
|
||||
|
||||
class ProductType(Enum):
|
||||
PLANNED = 1
|
||||
RECCURENT = 2
|
||||
class ProductType(StrEnum):
|
||||
PLANNED = "1"
|
||||
RECCURENT = "2"
|
||||
|
||||
class ShipmentProductLink(SQLModel, table=True):
|
||||
shipment_id: Optional[int] = Field(default=None, foreign_key="shipment.id", primary_key=True)
|
||||
@@ -62,7 +62,7 @@ class ProductBase(SQLModel):
|
||||
unit: Unit
|
||||
price: float
|
||||
price_kg: float | None
|
||||
weight: float
|
||||
weight: float | None
|
||||
type: ProductType
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
|
||||
@@ -83,10 +83,10 @@ class ProductUpdate(SQLModel):
|
||||
price_kg: float | None
|
||||
weight: float | None
|
||||
productor_id: int | None
|
||||
shipment_ids: list[int] | None
|
||||
shipment_ids: list[int] | None = []
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
shipment_ids: list[int] | None
|
||||
shipment_ids: list[int] | None = []
|
||||
|
||||
class FormBase(SQLModel):
|
||||
name: str
|
||||
|
||||
@@ -4,9 +4,9 @@ import src.models as models
|
||||
def get_all(session: Session, names: list[str], types: list[str]) -> list[models.ProductorPublic]:
|
||||
statement = select(models.Productor)
|
||||
if len(names) > 0:
|
||||
statement.where(models.Productor.name.in_(names))
|
||||
statement = statement.where(models.Productor.name.in_(names))
|
||||
if len(types) > 0:
|
||||
statement.where(models.Productor.type.in_(types))
|
||||
statement = statement.where(models.Productor.type.in_(types))
|
||||
return session.exec(statement).all()
|
||||
|
||||
def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
import src.messages as messages
|
||||
import src.models as models
|
||||
from src.database import get_session
|
||||
@@ -8,8 +8,12 @@ from src.auth.auth import get_current_user
|
||||
router = APIRouter(prefix='/products')
|
||||
#user=Depends(get_current_user)
|
||||
@router.get('/', response_model=list[models.ProductPublic], )
|
||||
def get_products(session: Session = Depends(get_session)):
|
||||
return service.get_all(session)
|
||||
def get_products(
|
||||
session: Session = Depends(get_session),
|
||||
names: list[str] = Query([]),
|
||||
productors: list[str] = Query([]),
|
||||
):
|
||||
return service.get_all(session, names, productors)
|
||||
|
||||
@router.get('/{id}', response_model=models.ProductPublic)
|
||||
def get_product(id: int, session: Session = Depends(get_session)):
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
|
||||
def get_all(session: Session) -> list[models.ProductPublic]:
|
||||
def get_all(
|
||||
session: Session,
|
||||
names: list[str],
|
||||
productors: list[str]
|
||||
) -> list[models.ProductPublic]:
|
||||
statement = select(models.Product)
|
||||
if len(names) > 0:
|
||||
statement = statement.where(models.Product.name.in_(names))
|
||||
if len(productors) > 0:
|
||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
||||
return session.exec(statement).all()
|
||||
|
||||
def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
|
||||
Reference in New Issue
Block a user