| 1234567891011121314151617181920212223242526 |
- # backend/app/services/category_service.py
- from sqlalchemy.orm import Session
- from fastapi import HTTPException, status
- from ..models import Category, Spider
- class CategoryService:
- @staticmethod
- def get_category_by_id(db: Session, category_id: int) -> Category:
- category = db.query(Category).filter(Category.id == category_id).first()
- if not category:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="分类不存在"
- )
- return category
-
- @staticmethod
- def check_category_name_exists(db: Session, name: str, exclude_id: int = None) -> bool:
- query = db.query(Category).filter(Category.name == name)
- if exclude_id:
- query = query.filter(Category.id != exclude_id)
- return query.first() is not None
-
- @staticmethod
- def get_spider_count(db: Session, category_id: int) -> int:
- return db.query(Spider).filter(Spider.category_id == category_id).count()
|