mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-29 13:13:54 +08:00
[offers][feat] add get comments endpoint
This commit is contained in:
@ -158,6 +158,12 @@ function Test() {
|
||||
}
|
||||
});
|
||||
|
||||
const replies = trpc.useQuery(['offers.comments.getComments', {profileId: 'cl96stky5002ew32gx2kale2x'}], {
|
||||
onError(err) {
|
||||
setError(err.shape?.message || "")
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = trpc.useMutation(['offers.profile.delete']);
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
@ -536,6 +542,7 @@ function Test() {
|
||||
return (
|
||||
<>
|
||||
<div>{createdData}</div>
|
||||
<div>{JSON.stringify(replies.data)}</div>
|
||||
<button type="button" onClick={handleClick}>
|
||||
Click Me!
|
||||
</button>
|
||||
|
@ -3,6 +3,7 @@ import superjson from 'superjson';
|
||||
import { companiesRouter } from './companies-router';
|
||||
import { createRouter } from './context';
|
||||
import { offersRouter } from './offers/offers';
|
||||
import { offersCommentsRouter } from './offers/offers-comments-router';
|
||||
import { offersProfileRouter } from './offers/offers-profile-router';
|
||||
import { protectedExampleRouter } from './protected-example-router';
|
||||
import { questionsAnswerCommentRouter } from './questions-answer-comment-router';
|
||||
@ -36,7 +37,8 @@ export const appRouter = createRouter()
|
||||
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
||||
.merge('questions.questions.', questionsQuestionRouter)
|
||||
.merge('offers.', offersRouter)
|
||||
.merge('offers.profile.', offersProfileRouter);
|
||||
.merge('offers.profile.', offersProfileRouter)
|
||||
.merge('offers.comments.', offersCommentsRouter);
|
||||
|
||||
// Export type definition of API
|
||||
export type AppRouter = typeof appRouter;
|
||||
|
@ -3,7 +3,67 @@ import * as trpc from '@trpc/server';
|
||||
|
||||
import { createProtectedRouter } from '../context';
|
||||
|
||||
export const offersProfileRouter = createProtectedRouter()
|
||||
export const offersCommentsRouter = createProtectedRouter()
|
||||
.query('getComments', {
|
||||
input: z.object({
|
||||
profileId: z.string()
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const result = await ctx.prisma.offersProfile.findFirst({
|
||||
include: {
|
||||
discussion: {
|
||||
include: {
|
||||
replies: true,
|
||||
replyingTo: true,
|
||||
user: true
|
||||
}
|
||||
}
|
||||
},
|
||||
where: {
|
||||
id: input.profileId
|
||||
}
|
||||
})
|
||||
|
||||
if (result) {
|
||||
return result.discussion.filter((x) => x.replyingToId === null)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
})
|
||||
.mutation("create", {
|
||||
input: z.object({
|
||||
message: z.string(),
|
||||
profileId: z.string(),
|
||||
replyingToId: z.string().optional(),
|
||||
userId: z.string()
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
await ctx.prisma.offersReply.create({
|
||||
data: {
|
||||
message: input.message,
|
||||
profile: {
|
||||
connect: {
|
||||
id: input.profileId
|
||||
}
|
||||
},
|
||||
replyingTo: {
|
||||
connect: {
|
||||
id: input.replyingToId
|
||||
}
|
||||
},
|
||||
user: {
|
||||
connect: {
|
||||
id: input.userId
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Get replies
|
||||
return
|
||||
}
|
||||
})
|
||||
.mutation("update", {
|
||||
input: z.object({
|
||||
id: z.string(),
|
||||
@ -30,7 +90,7 @@ export const offersProfileRouter = createProtectedRouter()
|
||||
// To validate user editing, OP or correct user
|
||||
// TODO: improve validation process
|
||||
if (profileEditToken === input.token || messageToUpdate?.userId === input.userId) {
|
||||
await ctx.prisma.offersReply.update({
|
||||
return await ctx.prisma.offersReply.update({
|
||||
data: {
|
||||
message: input.message
|
||||
},
|
||||
|
@ -985,6 +985,7 @@ export const offersProfileRouter = createRouter()
|
||||
include: {
|
||||
replies: true,
|
||||
replyingTo: true,
|
||||
user: true
|
||||
},
|
||||
},
|
||||
offers: {
|
||||
@ -1042,10 +1043,8 @@ export const offersProfileRouter = createRouter()
|
||||
|
||||
const profileEditToken = profile?.editToken;
|
||||
|
||||
// To validate user editing, OP or correct user
|
||||
// TODO: improve validation process
|
||||
if (profileEditToken === input.token) {
|
||||
return await ctx.prisma.offersProfile.update({
|
||||
const updated = await ctx.prisma.offersProfile.update({
|
||||
data: {
|
||||
user: {
|
||||
connect: {
|
||||
@ -1057,6 +1056,12 @@ export const offersProfileRouter = createRouter()
|
||||
id: input.profileId
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
id: updated.id,
|
||||
profileName: updated.profileName,
|
||||
userId: updated.userId
|
||||
}
|
||||
}
|
||||
|
||||
throw new trpc.TRPCError({
|
||||
|
Reference in New Issue
Block a user