This commit is contained in:
aaron 2025-04-10 19:01:37 +08:00
parent a26f60df6a
commit 8508aeb8b9
5 changed files with 13 additions and 11 deletions

View File

@ -113,7 +113,7 @@ async def create_clothing(
if category is None: if category is None:
raise BusinessError("指定的分类不存在", code=404) raise BusinessError("指定的分类不存在", code=404)
clothing = await clothing_service.create_clothing(db=db, clothing=clothing) clothing = await clothing_service.create_clothing(db=db, clothing=clothing, user_id=current_user.id)
logger.info(f"创建衣服成功: id={clothing.id}") logger.info(f"创建衣服成功: id={clothing.id}")
# 手动返回标准响应格式 # 手动返回标准响应格式
return StandardResponse(code=200, data=Clothing.model_validate(clothing)) return StandardResponse(code=200, data=Clothing.model_validate(clothing))

View File

@ -35,11 +35,11 @@ async def create_person_image(
*, *,
db: AsyncSession = Depends(deps.get_db), db: AsyncSession = Depends(deps.get_db),
current_user: User = Depends(deps.get_current_user), current_user: User = Depends(deps.get_current_user),
image_in: PersonImage image_in: PersonImageCreate
): ):
"""创建新的人物形象""" """创建新的人物形象"""
image_in.user_id = current_user.id image_in.user_id = current_user.id
image = await person_image_service.create_person_image(db=db, image=image_in) image = await person_image_service.create_person_image(db=db, image=image_in, user_id= current_user.id)
return StandardResponse(code=200, message="创建人物形象成功", data=image) return StandardResponse(code=200, message="创建人物形象成功", data=image)

View File

@ -8,9 +8,10 @@ class PersonImageBase(BaseModel):
image_url: str image_url: str
is_default: bool = False is_default: bool = False
class PersonImageCreate(PersonImageBase): class PersonImageCreate(BaseModel):
"""创建人物形象请求模型""" """创建人物形象请求模型"""
pass image_url: str
is_default: bool = True
class PersonImageUpdate(PersonImageBase): class PersonImageUpdate(PersonImageBase):
"""更新人物形象请求模型""" """更新人物形象请求模型"""

View File

@ -84,11 +84,12 @@ async def get_clothes_by_category(db: AsyncSession, category_id: int, skip: int
) )
return result.scalars().all() return result.scalars().all()
async def create_clothing(db: AsyncSession, clothing: ClothingCreate): async def create_clothing(db: AsyncSession, clothing: ClothingCreate, user_id: int):
"""创建衣服""" """创建衣服"""
db_clothing = Clothing( db_clothing = Clothing(
clothing_category_id=clothing.clothing_category_id, clothing_category_id=clothing.clothing_category_id,
image_url=clothing.image_url image_url=clothing.image_url,
user_id=user_id
) )
db.add(db_clothing) db.add(db_clothing)
await db.commit() await db.commit()

View File

@ -21,17 +21,17 @@ async def get_person_images_by_user(db: AsyncSession, user_id: int, skip: int =
) )
return result.scalars().all() return result.scalars().all()
async def create_person_image(db: AsyncSession, image: PersonImageCreate): async def create_person_image(db: AsyncSession, image: PersonImageCreate, user_id: int):
"""创建人物形象 """创建人物形象
image: 包含user_id的PersonImageCreate对象 image: 包含user_id的PersonImageCreate对象
""" """
# 如果设置为默认形象,先重置用户的所有形象为非默认 # 如果设置为默认形象,先重置用户的所有形象为非默认
if image.is_default and image.user_id: if image.is_default:
await reset_default_images(db, image.user_id) await reset_default_images(db, user_id)
db_image = PersonImage( db_image = PersonImage(
user_id=image.user_id, user_id=user_id,
image_url=image.image_url, image_url=image.image_url,
is_default=image.is_default is_default=image.is_default
) )