Filaments can now be created
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
"""Filament related endpoints."""
|
||||
|
||||
from typing import Union
|
||||
from fastapi import APIRouter
|
||||
from typing import Annotated, Optional, Union
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from spoolson.api.v1.models import Filament, Vendor
|
||||
from spoolson.database.database import get_db_session
|
||||
from spoolson.database.filament import create_filament
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/filament",
|
||||
@@ -13,13 +18,32 @@ router = APIRouter(
|
||||
# ruff: noqa: D103
|
||||
|
||||
|
||||
class FilamentParameters(BaseModel):
|
||||
name: Optional[str] = Field(
|
||||
max_length=64,
|
||||
description=(
|
||||
"Filament name, to distinguish this filament type among others from the same vendor."
|
||||
"Should contain its color for example."
|
||||
),
|
||||
)
|
||||
vendor_id: Optional[int] = Field(description="The ID of the vendor of this filament type.")
|
||||
material: Optional[str] = Field(max_length=64, description="The material of this filament, e.g. PLA.")
|
||||
price: Optional[float] = Field(ge=0, description="The price of this filament in the system configured currency.")
|
||||
density: float = Field(gt=0, description="The density of this filament in g/cm3.")
|
||||
diameter: float = Field(gt=0, description="The diameter of this filament in mm.")
|
||||
weight: Optional[float] = Field(gt=0, description="The weight of the filament in a full spool.")
|
||||
spool_weight: Optional[float] = Field(gt=0, description="The empty spool weight.")
|
||||
article_number: Optional[str] = Field(max_length=64, description="Vendor article number, e.g. EAN, QR code, etc.")
|
||||
comment: Optional[str] = Field(max_length=1024, description="Free text comment about this filament type.")
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def find(vendor: Union[int, None] = None) -> list[Filament]:
|
||||
async def find(_vendor: Union[int, None] = None) -> list[Filament]:
|
||||
return []
|
||||
|
||||
|
||||
@router.get("/{filament_id}")
|
||||
async def get(filament_id: int) -> Filament:
|
||||
async def get(_filament_id: int) -> Filament:
|
||||
return Filament(
|
||||
id=0,
|
||||
name=None,
|
||||
@@ -40,25 +64,26 @@ async def get(filament_id: int) -> Filament:
|
||||
|
||||
|
||||
@router.post("/")
|
||||
async def create() -> Filament:
|
||||
return Filament(
|
||||
id=0,
|
||||
name=None,
|
||||
vendor=Vendor(
|
||||
id=0,
|
||||
name="asdf",
|
||||
comment=None,
|
||||
),
|
||||
material=None,
|
||||
price=None,
|
||||
density=1,
|
||||
diameter=1,
|
||||
weight=None,
|
||||
spool_weight=None,
|
||||
article_number=None,
|
||||
comment=None,
|
||||
async def create(
|
||||
db: Annotated[AsyncSession, Depends(get_db_session)],
|
||||
body: FilamentParameters,
|
||||
) -> Filament:
|
||||
db_item = await create_filament(
|
||||
db=db,
|
||||
density=body.density,
|
||||
diameter=body.diameter,
|
||||
name=body.name,
|
||||
vendor_id=body.vendor_id,
|
||||
material=body.material,
|
||||
price=body.price,
|
||||
weight=body.weight,
|
||||
spool_weight=body.spool_weight,
|
||||
article_number=body.article_number,
|
||||
comment=body.comment,
|
||||
)
|
||||
|
||||
return Filament.from_db(db_item)
|
||||
|
||||
|
||||
@router.put("/{filament_id}")
|
||||
async def update() -> Filament:
|
||||
@@ -82,5 +107,5 @@ async def update() -> Filament:
|
||||
|
||||
|
||||
@router.delete("/{filament_id}")
|
||||
async def delete():
|
||||
async def delete() -> None:
|
||||
pass
|
||||
|
||||
@@ -5,12 +5,23 @@ from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from spoolson.database import models
|
||||
|
||||
|
||||
class Vendor(BaseModel):
|
||||
id: int = Field(description="Unique internal ID of this vendor.")
|
||||
name: str = Field(max_length=64, description="Vendor name.")
|
||||
comment: Optional[str] = Field(max_length=1024, description="Free text comment about this vendor.")
|
||||
|
||||
@staticmethod
|
||||
def from_db(item: models.Vendor) -> "Vendor":
|
||||
"""Create a new Pydantic vendor object from a database vendor object."""
|
||||
return Vendor(
|
||||
id=item.id,
|
||||
name=item.name,
|
||||
comment=item.comment,
|
||||
)
|
||||
|
||||
|
||||
class Filament(BaseModel):
|
||||
id: int = Field(description="Unique internal ID of this filament type.")
|
||||
@@ -31,6 +42,23 @@ class Filament(BaseModel):
|
||||
article_number: Optional[str] = Field(max_length=64, description="Vendor article number, e.g. EAN, QR code, etc.")
|
||||
comment: Optional[str] = Field(max_length=1024, description="Free text comment about this filament type.")
|
||||
|
||||
@staticmethod
|
||||
def from_db(item: models.Filament) -> "Filament":
|
||||
"""Create a new Pydantic filament object from a database filament object."""
|
||||
return Filament(
|
||||
id=item.id,
|
||||
name=item.name,
|
||||
vendor=Vendor.from_db(item.vendor) if item.vendor is not None else None,
|
||||
material=item.material,
|
||||
price=item.price,
|
||||
density=item.density,
|
||||
diameter=item.diameter,
|
||||
weight=item.weight,
|
||||
spool_weight=item.spool_weight,
|
||||
article_number=item.article_number,
|
||||
comment=item.comment,
|
||||
)
|
||||
|
||||
|
||||
class Spool(BaseModel):
|
||||
id: int = Field(description="Unique internal ID of this spool of filament.")
|
||||
@@ -42,3 +70,18 @@ class Spool(BaseModel):
|
||||
location: Optional[str] = Field(max_length=64, description="Where this spool can be found.")
|
||||
lot_nr: Optional[str] = Field(max_length=64, description="Vendor manufacturing lot/batch number of the spool.")
|
||||
comment: Optional[str] = Field(max_length=1024, description="Free text comment about this specific spool.")
|
||||
|
||||
@staticmethod
|
||||
def from_db(item: models.Spool) -> "Spool":
|
||||
"""Create a new Pydantic spool object from a database spool object."""
|
||||
return Spool(
|
||||
id=item.id,
|
||||
registered=item.registered,
|
||||
first_used=item.first_used,
|
||||
last_used=item.last_used,
|
||||
filament=Filament.from_db(item.filament),
|
||||
weight=item.weight,
|
||||
location=item.location,
|
||||
lot_nr=item.lot_nr,
|
||||
comment=item.comment,
|
||||
)
|
||||
|
||||
@@ -1,21 +1,31 @@
|
||||
"""Router setup for the v1 version of the API."""
|
||||
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
|
||||
from spoolson.exceptions import ItemNotFoundError
|
||||
|
||||
from . import filament, spool, vendor
|
||||
|
||||
# ruff: noqa: D103
|
||||
|
||||
app = FastAPI(
|
||||
title="Spoolson REST API v1",
|
||||
version="1.0.0",
|
||||
root_path_in_servers=False,
|
||||
)
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/v1",
|
||||
)
|
||||
|
||||
router.include_router(filament.router)
|
||||
router.include_router(spool.router)
|
||||
router.include_router(vendor.router)
|
||||
@app.exception_handler(ItemNotFoundError)
|
||||
async def itemnotfounderror_exception_handler(_request: Request, exc: ItemNotFoundError) -> Response:
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={"message": str(exc)},
|
||||
)
|
||||
|
||||
app.include_router(router)
|
||||
|
||||
app.include_router(filament.router)
|
||||
app.include_router(spool.router)
|
||||
app.include_router(vendor.router)
|
||||
|
||||
Reference in New Issue
Block a user