Files
core/packages/api-client/controllers/comment.ts
Innei 26b2b4f134 feat: reader for comment and like action (#2122)
* init

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* feat: reader like activity

Signed-off-by: Innei <i@innei.in>

* feat: add comment

Signed-off-by: Innei <i@innei.in>

* feat: comment reader

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
2024-09-14 20:26:18 +08:00

73 lines
1.8 KiB
TypeScript

import type { IRequestAdapter } from '~/interfaces/adapter'
import type { IController } from '~/interfaces/controller'
import type { PaginationParams } from '~/interfaces/params'
import type { IRequestHandler } from '~/interfaces/request'
import type { ReaderModel } from '~/models'
import type { PaginateResult } from '~/models/base'
import type { CommentModel } from '~/models/comment'
import type { HTTPClient } from '../core'
import type { CommentDto } from '../dtos/comment'
import { autoBind } from '~/utils/auto-bind'
declare module '../core/client' {
interface HTTPClient<
T extends IRequestAdapter = IRequestAdapter,
ResponseWrapper = unknown,
> {
comment: CommentController<ResponseWrapper>
}
}
export class CommentController<ResponseWrapper> implements IController {
base = 'comments'
name = 'comment'
constructor(private readonly client: HTTPClient) {
autoBind(this)
}
get proxy(): IRequestHandler<ResponseWrapper> {
return this.client.proxy(this.base)
}
/**
* 根据 comment id 获取评论,包括子评论
*/
getById(id: string) {
return this.proxy(id).get<CommentModel & { ref: string }>()
}
/**
* 获取文章的评论列表
* @param refId 文章 Id
*/
getByRefId(refId: string, pagination: PaginationParams = {}) {
const { page, size } = pagination
return this.proxy.ref(refId).get<
PaginateResult<CommentModel & { ref: string }> & {
readers: Record<string, ReaderModel>
}
>({
params: { page: page || 1, size: size || 10 },
})
}
/**
* 评论
*/
comment(refId: string, data: CommentDto) {
return this.proxy(refId).post<CommentModel>({
data,
})
}
/**
* 回复评论
*/
reply(commentId: string, data: CommentDto) {
return this.proxy.reply(commentId).post<CommentModel>({
data,
})
}
}