1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+
7
+ var _invertedIndex = require ( './inverted-index' ) ;
8
+
9
+ var _invertedIndex2 = _interopRequireDefault ( _invertedIndex ) ;
10
+
11
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
12
+
13
+ function _toConsumableArray ( arr ) { if ( Array . isArray ( arr ) ) { for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) { arr2 [ i ] = arr [ i ] ; } return arr2 ; } else { return Array . from ( arr ) ; } }
14
+
15
+ /**
16
+ * Define routes and route handlers
17
+ * @function apiEndPoints
18
+ * @param {Object } app -an express application instance
19
+ * @returns {Object } -index object which maps from words to book object indexes
20
+ */
21
+ var apiEndPoints = function apiEndpoints ( app ) {
22
+ var invertedIndex = new _invertedIndex2 . default ( ) ;
23
+ app . post ( '/api/create' , function ( req , res ) {
24
+ var _req$body = req . body ,
25
+ fileName = _req$body . fileName ,
26
+ fileContent = _req$body . fileContent ;
27
+
28
+ var index = invertedIndex . createIndex ( fileName , fileContent ) ;
29
+ if ( Array . isArray ( index ) ) {
30
+ return res . status ( 400 ) . json ( index [ 1 ] ) ;
31
+ }
32
+ return res . status ( 200 ) . json ( index ) ;
33
+ } ) ;
34
+
35
+ app . post ( '/api/search' , function ( req , res ) {
36
+ var _req$body2 = req . body ,
37
+ index = _req$body2 . index ,
38
+ fileName = _req$body2 . fileName ,
39
+ terms = _req$body2 . terms ;
40
+
41
+ var termsArray = terms ? terms . split ( ', ' ) : [ ] ;
42
+ var searchResult = invertedIndex . searchIndex . apply ( invertedIndex , [ index , fileName ] . concat ( _toConsumableArray ( termsArray ) ) ) ;
43
+ if ( typeof searchResult === 'string' ) {
44
+ return res . status ( 400 ) . json ( searchResult ) ;
45
+ }
46
+ return res . status ( 200 ) . json ( searchResult ) ;
47
+ } ) ;
48
+ } ;
49
+
50
+ exports . default = apiEndPoints ;
0 commit comments