diff --git a/src/api/index.ts b/src/api/index.ts index f262c241..a2f8c050 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -7,9 +7,7 @@ export * from './edit/types/edit' export * from './home/types/home' export * from './kungalgamer/types/kungalgamer' export * from './login/types/login' -export * from './topic/types/action' -export * from './topic/types/topic' -export * from './topic/types/aside' +export * from './topic/types' export * from './update-log/types/updateLog' // 暴露出所有 api diff --git a/src/api/topic/aside.ts b/src/api/topic/aside.ts new file mode 100644 index 00000000..ffc9d856 --- /dev/null +++ b/src/api/topic/aside.ts @@ -0,0 +1,38 @@ +import { fetchGet } from '@/utils/request' +// 将对象转为请求参数的函数 +import objectToQueryParams from '@/utils/objectToQueryParams' +import * as Aside from './types/aside' + +// 左侧相同标签下的其它话题 +export async function getRelatedTopicsByTagsApi( + request: Aside.TopicAsideOtherTagRequestData +): Promise { + try { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/related?${queryParams}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch aside other topic') + } +} + +// 楼主的其它话题 +export async function getPopularTopicsByUserUidApi( + request: Aside.TopicAsideMasterRequestData +): Promise { + try { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/popular?${queryParams}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch master other topic') + } +} diff --git a/src/api/topic/comment.ts b/src/api/topic/comment.ts new file mode 100644 index 00000000..a01cc2b5 --- /dev/null +++ b/src/api/topic/comment.ts @@ -0,0 +1,39 @@ +import { fetchGet, fetchPost } from '@/utils/request' + +import * as Comment from './types/comment' + +// 获取一个回复下面所有的评论 +export async function getCommentsByReplyRidApi( + tid: number, + rid: number +): Promise { + try { + const url = `/topics/${tid}/comment?rid=${rid}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch comments') + } +} + +// 根据 tid 和 rid 创建一个评论 +export async function postCommentByPidAndRidApi( + request: Comment.TopicCreateCommentRequestData +): Promise { + try { + const url = `/topics/${request.tid}/comment` + + const response = await fetchPost( + url, + request + ) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch comments') + } +} diff --git a/src/api/topic/index.ts b/src/api/topic/index.ts index d5456b13..818a6e5b 100644 --- a/src/api/topic/index.ts +++ b/src/api/topic/index.ts @@ -1,164 +1,8 @@ -import { fetchGet, fetchPost, fetchPut } from '@/utils/request' -// 将对象转为请求参数的函数 -import objectToQueryParams from '@/utils/objectToQueryParams' -import * as Action from './types/action' -import * as Aside from './types/aside' -import * as Topic from './types/topic' - -// 左侧相同标签下的其它话题 -export async function getRelatedTopicsByTagsApi( - request: Aside.TopicAsideOtherTagRequestData -): Promise { - try { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/related?${queryParams}` - - const response = await fetchGet(url) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch aside other topic') - } -} - -// 楼主的其它话题 -export async function getPopularTopicsByUserUidApi( - request: Aside.TopicAsideMasterRequestData -): Promise { - try { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/popular?${queryParams}` - - const response = await fetchGet(url) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch master other topic') - } -} - -// 获取单个话题 -export async function getTopicByTidApi( - tid: number -): Promise { - try { - const url = `/topics/${tid}` - - const response = await fetchGet(url) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch topic') - } -} - -// 推话题 -export async function updateTopicUpvoteApi( - request: Action.TopicUpvoteTopicRequestData -): Promise { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/upvote?${queryParams}` - - const response = fetchPut(url) - - return response -} - -// 点赞话题 -export async function updateTopicLikeApi( - request: Action.TopicLikeTopicRequestData -): Promise { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/like?${queryParams}` - - const response = fetchPut(url) - - return response -} - -// 点踩话题 -export async function updateTopicDislikeApi( - request: Action.TopicDislikeTopicRequestData -): Promise { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/dislike?${queryParams}` - - const response = fetchPut(url) - - return response -} - -// 根据话题 tid 获取话题回复 -export async function getRepliesByPidApi( - request: Topic.TopicReplyRequestData -): Promise { - try { - const queryParams = objectToQueryParams(request, 'tid') - const url = `/topics/${request.tid}/replies?${queryParams}` - - const response = await fetchGet(url) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch replies') - } -} - -// 根据 tid 创建一个回复 -export async function postReplyByPidApi( - request: Topic.TopicCreateReplyRequestData -): Promise { - try { - const url = `/topics/${request.tid}/reply` - - const response = await fetchPost( - url, - request - ) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to create reply') - } -} - -// 获取一个回复下面所有的评论 -export async function getCommentsByReplyRidApi( - tid: number, - rid: number -): Promise { - try { - const url = `/topics/${tid}/comment?rid=${rid}` - - const response = await fetchGet(url) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch comments') - } -} - -// 根据 tid 和 rid 创建一个评论 -export async function postCommentByPidAndRidApi( - request: Topic.TopicCreateCommentRequestData -): Promise { - try { - const url = `/topics/${request.tid}/comment` - - const response = await fetchPost( - url, - request - ) - - return response - } catch (error) { - console.log(error) - throw new Error('Failed to fetch comments') - } -} +// aside 的 api +export * from './aside' +// topic 相关的 api +export * from './topic' +// reply 相关的 api +export * from './reply' +// comment 相关的 api +export * from './comment' diff --git a/src/api/topic/reply.ts b/src/api/topic/reply.ts new file mode 100644 index 00000000..1a986822 --- /dev/null +++ b/src/api/topic/reply.ts @@ -0,0 +1,40 @@ +import { fetchGet, fetchPost } from '@/utils/request' +// 将对象转为请求参数的函数 +import objectToQueryParams from '@/utils/objectToQueryParams' +import * as Reply from './types/reply' + +// 根据话题 tid 获取话题回复 +export async function getRepliesByPidApi( + request: Reply.TopicReplyRequestData +): Promise { + try { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/replies?${queryParams}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch replies') + } +} + +// 根据 tid 创建一个回复 +export async function postReplyByPidApi( + request: Reply.TopicCreateReplyRequestData +): Promise { + try { + const url = `/topics/${request.tid}/reply` + + const response = await fetchPost( + url, + request + ) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to create reply') + } +} diff --git a/src/api/topic/topic.ts b/src/api/topic/topic.ts new file mode 100644 index 00000000..9ca1d38c --- /dev/null +++ b/src/api/topic/topic.ts @@ -0,0 +1,56 @@ +import { fetchGet, fetchPost, fetchPut } from '@/utils/request' +// 将对象转为请求参数的函数 +import objectToQueryParams from '@/utils/objectToQueryParams' +import * as Topic from './types/topic' + +// 获取单个话题 +export async function getTopicByTidApi( + tid: number +): Promise { + try { + const url = `/topics/${tid}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch topic') + } +} + +// 推话题 +export async function updateTopicUpvoteApi( + request: Topic.TopicUpvoteTopicRequestData +): Promise { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/upvote?${queryParams}` + + const response = fetchPut(url) + + return response +} + +// 点赞话题 +export async function updateTopicLikeApi( + request: Topic.TopicLikeTopicRequestData +): Promise { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/like?${queryParams}` + + const response = fetchPut(url) + + return response +} + +// 点踩话题 +export async function updateTopicDislikeApi( + request: Topic.TopicDislikeTopicRequestData +): Promise { + const queryParams = objectToQueryParams(request, 'tid') + const url = `/topics/${request.tid}/dislike?${queryParams}` + + const response = fetchPut(url) + + return response +} diff --git a/src/api/topic/types/action.ts b/src/api/topic/types/action.ts deleted file mode 100644 index 479c18ee..00000000 --- a/src/api/topic/types/action.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * 这是用户对话题的操作,推,点赞,点踩等 - */ - -// 推,推操作不可撤销 -export interface TopicUpvoteTopicRequestData { - tid: number - to_uid: number -} - -// 点赞 -export interface TopicLikeTopicRequestData { - tid: number - to_uid: number - isPush: boolean -} - -// 点踩 -export interface TopicDislikeTopicRequestData { - tid: number - to_uid: number - isPush: boolean -} - -// 推话题响应数据的格式 -export type TopicUpvoteTopicResponseData = KUNGalgameResponseData<{}> - -// 点赞话题响应数据的格式 -export type TopicLikeTopicResponseData = KUNGalgameResponseData<{}> - -// 点踩话题响应数据的格式 -export type TopicDislikeTopicResponseData = KUNGalgameResponseData<{}> diff --git a/src/api/topic/types/comment.ts b/src/api/topic/types/comment.ts new file mode 100644 index 00000000..b56bf253 --- /dev/null +++ b/src/api/topic/types/comment.ts @@ -0,0 +1,27 @@ +import { TopicUserInfo, TopicToUserInfo } from './topic' + +// 评论的数据 +export interface TopicComment { + rid: number + tid: number + c_user: Omit + to_user: TopicToUserInfo + content: string + likes: number[] + dislikes: number[] +} + +// 发表评论的请求数据 +export interface TopicCreateCommentRequestData { + tid: number + rid: number + to_uid: number + content: string +} + +// 单个回复底下所有的评论数据,是一个数组 +export type TopicCommentResponseData = KUNGalgameResponseData + +// 创建单个评论后返回的评论数据 +export type TopicCreateCommentResponseData = + KUNGalgameResponseData diff --git a/src/api/topic/types/index.ts b/src/api/topic/types/index.ts new file mode 100644 index 00000000..53b06292 --- /dev/null +++ b/src/api/topic/types/index.ts @@ -0,0 +1,4 @@ +export * from './aside' +export * from './topic' +export * from './reply' +export * from './comment' diff --git a/src/api/topic/types/reply.ts b/src/api/topic/types/reply.ts new file mode 100644 index 00000000..d4c91d3f --- /dev/null +++ b/src/api/topic/types/reply.ts @@ -0,0 +1,77 @@ +import { TopicUserInfo, TopicToUserInfo } from './topic' + +// 回复的请求数据 +export interface TopicReplyRequestData { + tid: number + page?: number + limit?: number + sortField: string + sortOrder: string +} + +// 回复的数据 +export interface TopicReply { + rid: number + tid: number + // reply 所在的楼层 + floor: number + // 被回复的 reply 所在的楼层 + to_floor: number + r_user: TopicUserInfo + to_user: TopicToUserInfo + edited: number + content: string + upvote: number[] + likes: number[] + dislikes: number[] + tags: string[] + time: number + cid: number[] +} + +// 发表回复的请求数据 +export interface TopicCreateReplyRequestData { + tid: number + to_uid: number + to_floor: number + tags: string[] + content: string +} + +// 推回复,推操作不可撤销 +export interface TopicUpvoteReplyRequestData { + tid: number + to_uid: number + rid: number +} + +// 点赞回复 +export interface TopicLikeReplyRequestData { + tid: number + to_uid: number + rid: number + isPush: boolean +} + +// 点踩回复 +export interface TopicDislikeReplyRequestData { + tid: number + to_uid: number + rid: number + isPush: boolean +} + +// 单个话题回复响应数据的格式,返回的是多条回复数据,是一个数组 +export type TopicReplyResponseData = KUNGalgameResponseData + +// 创建单个回复后返回的回复数据 +export type TopicCreateReplyResponseData = KUNGalgameResponseData + +// 推回复响应数据的格式 +export type TopicUpvoteReplyResponseData = KUNGalgameResponseData<{}> + +// 点赞回复响应数据的格式 +export type TopicLikeReplyResponseData = KUNGalgameResponseData<{}> + +// 点踩回复响应数据的格式 +export type TopicDislikeReplyResponseData = KUNGalgameResponseData<{}> diff --git a/src/api/topic/types/topic.ts b/src/api/topic/types/topic.ts index 6059acd3..feb2d625 100644 --- a/src/api/topic/types/topic.ts +++ b/src/api/topic/types/topic.ts @@ -33,6 +33,26 @@ export interface TopicDetail { upvote_time: number } +// 推话题,推操作不可撤销 +export interface TopicUpvoteTopicRequestData { + tid: number + to_uid: number +} + +// 点赞话题 +export interface TopicLikeTopicRequestData { + tid: number + to_uid: number + isPush: boolean +} + +// 点踩话题 +export interface TopicDislikeTopicRequestData { + tid: number + to_uid: number + isPush: boolean +} + // 更新话题的请求数据 export interface TopicUpdateRequestData { tid: number @@ -43,75 +63,14 @@ export interface TopicUpdateRequestData { category: string[] } -// 回复的请求数据 -export interface TopicReplyRequestData { - tid: number - page?: number - limit?: number - sortField: string - sortOrder: string -} - -// 回复的数据 -export interface TopicReply { - rid: number - tid: number - // reply 所在的楼层 - floor: number - // 被回复的 reply 所在的楼层 - to_floor: number - r_user: TopicUserInfo - to_user: TopicToUserInfo - edited: number - content: string - upvote: number[] - likes: number[] - dislikes: number[] - tags: string[] - time: number - cid: number[] -} - -// 发表回复的请求数据 -export interface TopicCreateReplyRequestData { - tid: number - to_uid: number - to_floor: number - tags: string[] - content: string -} - -// 评论的数据 -export interface TopicComment { - rid: number - tid: number - c_user: Omit - to_user: TopicToUserInfo - content: string - likes: number[] - dislikes: number[] -} - -// 发表评论的请求数据 -export interface TopicCreateCommentRequestData { - tid: number - rid: number - to_uid: number - content: string -} - // 获取单个话题响应数据的格式 export type TopicDetailResponseData = KUNGalgameResponseData -// 单个话题回复响应数据的格式,返回的是多条回复数据,是一个数组 -export type TopicReplyResponseData = KUNGalgameResponseData +// 推话题响应数据的格式 +export type TopicUpvoteTopicResponseData = KUNGalgameResponseData<{}> -// 创建单个回复后返回的回复数据 -export type TopicCreateReplyResponseData = KUNGalgameResponseData +// 点赞话题响应数据的格式 +export type TopicLikeTopicResponseData = KUNGalgameResponseData<{}> -// 单个回复底下所有的评论数据,是一个数组 -export type TopicCommentResponseData = KUNGalgameResponseData - -// 创建单个评论后返回的评论数据 -export type TopicCreateCommentResponseData = - KUNGalgameResponseData +// 点踩话题响应数据的格式 +export type TopicDislikeTopicResponseData = KUNGalgameResponseData<{}>