代码拉取完成,页面将自动刷新
from sqlalchemy.orm import Session
import models, schemas
# Create a product
def create_product(db: Session, product: schemas.ProductCreate):
db_product = models.Product(name=product.name, link=product.link)
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product
# Get a list of products with pagination
def get_products(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.Product).offset(skip).limit(limit).all()
# Get a single product by ID
def get_product(db: Session, product_id: int):
return db.query(models.Product).filter(models.Product.id == product_id).first()
# Search products by name (simple search)
def search_products(db: Session, query: str):
return db.query(models.Product).filter(models.Product.name.ilike(f"%{query}%")).all()
# Delete a product by ID
def delete_product(db: Session, product_id: int):
db_product = db.query(models.Product).filter(models.Product.id == product_id).first()
if db_product:
db.delete(db_product)
db.commit()
return db_product
return None
# Delete all products
def delete_all_products(db: Session):
deleted_count = db.query(models.Product).delete()
db.commit()
return deleted_count
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。