1 Star 0 Fork 0

AllanWong/FastAPI_and_Nextjs_WebScraping_WebCrawling

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
crud.py 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
Elanchezhian2712 提交于 2024-08-20 16:22 . newOne
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/allanwong/FastAPI_and_Nextjs_WebScraping_WebCrawling.git
[email protected]:allanwong/FastAPI_and_Nextjs_WebScraping_WebCrawling.git
allanwong
FastAPI_and_Nextjs_WebScraping_WebCrawling
FastAPI_and_Nextjs_WebScraping_WebCrawling
main

搜索帮助