48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from core.models.soap import Soap
|
|
from core.schemas.soap import SoapCreate, SoapSchema
|
|
|
|
def get_all_soaps(db: Session) -> List[SoapSchema]:
|
|
"""
|
|
Retrieves all soaps from the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
|
|
Returns:
|
|
List[SoapSchema]: A list of all soap objects.
|
|
"""
|
|
soaps = db.query(Soap).all()
|
|
return [SoapSchema.from_orm(soap) for soap in soaps]
|
|
|
|
def create_soap(db: Session, soap_data: SoapCreate) -> SoapSchema:
|
|
"""
|
|
Creates a new soap in the database.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
soap_data (SoapCreate): The data for the soap to create.
|
|
|
|
Returns:
|
|
SoapSchema: The newly created soap object.
|
|
"""
|
|
db_soap = Soap(name=soap_data.name)
|
|
db.add(db_soap)
|
|
db.commit()
|
|
db.refresh(db_soap)
|
|
return SoapSchema.from_orm(db_soap)
|
|
|
|
def get_soap_by_name(db: Session, name: str) -> Optional[SoapSchema]:
|
|
"""
|
|
Retrieves a single soap by its name.
|
|
|
|
Args:
|
|
db (Session): The database session.
|
|
name (str): The name of the soap to retrieve.
|
|
|
|
Returns:
|
|
Optional[SoapSchema]: The soap object if found, otherwise None.
|
|
"""
|
|
soap = db.query(Soap).filter(Soap.name == name).first()
|
|
return SoapSchema.from_orm(soap) if soap else None |