main
use ::session::AuthedUser;
use api::http::{Json, Path, Query, State};
use api::rest::{AppResponseData, NoData};
use uuid::Uuid;
use crate::errors::ApiError;
use crate::services::bookmarks::{BookmarkDto, CreateBookmarkCmd, UpdateBookmarkCmd};
use crate::services::service::ChatService;
#[utoipa::path(
get,
path = "/chat/rooms/{room_id}/bookmarks",
tag = "chat",
description = "按排序位置读取指定房间的书签列表。",
params(("room_id" = Uuid, Path, description = "room id"), api::pagination::PageQuery),
responses(
(status = 200, description = "bookmark list", body = AppResponseData<api::pagination::Page<BookmarkDto>>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn list_bookmarks(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
Query(page): Query<api::pagination::PageQuery>,
) -> Result<AppResponseData<api::pagination::Page<BookmarkDto>>, ApiError> {
let bookmarks = service.list_bookmarks(uid, room_id).await?;
Ok(AppResponseData::ok(api::pagination::paginate(
bookmarks, &page,
)?))
}
#[utoipa::path(
post,
path = "/chat/rooms/{room_id}/bookmarks",
tag = "chat",
description = "在可读取的房间中创建链接书签或表情书签。",
params(("room_id" = Uuid, Path, description = "room id")),
request_body = CreateBookmarkCmd,
responses(
(status = 200, description = "bookmark created", body = AppResponseData<BookmarkDto>),
(status = 400, description = "bad request", body = AppResponseData<NoData>),
(status = 409, description = "resource already exists", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn create_bookmark(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path(room_id): Path<Uuid>,
Json(cmd): Json<CreateBookmarkCmd>,
) -> Result<AppResponseData<BookmarkDto>, ApiError> {
let dto = service.create_bookmark(uid, room_id, cmd).await?;
Ok(AppResponseData::ok(dto))
}
#[utoipa::path(
patch,
path = "/chat/rooms/{room_id}/bookmarks/{bookmark_id}",
tag = "chat",
description = "更新书签的标题、链接、表情或排序位置;仅作者或空间管理员可改。",
params(
("room_id" = Uuid, Path, description = "room id"),
("bookmark_id" = Uuid, Path, description = "bookmark id")
),
request_body = UpdateBookmarkCmd,
responses(
(status = 200, description = "bookmark updated", body = AppResponseData<BookmarkDto>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 400, description = "request failed validation", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn update_bookmark(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path((room_id, bookmark_id)): Path<(Uuid, Uuid)>,
Json(cmd): Json<UpdateBookmarkCmd>,
) -> Result<AppResponseData<BookmarkDto>, ApiError> {
let dto = service
.update_bookmark(uid, room_id, bookmark_id, cmd)
.await?;
Ok(AppResponseData::ok(dto))
}
#[utoipa::path(
delete,
path = "/chat/rooms/{room_id}/bookmarks/{bookmark_id}",
tag = "chat",
description = "删除指定房间书签;仅作者或空间管理员可操作。",
params(
("room_id" = Uuid, Path, description = "room id"),
("bookmark_id" = Uuid, Path, description = "bookmark id")
),
responses(
(status = 200, description = "bookmark deleted", body = AppResponseData<NoData>),
(status = 404, description = "not found", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn delete_bookmark(
State(service): State<ChatService>,
AuthedUser(uid): AuthedUser,
Path((room_id, bookmark_id)): Path<(Uuid, Uuid)>,
) -> Result<AppResponseData<NoData>, ApiError> {
service.delete_bookmark(uid, room_id, bookmark_id).await?;
Ok(AppResponseData::empty())
}