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 dfb4b4c

Browse files
committed
histories are created when searched or watched videos
1 parent bb34aff commit dfb4b4c

File tree

8 files changed

+167
-34
lines changed

8 files changed

+167
-34
lines changed

‎README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
# Youtube Api Clone
1+
# Youtube Like Api Clone
22

33
Video upload didn't work with node v14.2.0
44
Node downgraded to version 12.6.3
55

66
- Make `videos` folder in root directory
7+
- Make .env file in root directory
8+
9+
```
10+
TOKEN_SECRET=secret1
11+
TOKEN_EXPIRE=1h
12+
REFRESH_SECRET=secret2
13+
REFRESH_EXPIRE=1d
14+
```
15+
16+
- Create database using sql file in root directory
17+
- Create .env file inside prisma
18+
19+
```
20+
DATABASE_URL="postgresql://<username>:<password>@localhost:5432/youtube?schema=public"
21+
```
22+
23+
- Development server
24+
```bash
25+
npm run dev
26+
```
27+
28+
[http://localhost:4000/](http://localhost:4000/)

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "youtube-clone",
2+
"name": "youtube-like-clone",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",

‎prisma/schema.prisma

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ model replies {
3838
}
3939

4040
model searchhistories {
41-
id String @default(dbgenerated()) @id
42-
text String
43-
userid String?
44-
users users? @relation(fields: [userid], references: [id])
41+
id String @default(dbgenerated()) @id
42+
text String
43+
user String?
44+
users users? @relation(fields: [user], references: [id])
4545
}
4646

4747
model subscriptions {
@@ -87,11 +87,11 @@ model videos {
8787
}
8888

8989
model watchhistories {
90-
id String @default(dbgenerated()) @id
91-
userid String?
92-
videoid String?
93-
users users? @relation(fields: [userid], references: [id])
94-
videos videos? @relation(fields: [videoid], references: [id])
90+
id String @default(dbgenerated()) @id
91+
user String?
92+
video String?
93+
users users? @relation(fields: [user], references: [id])
94+
videos videos? @relation(fields: [video], references: [id])
9595
}
9696

9797
enum reaction {

‎src/resolvers/histories.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
1+
import { Context } from "../context";
2+
import { getAuthId } from "../utils/auth";
3+
14
export default {
25
Query: {
3-
searchhistories: () => [],
4-
watchhistories: () => [],
6+
searchhistories: async (parent: any, args: any, ctx: Context) => {
7+
const userId = getAuthId(ctx.req);
8+
9+
const histories = await ctx.prisma.searchhistories.findMany({
10+
where: {
11+
user: userId,
12+
},
13+
});
14+
15+
return histories;
16+
},
17+
watchhistories: async (parent: any, args: any, ctx: Context) => {
18+
const userId = getAuthId(ctx.req);
19+
20+
const histories = await ctx.prisma.watchhistories.findMany({
21+
where: {
22+
user: userId,
23+
},
24+
});
25+
26+
return histories;
27+
},
528
},
6-
}
29+
30+
WatchHistory: {
31+
user: async (parent: any, args: any, ctx: Context) => {
32+
return await ctx.prisma.users.findOne({
33+
where: {
34+
id: parent.user,
35+
},
36+
});
37+
},
38+
39+
video: async (parent: any, args: any, ctx: Context) => {
40+
return await ctx.prisma.videos.findOne({
41+
where: {
42+
id: parent.video,
43+
},
44+
});
45+
},
46+
},
47+
48+
SearchHistory: {
49+
user: async (parent: any, args: any, ctx: Context) => {
50+
return await ctx.prisma.users.findOne({
51+
where: {
52+
id: parent.user,
53+
},
54+
});
55+
},
56+
},
57+
};

‎src/resolvers/videos.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
import { Context } from "../context";
2-
import { getAuthId } from "../utils/auth";
2+
import { getAuthId,optionalAuth } from "../utils/auth";
33
import { processUpload } from "../utils/upload";
44
import { unlink } from "fs";
55
import { promisify } from "util";
66

77
export default {
88
Query: {
9-
videos: async (_parent: any, _args: any, ctx: Context) => {
10-
return await ctx.prisma.videos.findMany();
9+
videos: async (_parent: any, args: any, ctx: Context) => {
10+
let searchQuery = {};
11+
12+
if (args.query) {
13+
const userId = optionalAuth(ctx.req);
14+
15+
if (typeof userId === "string") {
16+
await ctx.prisma.searchhistories.create({
17+
data: {
18+
text: args.query,
19+
users: {
20+
connect: {
21+
id: userId,
22+
},
23+
},
24+
},
25+
});
26+
}
27+
28+
searchQuery = {
29+
where: {
30+
title: {
31+
contains: args.query,
32+
},
33+
},
34+
};
35+
}
36+
37+
const videos = await ctx.prisma.videos.findMany(searchQuery);
38+
39+
return videos;
1140
},
1241
video: async (_parent: any, args: any, ctx: Context) => {
1342
const video = await ctx.prisma.videos.findOne({
@@ -18,6 +47,25 @@ export default {
1847

1948
if (!video) throw new Error("Video not found");
2049

50+
const userId = optionalAuth(ctx.req);
51+
52+
if (typeof userId === "string") {
53+
await ctx.prisma.watchhistories.create({
54+
data: {
55+
users: {
56+
connect: {
57+
id: userId,
58+
},
59+
},
60+
videos: {
61+
connect: {
62+
id: video.id,
63+
},
64+
},
65+
},
66+
});
67+
}
68+
2169
return video;
2270
},
2371
},

‎src/types/historyType.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export default `
77
type SearchHistory {
88
id: ID!
99
text: String!
10-
userid: String
10+
user: User!
1111
}
1212
1313
type WatchHistory {
1414
id: ID!
15-
userid: String!
16-
videoid: String!
15+
user: User!
16+
video: Video!
1717
}
18-
`
18+
`;

‎src/types/videoTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default `
22
scalar Upload
33
44
type Query {
5-
videos: [Video!]!
5+
videos(query: String): [Video!]!
66
video(id: ID!): Video!
77
}
88

‎src/utils/auth.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
import { ContextParameters } from 'graphql-yoga/dist/types'
2-
import { verifyToken } from './jwt'
1+
import { ContextParameters } from "graphql-yoga/dist/types";
2+
import { verifyToken } from "./jwt";
33

44
export interface decodedToken {
5-
iat: number
6-
exp: number
7-
id: string
5+
iat: number;
6+
exp: number;
7+
id: string;
88
}
99

1010
const getAuthId = (req: ContextParameters): string => {
11-
const header = req.request.headers.authorization
12-
if (!header) throw new Error("You aren't authorized")
11+
const header = req.request.headers.authorization;
12+
if (!header) throw new Error("You aren't authorized");
1313

14-
const token = header.replace('Bearer ','')
14+
const token = header.replace("Bearer ","");
1515

16-
const decoded: decodedToken = verifyToken(token) as decodedToken
16+
const decoded: decodedToken = verifyToken(token) as decodedToken;
1717

18-
return decoded.id
19-
}
18+
return decoded.id;
19+
};
20+
21+
const optionalAuth = (req: ContextParameters): string | boolean => {
22+
const header = req.request.headers.authorization;
23+
24+
if (header) {
25+
const token = header.replace("Bearer ", "");
26+
const decoded: decodedToken = verifyToken(token) as decodedToken;
27+
28+
return decoded.id;
29+
}
30+
return false;
31+
};
2032

21-
export { getAuthId}
33+
export { getAuthId,optionalAuth};

0 commit comments

Comments
(0)

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