rebuild: topic api

This commit is contained in:
KUN1007 2023-10-06 17:07:46 +08:00
parent d3e45850ce
commit d1d6a77cb9
11 changed files with 316 additions and 266 deletions

View file

@ -7,9 +7,7 @@ export * from './edit/types/edit'
export * from './home/types/home' export * from './home/types/home'
export * from './kungalgamer/types/kungalgamer' export * from './kungalgamer/types/kungalgamer'
export * from './login/types/login' export * from './login/types/login'
export * from './topic/types/action' export * from './topic/types'
export * from './topic/types/topic'
export * from './topic/types/aside'
export * from './update-log/types/updateLog' export * from './update-log/types/updateLog'
// 暴露出所有 api // 暴露出所有 api

38
src/api/topic/aside.ts Normal file
View file

@ -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<Aside.TopicAsideResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/related?${queryParams}`
const response = await fetchGet<Aside.TopicAsideResponseData>(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<Aside.TopicAsideResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/popular?${queryParams}`
const response = await fetchGet<Aside.TopicAsideResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch master other topic')
}
}

39
src/api/topic/comment.ts Normal file
View file

@ -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<Comment.TopicCommentResponseData> {
try {
const url = `/topics/${tid}/comment?rid=${rid}`
const response = await fetchGet<Comment.TopicCommentResponseData>(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<Comment.TopicCreateCommentResponseData> {
try {
const url = `/topics/${request.tid}/comment`
const response = await fetchPost<Comment.TopicCreateCommentResponseData>(
url,
request
)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch comments')
}
}

View file

@ -1,164 +1,8 @@
import { fetchGet, fetchPost, fetchPut } from '@/utils/request' // aside 的 api
// 将对象转为请求参数的函数 export * from './aside'
import objectToQueryParams from '@/utils/objectToQueryParams' // topic 相关的 api
import * as Action from './types/action' export * from './topic'
import * as Aside from './types/aside' // reply 相关的 api
import * as Topic from './types/topic' export * from './reply'
// comment 相关的 api
// 左侧相同标签下的其它话题 export * from './comment'
export async function getRelatedTopicsByTagsApi(
request: Aside.TopicAsideOtherTagRequestData
): Promise<Aside.TopicAsideResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/related?${queryParams}`
const response = await fetchGet<Aside.TopicAsideResponseData>(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<Aside.TopicAsideResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/popular?${queryParams}`
const response = await fetchGet<Aside.TopicAsideResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch master other topic')
}
}
// 获取单个话题
export async function getTopicByTidApi(
tid: number
): Promise<Topic.TopicDetailResponseData> {
try {
const url = `/topics/${tid}`
const response = await fetchGet<Topic.TopicDetailResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch topic')
}
}
// 推话题
export async function updateTopicUpvoteApi(
request: Action.TopicUpvoteTopicRequestData
): Promise<Action.TopicUpvoteTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/upvote?${queryParams}`
const response = fetchPut<Action.TopicUpvoteTopicResponseData>(url)
return response
}
// 点赞话题
export async function updateTopicLikeApi(
request: Action.TopicLikeTopicRequestData
): Promise<Action.TopicLikeTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/like?${queryParams}`
const response = fetchPut<Action.TopicLikeTopicResponseData>(url)
return response
}
// 点踩话题
export async function updateTopicDislikeApi(
request: Action.TopicDislikeTopicRequestData
): Promise<Action.TopicDislikeTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/dislike?${queryParams}`
const response = fetchPut<Action.TopicDislikeTopicResponseData>(url)
return response
}
// 根据话题 tid 获取话题回复
export async function getRepliesByPidApi(
request: Topic.TopicReplyRequestData
): Promise<Topic.TopicReplyResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/replies?${queryParams}`
const response = await fetchGet<Topic.TopicReplyResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch replies')
}
}
// 根据 tid 创建一个回复
export async function postReplyByPidApi(
request: Topic.TopicCreateReplyRequestData
): Promise<Topic.TopicCreateReplyResponseData> {
try {
const url = `/topics/${request.tid}/reply`
const response = await fetchPost<Topic.TopicCreateReplyResponseData>(
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<Topic.TopicCommentResponseData> {
try {
const url = `/topics/${tid}/comment?rid=${rid}`
const response = await fetchGet<Topic.TopicCommentResponseData>(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<Topic.TopicCreateCommentResponseData> {
try {
const url = `/topics/${request.tid}/comment`
const response = await fetchPost<Topic.TopicCreateCommentResponseData>(
url,
request
)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch comments')
}
}

40
src/api/topic/reply.ts Normal file
View file

@ -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<Reply.TopicReplyResponseData> {
try {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/replies?${queryParams}`
const response = await fetchGet<Reply.TopicReplyResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch replies')
}
}
// 根据 tid 创建一个回复
export async function postReplyByPidApi(
request: Reply.TopicCreateReplyRequestData
): Promise<Reply.TopicCreateReplyResponseData> {
try {
const url = `/topics/${request.tid}/reply`
const response = await fetchPost<Reply.TopicCreateReplyResponseData>(
url,
request
)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to create reply')
}
}

56
src/api/topic/topic.ts Normal file
View file

@ -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<Topic.TopicDetailResponseData> {
try {
const url = `/topics/${tid}`
const response = await fetchGet<Topic.TopicDetailResponseData>(url)
return response
} catch (error) {
console.log(error)
throw new Error('Failed to fetch topic')
}
}
// 推话题
export async function updateTopicUpvoteApi(
request: Topic.TopicUpvoteTopicRequestData
): Promise<Topic.TopicUpvoteTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/upvote?${queryParams}`
const response = fetchPut<Topic.TopicUpvoteTopicResponseData>(url)
return response
}
// 点赞话题
export async function updateTopicLikeApi(
request: Topic.TopicLikeTopicRequestData
): Promise<Topic.TopicLikeTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/like?${queryParams}`
const response = fetchPut<Topic.TopicLikeTopicResponseData>(url)
return response
}
// 点踩话题
export async function updateTopicDislikeApi(
request: Topic.TopicDislikeTopicRequestData
): Promise<Topic.TopicDislikeTopicResponseData> {
const queryParams = objectToQueryParams(request, 'tid')
const url = `/topics/${request.tid}/dislike?${queryParams}`
const response = fetchPut<Topic.TopicDislikeTopicResponseData>(url)
return response
}

View file

@ -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<{}>

View file

@ -0,0 +1,27 @@
import { TopicUserInfo, TopicToUserInfo } from './topic'
// 评论的数据
export interface TopicComment {
rid: number
tid: number
c_user: Omit<TopicUserInfo, 'moemoepoint'>
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<TopicComment[]>
// 创建单个评论后返回的评论数据
export type TopicCreateCommentResponseData =
KUNGalgameResponseData<TopicComment>

View file

@ -0,0 +1,4 @@
export * from './aside'
export * from './topic'
export * from './reply'
export * from './comment'

View file

@ -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<TopicReply[]>
// 创建单个回复后返回的回复数据
export type TopicCreateReplyResponseData = KUNGalgameResponseData<TopicReply>
// 推回复响应数据的格式
export type TopicUpvoteReplyResponseData = KUNGalgameResponseData<{}>
// 点赞回复响应数据的格式
export type TopicLikeReplyResponseData = KUNGalgameResponseData<{}>
// 点踩回复响应数据的格式
export type TopicDislikeReplyResponseData = KUNGalgameResponseData<{}>

View file

@ -33,6 +33,26 @@ export interface TopicDetail {
upvote_time: number 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 { export interface TopicUpdateRequestData {
tid: number tid: number
@ -43,75 +63,14 @@ export interface TopicUpdateRequestData {
category: string[] 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<TopicUserInfo, 'moemoepoint'>
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<TopicDetail> export type TopicDetailResponseData = KUNGalgameResponseData<TopicDetail>
// 单个话题回复响应数据的格式,返回的是多条回复数据,是一个数组 // 推话题响应数据的格式
export type TopicReplyResponseData = KUNGalgameResponseData<TopicReply[]> export type TopicUpvoteTopicResponseData = KUNGalgameResponseData<{}>
// 创建单个回复后返回的回复数据 // 点赞话题响应数据的格式
export type TopicCreateReplyResponseData = KUNGalgameResponseData<TopicReply> export type TopicLikeTopicResponseData = KUNGalgameResponseData<{}>
// 单个回复底下所有的评论数据,是一个数组 // 点踩话题响应数据的格式
export type TopicCommentResponseData = KUNGalgameResponseData<TopicComment[]> export type TopicDislikeTopicResponseData = KUNGalgameResponseData<{}>
// 创建单个评论后返回的评论数据
export type TopicCreateCommentResponseData =
KUNGalgameResponseData<TopicComment>