Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5342007

Browse files
committed
Comment crud completed and added two way relations between comments, users and videos
1 parent f018413 commit 5342007

File tree

7 files changed

+135
-7
lines changed

7 files changed

+135
-7
lines changed

‎prisma/schema.prisma

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ model comments {
1111
createdat DateTime @default(now())
1212
id String @default(dbgenerated()) @id
1313
text String
14-
userid String?
15-
videoid String?
16-
users users? @relation(fields: [userid], references: [id])
17-
videos videos? @relation(fields: [videoid], references: [id])
14+
user String?
15+
video String?
16+
users users? @relation(fields: [user], references: [id])
17+
videos videos? @relation(fields: [video], references: [id])
1818
replies replies[]
1919
}
2020

‎src/resolvers/comments.ts

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,100 @@
1+
import { Context } from '../context'
2+
import { getAuthId } from '../utils/auth'
3+
14
export default {
25
Query: {
3-
comments: () => [],
6+
comments: async (_parent: any, _args: any, ctx: Context) => {
7+
const comments = await ctx.prisma.comments.findMany()
8+
return comments
9+
},
10+
},
11+
12+
Mutation: {
13+
createComment: async (
14+
_parent: any,
15+
{ data }: any,
16+
{ prisma, req }: Context
17+
) => {
18+
const userId = getAuthId(req)
19+
20+
const createdComment = await prisma.comments.create({
21+
data: {
22+
text: data.text,
23+
users: {
24+
connect: {
25+
id: userId,
26+
},
27+
},
28+
videos: {
29+
connect: {
30+
id: data.video,
31+
},
32+
},
33+
},
34+
})
35+
36+
return createdComment
37+
},
38+
39+
updateComment: async (_parent: any, args: any, ctx: Context) => {
40+
const userId = getAuthId(ctx.req)
41+
const comment = await ctx.prisma.comments.findOne({
42+
where: {
43+
id: args.id,
44+
},
45+
})
46+
47+
if (!comment || comment.user !== userId)
48+
throw new Error('Comment not found')
49+
50+
const updatedComment = await ctx.prisma.comments.update({
51+
where: {
52+
id: args.id,
53+
},
54+
data: args.data,
55+
})
56+
57+
return updatedComment
58+
},
59+
60+
deleteComment: async (parent: any, args: any, ctx: Context) => {
61+
const userId = getAuthId(ctx.req)
62+
63+
const comment = await ctx.prisma.comments.findOne({
64+
where: {
65+
id: args.id,
66+
},
67+
})
68+
69+
if (!comment || comment.user != userId) {
70+
throw new Error('Comment not found')
71+
}
72+
73+
const deletedComment = await ctx.prisma.comments.delete({
74+
where: {
75+
id: args.id,
76+
},
77+
})
78+
79+
return deletedComment
80+
},
81+
},
82+
83+
Comment: {
84+
user: async (parent: any, _args: any, ctx: Context) => {
85+
return await ctx.prisma.users.findOne({
86+
where: {
87+
id: parent.user,
88+
},
89+
})
90+
},
91+
92+
video: async (parent: any, _args: any, ctx: Context) => {
93+
return await ctx.prisma.videos.findOne({
94+
where: {
95+
id: parent.video,
96+
},
97+
})
98+
},
499
},
5100
}

‎src/resolvers/users.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,13 @@ export default {
159159
},
160160
})
161161
},
162+
163+
comments: async (parent: any, args: any, ctx: Context) => {
164+
return await ctx.prisma.comments.findMany({
165+
where: {
166+
user: parent.id,
167+
},
168+
})
169+
},
162170
},
163171
}

‎src/resolvers/videos.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,13 @@ export default {
9999
},
100100
})
101101
},
102+
103+
comments: async (parent: any, args: any, ctx: Context) => {
104+
return await ctx.prisma.comments.findMany({
105+
where: {
106+
video: parent.id,
107+
},
108+
})
109+
},
102110
},
103111
}

‎src/types/commentType.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ export default `
33
comments: [Comment!]!
44
}
55
6+
type Mutation {
7+
createComment(data: CreateCommentInput!): Comment!
8+
updateComment(id: ID!, data: UpdateCommentInput!): Comment!
9+
deleteComment(id: ID!): Comment!
10+
}
11+
12+
input CreateCommentInput {
13+
text: String!
14+
video: String!
15+
}
16+
17+
input UpdateCommentInput {
18+
text: String!
19+
}
20+
621
type Comment {
722
id: ID!
823
text: String!
9-
userid: String!
10-
videoid: String!
24+
user: User!
25+
video: Video!
1126
createdat: String!
1227
}
1328
`

‎src/types/userType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ export default `
5757
fullname: String!
5858
createdat: String!
5959
videos: [Video!]!
60+
comments: [Comment!]!
6061
}
6162
`

‎src/types/videoTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ export default `
3232
views: Int!
3333
creator: User!
3434
createdat: String!
35+
comments: [Comment!]!
3536
}
3637
`

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /