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 a8d16b6

Browse files
committed
reactions like and dislike completed
1 parent 52ef864 commit a8d16b6

File tree

7 files changed

+148
-41
lines changed

7 files changed

+148
-41
lines changed

‎prisma/schema.prisma

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ model comments {
1919
}
2020

2121
model reactions {
22-
id String @default(dbgenerated()) @id
23-
reaction reaction
24-
userid String?
25-
videoid String?
26-
users users? @relation(fields: [userid], references: [id])
27-
videos videos? @relation(fields: [videoid], references: [id])
22+
id String @default(dbgenerated()) @id
23+
reaction reaction?
24+
user String?
25+
video String?
26+
users users? @relation(fields: [user], references: [id])
27+
videos videos? @relation(fields: [video], references: [id])
2828
}
2929

3030
model replies {

‎src/resolvers/reaction.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
1+
import { Context } from "../context";
2+
import { getAuthId } from "../utils/auth";
3+
14
export default {
25
Query: {
3-
reactions: () => [],
6+
reactions: async (_: any, __: any, ctx: Context) =>
7+
await ctx.prisma.reactions.findMany(),
48
},
5-
}
9+
10+
Mutation: {
11+
manageReaction: async (parent: any, args: any, ctx: Context) => {
12+
const userId = getAuthId(ctx.req);
13+
let managedReaction;
14+
15+
const reactions = await ctx.prisma.reactions.findMany({
16+
where: {
17+
AND: [
18+
{
19+
user: userId,
20+
},
21+
{
22+
video: args.video,
23+
},
24+
],
25+
},
26+
});
27+
28+
const reactionInput = args.liked ? "like" : "dislike";
29+
30+
if (reactions.length === 0) {
31+
managedReaction = await ctx.prisma.reactions.create({
32+
data: {
33+
reaction: reactionInput,
34+
videos: {
35+
connect: {
36+
id: args.video,
37+
},
38+
},
39+
users: {
40+
connect: {
41+
id: userId,
42+
},
43+
},
44+
},
45+
});
46+
} else {
47+
if (reactionInput == reactions[0].reaction) {
48+
managedReaction = await ctx.prisma.reactions.delete({
49+
where: {
50+
id: reactions[0].id,
51+
},
52+
});
53+
} else {
54+
managedReaction = await ctx.prisma.reactions.update({
55+
where: {
56+
id: reactions[0].id,
57+
},
58+
data: {
59+
reaction: reactionInput,
60+
},
61+
});
62+
}
63+
}
64+
65+
return managedReaction;
66+
},
67+
},
68+
69+
Reaction: {
70+
user: async (parent: any, args: any, ctx: Context) => {
71+
return await ctx.prisma.users.findOne({
72+
where: {
73+
id: parent.user,
74+
},
75+
});
76+
},
77+
78+
video: async (parent: any, args: any, ctx: Context) => {
79+
return await ctx.prisma.videos.findOne({
80+
where: {
81+
id: parent.video,
82+
},
83+
});
84+
},
85+
},
86+
};

‎src/resolvers/users.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,13 @@ export default {
179179
},
180180
});
181181
},
182+
183+
reactions: async (parent: any, args: any, ctx: Context) => {
184+
return await ctx.prisma.reactions.findMany({
185+
where: {
186+
user: parent.id,
187+
},
188+
});
189+
},
182190
},
183191
};

‎src/resolvers/videos.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { Context } from '../context'
2-
import { getAuthId } from '../utils/auth'
3-
import { processUpload } from '../utils/upload'
4-
import { unlink } from 'fs'
5-
import { promisify } from 'util'
1+
import { Context } from "../context";
2+
import { getAuthId } from "../utils/auth";
3+
import { processUpload } from "../utils/upload";
4+
import { unlink } from "fs";
5+
import { promisify } from "util";
66

77
export default {
88
Query: {
99
videos: async (_parent: any, _args: any, ctx: Context) => {
10-
return await ctx.prisma.videos.findMany()
10+
return await ctx.prisma.videos.findMany();
1111
},
1212
video: async (_parent: any, args: any, ctx: Context) => {
1313
const video = await ctx.prisma.videos.findOne({
1414
where: {
1515
id: args.id,
1616
},
17-
})
17+
});
1818

19-
if (!video) throw new Error('Video not found')
19+
if (!video) throw new Error("Video not found");
2020

21-
return video
21+
return video;
2222
},
2323
},
2424

