|
| 1 | +import { Context } from '../context' |
| 2 | +import { getAuthId } from '../utils/auth' |
| 3 | + |
1 | 4 | export default {
|
2 | 5 | 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 | + }, |
4 | 99 | },
|
5 | 100 | }
|
0 commit comments