feat(diary): 添加贴纸包 UpdateStickerPackReq DTO + update service/handler — Task 13
Some checks failed
Main Merge / backend (push) Has been cancelled
Main Merge / frontend (push) Has been cancelled

This commit is contained in:
iven
2026-06-02 23:54:04 +08:00
parent d6dd017155
commit 45530616ee
4 changed files with 130 additions and 3 deletions

View File

@@ -223,6 +223,23 @@ pub struct CreateStickerPackReq {
fn default_true() -> bool { true }
/// 更新贴纸包请求 — 所有字段可选,仅更新传入的字段
#[derive(Debug, Deserialize, ToSchema)]
pub struct UpdateStickerPackReq {
/// 贴纸包名称
pub name: Option<String>,
/// 描述
pub description: Option<String>,
/// 缩略图 URL
pub thumbnail_url: Option<String>,
/// 是否免费
pub is_free: Option<bool>,
/// 价格(积分)
pub price: Option<i32>,
/// 分类
pub category: Option<String>,
}
/// 创建贴纸请求
#[derive(Debug, Deserialize, ToSchema)]
pub struct CreateStickerReq {

View File

@@ -10,7 +10,7 @@ use erp_core::error::AppError;
use erp_core::rbac::require_permission;
use erp_core::types::{ApiResponse, TenantContext};
use crate::dto::{CreateStickerPackReq, CreateStickerReq, StickerPackResp, StickerResp, TemplateResp};
use crate::dto::{CreateStickerPackReq, CreateStickerReq, StickerPackResp, StickerResp, TemplateResp, UpdateStickerPackReq};
use crate::service::sticker_service::StickerService;
use crate::state::DiaryState;
@@ -127,6 +127,54 @@ where
Ok(Json(ApiResponse::ok(resp)))
}
#[utoipa::path(
put,
path = "/api/v1/diary/sticker-packs/{pack_id}",
params(("pack_id" = Uuid, Path, description = "贴纸包ID")),
request_body = UpdateStickerPackReq,
responses(
(status = 200, description = "更新成功", body = ApiResponse<StickerPackResp>),
(status = 400, description = "验证失败"),
(status = 401, description = "未授权"),
(status = 403, description = "权限不足"),
(status = 404, description = "贴纸包不存在"),
),
security(("bearer_auth" = [])),
tag = "贴纸管理"
)]
/// PUT /api/v1/diary/sticker-packs/:pack_id
///
/// 更新贴纸包(部分更新)。需要 `diary.class.manage` 权限。
pub async fn update_sticker_pack<S>(
State(state): State<DiaryState>,
Extension(ctx): Extension<TenantContext>,
Path(pack_id): Path<Uuid>,
Json(req): Json<UpdateStickerPackReq>,
) -> Result<Json<ApiResponse<StickerPackResp>>, AppError>
where
DiaryState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "diary.class.manage")?;
if let Some(ref name) = req.name {
if name.trim().is_empty() {
return Err(AppError::Validation("贴纸包名称不能为空".to_string()));
}
}
let resp = StickerService::update_sticker_pack(
ctx.tenant_id,
pack_id,
ctx.user_id,
&req,
&state.db,
)
.await?;
Ok(Json(ApiResponse::ok(resp)))
}
#[utoipa::path(
delete,
path = "/api/v1/diary/sticker-packs/{pack_id}",

View File

@@ -199,7 +199,8 @@ impl DiaryModule {
)
.route(
"/diary/sticker-packs/{pack_id}",
axum::routing::delete(sticker_handler::delete_sticker_pack),
axum::routing::put(sticker_handler::update_sticker_pack)
.delete(sticker_handler::delete_sticker_pack),
)
.route(
"/diary/sticker-packs/{pack_id}/stickers",

View File

@@ -6,7 +6,7 @@ use sea_orm::{
};
use uuid::Uuid;
use crate::dto::{CreateStickerPackReq, CreateStickerReq, StickerPackResp, StickerResp, TemplateResp};
use crate::dto::{CreateStickerPackReq, CreateStickerReq, StickerPackResp, StickerResp, TemplateResp, UpdateStickerPackReq};
use crate::entity::{sticker, sticker_pack, template};
use crate::error::{DiaryError, DiaryResult};
@@ -192,6 +192,67 @@ impl StickerService {
})
}
/// 更新贴纸包(部分更新,仅修改传入的字段)
pub async fn update_sticker_pack(
tenant_id: Uuid,
pack_id: Uuid,
user_id: Uuid,
req: &UpdateStickerPackReq,
db: &DatabaseConnection,
) -> DiaryResult<StickerPackResp> {
let model = sticker_pack::Entity::find()
.filter(sticker_pack::Column::Id.eq(pack_id))
.filter(sticker_pack::Column::TenantId.eq(tenant_id))
.filter(sticker_pack::Column::DeletedAt.is_null())
.one(db)
.await?
.ok_or_else(|| DiaryError::NotFound(format!("贴纸包 {} 不存在", pack_id)))?;
let now = chrono::Utc::now();
let mut active: sticker_pack::ActiveModel = model.into();
if let Some(ref name) = req.name {
active.name = Set(name.clone());
}
if let Some(ref description) = req.description {
active.description = Set(Some(description.clone()));
}
if let Some(ref thumbnail_url) = req.thumbnail_url {
active.thumbnail_url = Set(Some(thumbnail_url.clone()));
}
if let Some(is_free) = req.is_free {
active.is_free = Set(is_free);
}
if let Some(price) = req.price {
active.price = Set(price);
}
if let Some(ref category) = req.category {
active.category = Set(Some(category.clone()));
}
active.updated_at = Set(now);
active.updated_by = Set(user_id);
let updated = active.update(db).await?;
// 查询贴纸数量
let sticker_count = sticker::Entity::find()
.filter(sticker::Column::PackId.eq(updated.id))
.filter(sticker::Column::DeletedAt.is_null())
.count(db)
.await? as i32;
Ok(StickerPackResp {
id: updated.id,
name: updated.name,
description: updated.description,
cover_image_url: updated.thumbnail_url,
sticker_count,
is_free: updated.is_free,
category: updated.category,
})
}
/// 删除贴纸包(软删除)
pub async fn delete_sticker_pack(
tenant_id: Uuid,