120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.future import select
|
|
from sqlalchemy import update, delete, func
|
|
from typing import List, Optional
|
|
|
|
from app.models.clothing import Clothing, ClothingCategory
|
|
from app.schemas.clothing import ClothingCreate, ClothingUpdate
|
|
from app.schemas.clothing import ClothingCategoryCreate, ClothingCategoryUpdate
|
|
from app.models.users import User
|
|
from fastapi import Depends
|
|
|
|
# 衣服分类服务函数
|
|
async def get_category(db: AsyncSession, category_id: int):
|
|
"""获取单个衣服分类"""
|
|
result = await db.execute(select(ClothingCategory).filter(ClothingCategory.id == category_id))
|
|
return result.scalars().first()
|
|
|
|
async def get_categories(db: AsyncSession, skip: int = 0, limit: int = 100):
|
|
"""获取所有衣服分类"""
|
|
result = await db.execute(
|
|
select(ClothingCategory)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
return result.scalars().all()
|
|
|
|
async def create_category(db: AsyncSession, category: ClothingCategoryCreate):
|
|
"""创建衣服分类"""
|
|
db_category = ClothingCategory(
|
|
name=category.name
|
|
)
|
|
db.add(db_category)
|
|
await db.commit()
|
|
await db.refresh(db_category)
|
|
return db_category
|
|
|
|
async def update_category(db: AsyncSession, category_id: int, category_update: ClothingCategoryUpdate):
|
|
"""更新衣服分类"""
|
|
db_category = await get_category(db, category_id)
|
|
if not db_category:
|
|
return None
|
|
|
|
if category_update.name:
|
|
db_category.name = category_update.name
|
|
|
|
await db.commit()
|
|
await db.refresh(db_category)
|
|
return db_category
|
|
|
|
async def delete_category(db: AsyncSession, category_id: int):
|
|
"""删除衣服分类(会级联删除相关衣服)"""
|
|
db_category = await get_category(db, category_id)
|
|
if db_category:
|
|
await db.delete(db_category)
|
|
await db.commit()
|
|
return db_category
|
|
|
|
# 衣服服务函数
|
|
async def get_clothing(db: AsyncSession, clothing_id: int):
|
|
"""获取单个衣服"""
|
|
result = await db.execute(select(Clothing).filter(Clothing.id == clothing_id))
|
|
return result.scalars().first()
|
|
|
|
async def get_clothes(db: AsyncSession, skip: int = 0, limit: int = 100, user_id: int = None):
|
|
"""获取所有衣服"""
|
|
result = await db.execute(
|
|
select(Clothing)
|
|
.filter(Clothing.user_id == user_id)
|
|
.order_by(Clothing.create_time.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
return result.scalars().all()
|
|
|
|
async def get_clothes_by_category(db: AsyncSession, category_id: int, skip: int = 0, limit: int = 100):
|
|
"""根据分类获取衣服"""
|
|
query = select(Clothing).order_by(Clothing.create_time.desc())
|
|
if category_id > 0:
|
|
query = query.filter(Clothing.clothing_category_id == category_id)
|
|
result = await db.execute(
|
|
query
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
return result.scalars().all()
|
|
|
|
async def create_clothing(db: AsyncSession, clothing: ClothingCreate):
|
|
"""创建衣服"""
|
|
db_clothing = Clothing(
|
|
clothing_category_id=clothing.clothing_category_id,
|
|
image_url=clothing.image_url
|
|
)
|
|
db.add(db_clothing)
|
|
await db.commit()
|
|
await db.refresh(db_clothing)
|
|
return db_clothing
|
|
|
|
async def update_clothing(db: AsyncSession, clothing_id: int, clothing_update: ClothingUpdate):
|
|
"""更新衣服"""
|
|
db_clothing = await get_clothing(db, clothing_id)
|
|
if not db_clothing:
|
|
return None
|
|
|
|
if clothing_update.clothing_category_id is not None:
|
|
db_clothing.clothing_category_id = clothing_update.clothing_category_id
|
|
|
|
if clothing_update.image_url:
|
|
db_clothing.image_url = clothing_update.image_url
|
|
|
|
await db.commit()
|
|
await db.refresh(db_clothing)
|
|
return db_clothing
|
|
|
|
async def delete_clothing(db: AsyncSession, clothing_id: int):
|
|
"""删除衣服"""
|
|
db_clothing = await get_clothing(db, clothing_id)
|
|
if db_clothing:
|
|
await db.delete(db_clothing)
|
|
await db.commit()
|
|
return db_clothing |