feat(douyu): 增加协作单礼包字典与权限控制
This commit is contained in:
@@ -29,6 +29,9 @@ from ..schemas import (
|
||||
DouyuCollabArchiveRequest,
|
||||
DouyuCollabCkExportOut,
|
||||
DouyuCollabCkExportRequest,
|
||||
DouyuCollabGiftCreate,
|
||||
DouyuCollabGiftOut,
|
||||
DouyuCollabGiftUpdate,
|
||||
DouyuCollabGoodsOut,
|
||||
DouyuCollabImagesRequest,
|
||||
DouyuCollabMetricOut,
|
||||
@@ -75,6 +78,21 @@ def _shop_out(shop) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _gift_out(gift) -> dict:
|
||||
return {
|
||||
"id": int(gift.id),
|
||||
"name": gift.name,
|
||||
"points": int(gift.points or 0),
|
||||
"sort_order": int(gift.sort_order or 0),
|
||||
"is_active": bool(gift.is_active),
|
||||
"remark": gift.remark or "",
|
||||
"created_by": gift.created_by,
|
||||
"updated_by": gift.updated_by,
|
||||
"created_at": gift.created_at,
|
||||
"updated_at": gift.updated_at,
|
||||
}
|
||||
|
||||
|
||||
def _publish_change(current: User, *, scope: str, action: str) -> None:
|
||||
collab_events.publish(scope=scope, action=action, actor_id=int(current.id))
|
||||
|
||||
@@ -693,3 +711,61 @@ def delete_collab_shop(
|
||||
raise _http_error(exc) from exc
|
||||
_publish_change(current, scope="shops", action="shop.delete")
|
||||
return {**result, "id": shop_id}
|
||||
|
||||
|
||||
@router.get("/collab-gifts", response_model=list[DouyuCollabGiftOut])
|
||||
def list_collab_gifts(
|
||||
db: Session = Depends(get_db),
|
||||
_current: User = Depends(require_permission("douyu:collab:view")),
|
||||
):
|
||||
return [_gift_out(gift) for gift in collab.list_gifts(db)]
|
||||
|
||||
|
||||
@router.post("/collab-gifts", response_model=DouyuCollabGiftOut)
|
||||
def create_collab_gift(
|
||||
req: DouyuCollabGiftCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:collab:manage")),
|
||||
):
|
||||
try:
|
||||
gift = collab.create_gift(
|
||||
db,
|
||||
current,
|
||||
name=req.name,
|
||||
points=req.points,
|
||||
remark=req.remark,
|
||||
)
|
||||
except collab.CollabError as exc:
|
||||
raise _http_error(exc) from exc
|
||||
_publish_change(current, scope="gifts", action="gift.create")
|
||||
return _gift_out(gift)
|
||||
|
||||
|
||||
@router.put("/collab-gifts/{gift_id}", response_model=DouyuCollabGiftOut)
|
||||
def update_collab_gift(
|
||||
gift_id: int,
|
||||
req: DouyuCollabGiftUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:collab:manage")),
|
||||
):
|
||||
try:
|
||||
gift = collab.get_gift(db, gift_id)
|
||||
collab.update_gift(db, current, gift, req.model_dump(exclude_unset=True))
|
||||
except collab.CollabError as exc:
|
||||
raise _http_error(exc) from exc
|
||||
_publish_change(current, scope="gifts", action="gift.update")
|
||||
return _gift_out(gift)
|
||||
|
||||
|
||||
@router.delete("/collab-gifts/{gift_id}")
|
||||
def delete_collab_gift(
|
||||
gift_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:collab:manage")),
|
||||
):
|
||||
try:
|
||||
result = collab.delete_gift(db, current, collab.get_gift(db, gift_id))
|
||||
except collab.CollabError as exc:
|
||||
raise _http_error(exc) from exc
|
||||
_publish_change(current, scope="gifts", action="gift.delete")
|
||||
return {**result, "id": gift_id}
|
||||
|
||||
Reference in New Issue
Block a user