1+ // Importing axios module to make get requests
2+ var fetch = require ( 'axios' ) ;
3+
4+ // Importing mongo model for storing search data and video details
5+ var searchData = require ( '../models/searchModel' ) ;
6+ var videoData = require ( '../models/videoModel' ) . videoData ;
7+
8+ // Search Function which regularly refreshes search results
9+ async function findByTitle ( Query , title ) {
10+
11+ try {
12+ var searchDB = await searchData . findOne ( { query : Query } ) ;
13+
14+ if ( searchDB === null ) {
15+
16+ return null ;
17+ }
18+ else {
19+
20+ var data ;
21+ for ( video in searchDB . videoDetails ) {
22+ if ( searchDB . videoDetails [ video ] . title == title ) {
23+ data = searchDB . videoDetails [ video ] ;
24+ }
25+ }
26+ return data ;
27+ }
28+ }
29+ catch ( err ) {
30+ console . log ( err ) ;
31+ return err ;
32+ }
33+ }
34+
35+ // Search Function which regularly refreshes search results
36+ async function findByDescription ( Query , description ) {
37+
38+ try {
39+ var searchDB = await searchData . findOne ( { query : Query } ) ;
40+
41+ if ( searchDB === null ) {
42+ return null ;
43+ }
44+ else {
45+
46+ var data ;
47+ for ( video in searchDB . videoDetails ) {
48+ if ( searchDB . videoDetails [ video ] . description == description ) {
49+ data = searchDB . videoDetails [ video ] ;
50+ }
51+ }
52+ return data ;
53+ }
54+ }
55+ catch ( err ) {
56+ console . log ( err ) ;
57+ return err ;
58+ }
59+ }
60+
61+ // Search Function which regularly refreshes search results
62+ async function findByTitleAndDescription ( Query , title , description ) {
63+
64+ try {
65+ var searchDB = await searchData . findOne ( { query : Query } ) ;
66+
67+ if ( searchDB === null ) {
68+ return null ;
69+ }
70+ else {
71+
72+ var data ;
73+ for ( video in searchDB . videoDetails ) {
74+ if ( searchDB . videoDetails [ video ] . title == title && searchDB . videoDetails [ video ] . description == description ) {
75+ data = searchDB . videoDetails [ video ] ;
76+ }
77+ }
78+ return data ;
79+ }
80+ }
81+ catch ( err ) {
82+ console . log ( err ) ;
83+ return err ;
84+ }
85+ }
86+
87+
88+ // Controller to provide results as per request
89+ const provideResults = async ( req , res ) => {
90+ var maxResults = 2 ;
91+ var Query = req . params . searchquery ;
92+
93+ if ( req . query . title && ! req . query . description ) {
94+
95+ var title = req . query . title ;
96+ var data = await findByTitle ( Query , title ) ;
97+ }
98+ else if ( req . query . description && ! req . query . title ) {
99+
100+ var description = req . query . description ;
101+ var data = await findByDescription ( Query , description ) ;
102+ }
103+ else if ( req . query . title && req . query . description ) {
104+
105+ var title = req . query . title ;
106+ var description = req . query . description ;
107+ var data = await findByTitleAndDescription ( Query , title , description ) ;
108+ }
109+ else {
110+ res . status ( 400 ) . json ( { response : "Please provide either title or description of the required video." } ) ;
111+ }
112+
113+
114+ if ( data === null ) {
115+ res . status ( 400 ) . json ( { response : "Please search for a video only after obtaining results through Youtube Api, thus visit the /search/:query URL first." } ) ;
116+ } else {
117+ res . status ( 200 ) . json ( data ) ;
118+ }
119+ }
120+
121+ module . exports = {
122+ provideResults
123+ }
0 commit comments