1
1
import { Context } from "../context" ;
2
- import { getAuthId } from "../utils/auth" ;
2
+ import { getAuthId , optionalAuth } from "../utils/auth" ;
3
3
import { processUpload } from "../utils/upload" ;
4
4
import { unlink } from "fs" ;
5
5
import { promisify } from "util" ;
6
6
7
7
export default {
8
8
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 ;
11
40
} ,
12
41
video : async ( _parent : any , args : any , ctx : Context ) => {
13
42
const video = await ctx . prisma . videos . findOne ( {
@@ -18,6 +47,25 @@ export default {
18
47
19
48
if ( ! video ) throw new Error ( "Video not found" ) ;
20
49
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
+
21
69
return video ;
22
70
} ,
23
71
} ,
0 commit comments