126 lines
4.4 KiB
Python
126 lines
4.4 KiB
Python
'''
|
|
Author: caoziyuan ziyuan.cao@zhuying.com
|
|
Date: 2025-12-23 15:20:07
|
|
LastEditors: caoziyuan ziyuan.cao@zhuying.com
|
|
LastEditTime: 2025-12-23 15:41:59
|
|
FilePath: \naming-backend\app\api\v1\module_common\goodname\controller.py
|
|
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
'''
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import List
|
|
from fastapi import APIRouter, Query, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.common.response import SuccessResponse
|
|
from app.core.dependencies import db_getter, get_current_user
|
|
from app.api.v1.module_system.auth.schema import AuthSchema
|
|
from app.api.v1.module_common.goodname.crud import NameRecordCRUD
|
|
from app.api.v1.module_common.goodname.schema import NameRecordCreate, NameRecordUpdate, NameRecordOut
|
|
|
|
# 定义路由
|
|
GoodNameRouter = APIRouter(prefix="/goodname", tags=["佳名赏析"])
|
|
|
|
|
|
@GoodNameRouter.get("/list", summary="获取佳名列表", description="获取佳名赏析列表(无需登录)")
|
|
async def get_good_name_list(
|
|
limit: int = Query(default=3, ge=1, le=50, description="返回数量"),
|
|
db: AsyncSession = Depends(db_getter)
|
|
) -> SuccessResponse:
|
|
"""
|
|
获取佳名赏析列表
|
|
|
|
返回格式:
|
|
[
|
|
{"name": "清芷", "source": "楚辞", "desc": "岸芷汀兰,郁郁青青"},
|
|
{"name": "云帆", "source": "行路难", "desc": "长风破浪会有时,直挂云帆济沧海"},
|
|
...
|
|
]
|
|
"""
|
|
auth = AuthSchema(db=db, check_data_scope=False)
|
|
crud = NameRecordCRUD(auth)
|
|
data = await crud.get_good_names_for_display(limit)
|
|
return SuccessResponse(data=data)
|
|
|
|
|
|
@GoodNameRouter.get("/random", summary="随机获取佳名", description="随机获取佳名赏析(无需登录)")
|
|
async def get_random_good_names(
|
|
count: int = Query(default=3, ge=1, le=10, description="返回数量"),
|
|
db: AsyncSession = Depends(db_getter)
|
|
) -> SuccessResponse:
|
|
"""
|
|
随机获取佳名
|
|
"""
|
|
auth = AuthSchema(db=db, check_data_scope=False)
|
|
crud = NameRecordCRUD(auth)
|
|
data = await crud.get_random_names(count)
|
|
return SuccessResponse(data=data)
|
|
|
|
|
|
@GoodNameRouter.post("/create", summary="创建起名记录", description="创建起名记录(需要登录)")
|
|
async def create_name_record(
|
|
data: NameRecordCreate,
|
|
auth: AuthSchema = Depends(get_current_user)
|
|
) -> SuccessResponse:
|
|
"""
|
|
创建起名记录
|
|
"""
|
|
crud = NameRecordCRUD(auth)
|
|
result = await crud.create(data)
|
|
return SuccessResponse(data={"id": result.id}, msg="创建成功")
|
|
|
|
|
|
@GoodNameRouter.get("/my", summary="获取我的起名记录", description="获取当前用户的起名记录(需要登录)")
|
|
async def get_my_name_records(
|
|
page: int = Query(default=1, ge=1, description="页码"),
|
|
size: int = Query(default=10, ge=1, le=100, description="每页数量"),
|
|
auth: AuthSchema = Depends(get_current_user)
|
|
) -> SuccessResponse:
|
|
"""
|
|
获取我的起名记录
|
|
"""
|
|
crud = NameRecordCRUD(auth)
|
|
offset = (page - 1) * size
|
|
result = await crud.page(
|
|
offset=offset,
|
|
limit=size,
|
|
order_by=[{"created_time": "desc"}],
|
|
search={"created_id": auth.user.id},
|
|
out_schema=NameRecordOut
|
|
)
|
|
return SuccessResponse(data=result)
|
|
|
|
|
|
@GoodNameRouter.put("/favorite/{record_id}", summary="收藏/取消收藏", description="收藏或取消收藏起名记录(需要登录)")
|
|
async def toggle_favorite(
|
|
record_id: int,
|
|
auth: AuthSchema = Depends(get_current_user)
|
|
) -> SuccessResponse:
|
|
"""
|
|
收藏/取消收藏起名记录
|
|
"""
|
|
crud = NameRecordCRUD(auth)
|
|
record = await crud.get(id=record_id)
|
|
if not record:
|
|
return SuccessResponse(msg="记录不存在", success=False)
|
|
|
|
# 切换收藏状态
|
|
new_status = 0 if record.is_favorite == 1 else 1
|
|
await crud.update(record_id, {"is_favorite": new_status})
|
|
|
|
msg = "收藏成功" if new_status == 1 else "已取消收藏"
|
|
return SuccessResponse(msg=msg)
|
|
|
|
|
|
@GoodNameRouter.delete("/delete/{record_id}", summary="删除起名记录", description="删除起名记录(需要登录)")
|
|
async def delete_name_record(
|
|
record_id: int,
|
|
auth: AuthSchema = Depends(get_current_user)
|
|
) -> SuccessResponse:
|
|
"""
|
|
删除起名记录
|
|
"""
|
|
crud = NameRecordCRUD(auth)
|
|
await crud.delete([record_id])
|
|
return SuccessResponse(msg="删除成功")
|