mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-29 05:02:52 +08:00
[offers][chore] improve crud endpoints for comments
This commit is contained in:
@ -34,6 +34,59 @@ function Test() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const deleteCommentMutation = trpc.useMutation(['offers.comments.delete'], {
|
||||||
|
onError(err: any) {
|
||||||
|
alert(err);
|
||||||
|
},
|
||||||
|
onSuccess(data) {
|
||||||
|
setCreatedData(JSON.stringify(data));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleDeleteComment = () => {
|
||||||
|
deleteCommentMutation.mutate({
|
||||||
|
id: 'cl97fprun001j7iyg6ev9x983',
|
||||||
|
profileId: 'cl96stky5002ew32gx2kale2x',
|
||||||
|
token: 'afca11e436d21bde24543718fa957c6c625335439dc504f24ee35eae7b5ef1',
|
||||||
|
userId: 'cl97dl51k001e7iygd5v5gt58'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateCommentMutation = trpc.useMutation(['offers.comments.update'], {
|
||||||
|
onError(err: any) {
|
||||||
|
alert(err);
|
||||||
|
},
|
||||||
|
onSuccess(data) {
|
||||||
|
setCreatedData(JSON.stringify(data));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleUpdateComment = () => {
|
||||||
|
updateCommentMutation.mutate({
|
||||||
|
id: 'cl97fxb0y001l7iyg14sdobt2',
|
||||||
|
message: 'hello hello',
|
||||||
|
profileId: 'cl96stky5002ew32gx2kale2x',
|
||||||
|
token: 'afca11e436d21bde24543718fa957c6c625335439dc504f24ee35eae7b5ef1ba'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const createCommentMutation = trpc.useMutation(['offers.comments.create'], {
|
||||||
|
onError(err: any) {
|
||||||
|
alert(err);
|
||||||
|
},
|
||||||
|
onSuccess(data) {
|
||||||
|
setCreatedData(JSON.stringify(data));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleCreate = () => {
|
||||||
|
createCommentMutation.mutate({
|
||||||
|
message: 'hello',
|
||||||
|
profileId: 'cl96stky5002ew32gx2kale2x',
|
||||||
|
// UserId: 'cl97dl51k001e7iygd5v5gt58'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const handleLink = () => {
|
const handleLink = () => {
|
||||||
addToUserProfileMutation.mutate({
|
addToUserProfileMutation.mutate({
|
||||||
profileId: 'cl96stky5002ew32gx2kale2x',
|
profileId: 'cl96stky5002ew32gx2kale2x',
|
||||||
@ -552,6 +605,15 @@ function Test() {
|
|||||||
<button type="button" onClick={handleLink}>
|
<button type="button" onClick={handleLink}>
|
||||||
LINKKKK!
|
LINKKKK!
|
||||||
</button>
|
</button>
|
||||||
|
<button type="button" onClick={handleCreate}>
|
||||||
|
CREATE COMMENT!
|
||||||
|
</button>
|
||||||
|
<button type="button" onClick={handleDeleteComment}>
|
||||||
|
DELETE COMMENT!
|
||||||
|
</button>
|
||||||
|
<button type="button" onClick={handleUpdateComment}>
|
||||||
|
UPDATE COMMENT!
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
className="text-danger-600"
|
className="text-danger-600"
|
||||||
type="button"
|
type="button"
|
||||||
|
@ -36,32 +36,70 @@ export const offersCommentsRouter = createProtectedRouter()
|
|||||||
message: z.string(),
|
message: z.string(),
|
||||||
profileId: z.string(),
|
profileId: z.string(),
|
||||||
replyingToId: z.string().optional(),
|
replyingToId: z.string().optional(),
|
||||||
userId: z.string()
|
userId: z.string().optional()
|
||||||
}),
|
}),
|
||||||
async resolve({ ctx, input }) {
|
async resolve({ ctx, input }) {
|
||||||
await ctx.prisma.offersReply.create({
|
const createdReply = await ctx.prisma.offersReply.create({
|
||||||
data: {
|
data: {
|
||||||
message: input.message,
|
message: input.message,
|
||||||
profile: {
|
profile: {
|
||||||
connect: {
|
connect: {
|
||||||
id: input.profileId
|
id: input.profileId
|
||||||
}
|
}
|
||||||
},
|
|
||||||
replyingTo: {
|
|
||||||
connect: {
|
|
||||||
id: input.replyingToId
|
|
||||||
}
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
connect: {
|
|
||||||
id: input.userId
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (input.replyingToId) {
|
||||||
|
await ctx.prisma.offersReply.update({
|
||||||
|
data: {
|
||||||
|
replyingTo: {
|
||||||
|
connect: {
|
||||||
|
id: input.replyingToId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: createdReply.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.userId) {
|
||||||
|
await ctx.prisma.offersReply.update({
|
||||||
|
data: {
|
||||||
|
user: {
|
||||||
|
connect: {
|
||||||
|
id: input.userId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: createdReply.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
// Get replies
|
// Get replies
|
||||||
return
|
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("update", {
|
.mutation("update", {
|
||||||
@ -90,7 +128,7 @@ export const offersCommentsRouter = 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) {
|
||||||
return await ctx.prisma.offersReply.update({
|
await ctx.prisma.offersReply.update({
|
||||||
data: {
|
data: {
|
||||||
message: input.message
|
message: input.message
|
||||||
},
|
},
|
||||||
@ -98,6 +136,27 @@ export const offersCommentsRouter = createProtectedRouter()
|
|||||||
id: input.id
|
id: input.id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new trpc.TRPCError({
|
throw new trpc.TRPCError({
|
||||||
@ -136,6 +195,26 @@ export const offersCommentsRouter = createProtectedRouter()
|
|||||||
id: input.id
|
id: input.id
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new trpc.TRPCError({
|
throw new trpc.TRPCError({
|
||||||
|
Reference in New Issue
Block a user