@@ -2,53 +2,69 @@ const mongoose = require('mongoose');
2
2
let Airline = require ( '../models/Airline' ) ;
3
3
4
4
module . exports . addAirlineDetails = ( req , res ) => {
5
- let airline = new Airline ( ) ;
6
- airline . name = req . body . name ;
7
- airline . slogan = req . body . slogan ;
8
- airline . founded_on = req . body . founded_on ;
9
- airline . origin_contry = req . body . origin_contry ;
10
- airline . hubs = req . body . hubs ;
11
- airline . focus_cities = req . body . focus_cities ;
12
- airline . best_travel_reward = req . body . best_travel_reward ;
13
- airline . save ( ( err ) => {
14
- if ( err ) { console . error ( err ) }
15
- res . status ( 200 ) ;
16
- res . json ( airline ) ;
17
- } ) ;
18
- console . log ( req . body ) ;
19
- console . log ( airline ) ;
5
+
6
+ if ( ! req . payload . _id ) {
7
+ res . status ( 401 ) . json ( {
8
+ "message" : "UnauthorizedError: Dont try in novice hacker way"
9
+ } ) ;
10
+ } else {
11
+ let airline = new Airline ( ) ;
12
+ airline . name = req . body . name ;
13
+ airline . slogan = req . body . slogan ;
14
+ airline . founded_on = req . body . founded_on ;
15
+ airline . origin_contry = req . body . origin_contry ;
16
+ airline . hubs = req . body . hubs ;
17
+ airline . focus_cities = req . body . focus_cities ;
18
+ airline . best_travel_reward = req . body . best_travel_reward ;
19
+ airline . save ( ( err ) => {
20
+ if ( err ) { console . error ( err ) }
21
+ res . status ( 200 ) ;
22
+ res . json ( airline ) ;
23
+ } ) ;
24
+ }
20
25
}
21
26
module . exports . updateAirlineDetails = ( req , res ) => {
22
- Airline . findByIdAndUpdate (
23
- { _id : req . params . id } ,
24
- {
25
- $set : {
26
- name : req . body . name ,
27
- slogan : req . body . slogan ,
28
- founded_on : req . body . founded_on ,
29
- origin_contry : req . body . origin_contry ,
30
- hubs : req . body . hubs ,
31
- focus_cities : req . body . focus_cities ,
32
- best_travel_reward : req . body . best_travel_reward
33
- }
34
- } , ( err , item ) => {
35
- if ( err ) {
36
- res . json ( { mag : "Unable to update item" , err : err } ) ;
37
- } else {
38
- res . json ( { msg : 'Item updated successfully' } ) ;
39
- }
27
+ if ( ! req . payload . _id ) {
28
+ res . status ( 401 ) . json ( {
29
+ "message" : "UnauthorizedError: Dont try in novice hacker way"
40
30
} ) ;
41
-
31
+ } else {
32
+ Airline . findByIdAndUpdate (
33
+ { _id : req . params . id } ,
34
+ {
35
+ $set : {
36
+ name : req . body . name ,
37
+ slogan : req . body . slogan ,
38
+ founded_on : req . body . founded_on ,
39
+ origin_contry : req . body . origin_contry ,
40
+ hubs : req . body . hubs ,
41
+ focus_cities : req . body . focus_cities ,
42
+ best_travel_reward : req . body . best_travel_reward
43
+ }
44
+ } , ( err , item ) => {
45
+ if ( err ) {
46
+ res . json ( { msg : "Unable to update item" , err : err } ) ;
47
+ } else {
48
+ res . json ( { msg : 'Item updated successfully' } ) ;
49
+ }
50
+ } ) ;
51
+ }
42
52
}
43
53
module . exports . deleteAirlineDetails = ( req , res ) => {
44
- Airline . remove (
45
- { _id : req . params . id } , ( err , item ) => {
46
- if ( err ) {
47
- res . json ( { mag : "Unable to delete item" , err : err } ) ;
48
- } else {
49
- res . json ( { msg : 'Item delete successfully' } ) ;
50
- }
54
+ if ( ! req . payload . _id ) {
55
+ res . status ( 401 ) . json ( {
56
+ "message" : "UnauthorizedError: Dont try in novice hacker way"
51
57
} ) ;
58
+ } else {
59
+ Airline . remove (
60
+ { _id : req . params . id } , ( err , item ) => {
61
+ if ( err ) {
62
+ res . json ( { msg : "Unable to delete item" , err : err } ) ;
63
+ } else {
64
+ res . json ( { msg : 'Item delete successfully' } ) ;
65
+ }
66
+ } ) ;
67
+ }
52
68
}
53
69
module . exports . getAirlinesDetails = ( req , res ) => {
54
70
@@ -57,4 +73,54 @@ module.exports.getAirlinesDetails = (req, res) => {
57
73
} ) ;
58
74
}
59
75
module . exports . getAirlineDetails = ( req , res ) => {
60
- }
76
+ }
77
+ module . exports . submitReview = ( req , res ) => {
78
+ if ( ! req . payload . _id ) {
79
+ res . status ( 401 ) . json ( {
80
+ "message" : "UnauthorizedError: Dont try in novice hacker way"
81
+ } ) ;
82
+ } else {
83
+ let userid = req . payload . _id ;
84
+ let airlineid = req . params . id ;
85
+ Airline . findById ( { _id : airlineid } ,
86
+ ( err , item ) => {
87
+ if ( err ) {
88
+ res . json ( { msg : "Unable to get airline" , err : err } ) ;
89
+ } else {
90
+ let existingRating = item . rating . find ( function ( ele ) {
91
+ return ele . username == userid ;
92
+ } ) ;
93
+ let existingComment = item . comments . find ( function ( ele ) {
94
+ return ele . username == userid ;
95
+ } ) ;
96
+ if ( existingRating ) {
97
+ item . rating . id ( existingRating . _id ) . rate = req . body . rating ;
98
+ item . comments . id ( existingComment . _id ) . comment = req . body . comment ;
99
+ item . save ( ) ;
100
+ res . json ( { msg : 'Ratings updated successful' } ) ;
101
+ } else {
102
+ Airline . findByIdAndUpdate ( { _id : airlineid } , {
103
+ $push : {
104
+ rating : {
105
+ username : userid ,
106
+ rate : req . body . rating
107
+ } ,
108
+ comments : {
109
+ username : userid ,
110
+ comment : req . body . comment
111
+ }
112
+ }
113
+ } , ( err , data ) => {
114
+ if ( err ) {
115
+ res . json ( { msg : "Unable to store your review" , err : err } ) ;
116
+ } else {
117
+ res . json ( { msg : 'Ratings updated successful' } ) ;
118
+ }
119
+ } ) ;
120
+ }
121
+ }
122
+ } )
123
+ }
124
+ }
125
+
126
+
0 commit comments