2525
Mutation: {
2626
createVideo: async (_parent: any, args: any, ctx: Context) => {
27-
const userId = getAuthId(ctx.req)
27+
const userId = getAuthId(ctx.req);
2828
// const userId = '7734e503-a268-4afc-938b-178b5b59a23c'
2929

30-
const url = await processUpload(args.data.file)
30+
const url = await processUpload(args.data.file);
3131

3232
const createdVideo = await ctx.prisma.videos.create({
3333
data: {
@@ -40,54 +40,58 @@ export default {
4040
},
4141
},
4242
},
43-
})
43+
});
4444

45-
return createdVideo
45+
return createdVideo;
4646
},
4747

4848
updateVideo: async (_parent: any, args: any, ctx: Context) => {
49-
const userId = getAuthId(ctx.req)
49+
const userId = getAuthId(ctx.req);
5050

5151
const video = await ctx.prisma.videos.findOne({
5252
where: {
5353
id: args.id,
5454
},
55-
})
55+
});
5656

57-
if (!video || video.creator !== userId) throw new Error('Video not found')
57+
if (!video || video.creator !== userId) {
58+
throw new Error("Video not found");
59+
}
5860

5961
const updatedVideo = await ctx.prisma.videos.update({
6062
where: {
6163
id: args.id,
6264
},
6365
data: args.data,
64-
})
66+
});
6567

66-
return updatedVideo
68+
return updatedVideo;
6769
},
6870

6971
deleteVideo: async (_parent: any, args: any, ctx: Context) => {
70-
const userId = getAuthId(ctx.req)
72+
const userId = getAuthId(ctx.req);
7173

7274
const video = await ctx.prisma.videos.findOne({
7375
where: {
7476
id: args.id,
7577
},
76-
})
78+
});
7779

78-
if (!video || video.creator !== userId) throw new Error('Video not found')
80+
if (!video || video.creator !== userId) {
81+
throw new Error("Video not found");
82+
}
7983

80-
const fsunlink = promisify(unlink)
84+
const fsunlink = promisify(unlink);
8185

8286
const deletedVideo = await ctx.prisma.videos.delete({
8387
where: {
8488
id: args.id,
8589
},
86-
})
90+
});
8791

88-
await fsunlink(`videos/${deletedVideo.url}`)
92+
await fsunlink(`videos/${deletedVideo.url}`);
8993

90-
return deletedVideo
94+
return deletedVideo;
9195
},
9296
},
9397

@@ -97,15 +101,23 @@ export default {
97101
where: {
98102
id: parent.creator,
99103
},
100-
})
104+
});
101105
},
102106

103107
comments: async (parent: any, args: any, ctx: Context) => {
104108
return await ctx.prisma.comments.findMany({
105109
where: {
106110
video: parent.id,
107111
},
108-
})
112+
});
113+
},
114+
115+
reactions: async (parent: any, args: any, ctx: Context) => {
116+
return await ctx.prisma.reactions.findMany({
117+
where: {
118+
video: parent.id,
119+
},
120+
});
109121
},
110122
},
111-
}
123+
};

‎src/types/reactionType.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ export default `
33
reactions: [Reaction!]!
44
}
55
6+
type Mutation {
7+
manageReaction(video: ID!, liked: Boolean!): Reaction!
8+
}
9+
610
type Reaction {
711
id: ID!
812
reaction: String!
9-
videoid: String!
10-
userid: String!
13+
video: Video!
14+
user: User!
1115
}
12-
`
16+
`;

‎src/types/userType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ export default `
5959
videos: [Video!]!
6060
comments: [Comment!]!
6161
replies: [Reply!]!
62+
reactions: [Reaction!]!
6263
}
63-
`
64+
`;

‎src/types/videoTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ export default `
3333
creator: User!
3434
createdat: String!
3535
comments: [Comment!]!
36+
reactions: [Reaction!]!
3637
}
37-
`
38+
`;

0 commit comments

Comments
(0)

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