mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-30 05:34:33 +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 deleteMutation = trpc.useMutation(['offers.profile.delete']);
|
||||||
|
|
||||||
const handleDelete = (id: string) => {
|
const handleDelete = (id: string) => {
|
||||||
@ -536,6 +542,7 @@ function Test() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>{createdData}</div>
|
<div>{createdData}</div>
|
||||||
|
<div>{JSON.stringify(replies.data)}</div>
|
||||||
<button type="button" onClick={handleClick}>
|
<button type="button" onClick={handleClick}>
|
||||||
Click Me!
|
Click Me!
|
||||||
</button>
|
</button>
|
||||||
|
@ -3,6 +3,7 @@ import superjson from 'superjson';
|
|||||||
import { companiesRouter } from './companies-router';
|
import { companiesRouter } from './companies-router';
|
||||||
import { createRouter } from './context';
|
import { createRouter } from './context';
|
||||||
import { offersRouter } from './offers/offers';
|
import { offersRouter } from './offers/offers';
|
||||||
|
import { offersCommentsRouter } from './offers/offers-comments-router';
|
||||||
import { offersProfileRouter } from './offers/offers-profile-router';
|
import { offersProfileRouter } from './offers/offers-profile-router';
|
||||||
import { protectedExampleRouter } from './protected-example-router';
|
import { protectedExampleRouter } from './protected-example-router';
|
||||||
import { questionsAnswerCommentRouter } from './questions-answer-comment-router';
|
import { questionsAnswerCommentRouter } from './questions-answer-comment-router';
|
||||||
@ -36,7 +37,8 @@ export const appRouter = createRouter()
|
|||||||
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
||||||
.merge('questions.questions.', questionsQuestionRouter)
|
.merge('questions.questions.', questionsQuestionRouter)
|
||||||
.merge('offers.', offersRouter)
|
.merge('offers.', offersRouter)
|
||||||
.merge('offers.profile.', offersProfileRouter);
|
.merge('offers.profile.', offersProfileRouter)
|
||||||
|
.merge('offers.comments.', offersCommentsRouter);
|
||||||
|
|
||||||
// Export type definition of API
|
// Export type definition of API
|
||||||
export type AppRouter = typeof appRouter;
|
export type AppRouter = typeof appRouter;
|
||||||
|
@ -3,7 +3,67 @@ import * as trpc from '@trpc/server';
|
|||||||
|
|
||||||
import { createProtectedRouter } from '../context';
|
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", {
|
.mutation("update", {
|
||||||
input: z.object({
|
input: z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@ -30,7 +90,7 @@ export const offersProfileRouter = createProtectedRouter()
|
|||||||
// To validate user editing, OP or correct user
|
// To validate user editing, OP or correct user
|
||||||
// TODO: improve validation process
|
// TODO: improve validation process
|
||||||
if (profileEditToken === input.token || messageToUpdate?.userId === input.userId) {
|
if (profileEditToken === input.token || messageToUpdate?.userId === input.userId) {
|
||||||
await ctx.prisma.offersReply.update({
|
return await ctx.prisma.offersReply.update({
|
||||||
data: {
|
data: {
|
||||||
message: input.message
|
message: input.message
|
||||||
},
|
},
|
||||||
|
@ -985,6 +985,7 @@ export const offersProfileRouter = createRouter()
|
|||||||
include: {
|
include: {
|
||||||
replies: true,
|
replies: true,
|
||||||
replyingTo: true,
|
replyingTo: true,
|
||||||
|
user: true
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
offers: {
|
offers: {
|
||||||
@ -1042,10 +1043,8 @@ export const offersProfileRouter = createRouter()
|
|||||||
|
|
||||||
const profileEditToken = profile?.editToken;
|
const profileEditToken = profile?.editToken;
|
||||||
|
|
||||||
// To validate user editing, OP or correct user
|
|
||||||
// TODO: improve validation process
|
|
||||||
if (profileEditToken === input.token) {
|
if (profileEditToken === input.token) {
|
||||||
return await ctx.prisma.offersProfile.update({
|
const updated = await ctx.prisma.offersProfile.update({
|
||||||
data: {
|
data: {
|
||||||
user: {
|
user: {
|
||||||
connect: {
|
connect: {
|
||||||
@ -1057,6 +1056,12 @@ export const offersProfileRouter = createRouter()
|
|||||||
id: input.profileId
|
id: input.profileId
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: updated.id,
|
||||||
|
profileName: updated.profileName,
|
||||||
|
userId: updated.userId
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new trpc.TRPCError({
|
throw new trpc.TRPCError({
|
||||||
|
Reference in New Issue
Block a user