· 6 years ago · Mar 15, 2019, 10:58 AM
1swagger: "2.0"
2info:
3 version: "0.0.1"
4 title: Movie DB
5# during dev, should point to your local machine
6host: localhost:8000
7# basePath prefixes all resource paths
8basePath: /
9#
10schemes:
11 # tip: remove http to make production-grade
12 - http
13 - https
14# format of bodies a client can send (Content-Type)
15securityDefinitions:
16 Bearer:
17 type: apiKey
18 name: Authorization
19 in: header
20
21consumes:
22 - application/json
23 - text/html
24# format of the responses to the client (Accepts)
25produces:
26 - application/json
27paths:
28 /movies:
29 # binds a127 app logic to a route
30 x-swagger-router-controller: movies
31 get:
32 security:
33 - Bearer: []
34 x-security-scopes:
35 - admin
36 description: Returns 'Hello' to the caller
37 # used as the method name of the controller
38 operationId: index
39 parameters:
40 - name: name
41 in: query
42 description: The name of the person to whom to say hello
43 required: false
44 type: string
45 responses:
46 "200":
47 description: Success
48 schema:
49 # a pointer to a definition
50 $ref: "#/definitions/MovieListBody"
51 # responses may fall through to errors
52 default:
53 description: Error
54 schema:
55 $ref: "#/definitions/ErrorResponse"
56 post:
57 description: Creates a new movie entry
58 operationId: create
59 parameters:
60 - name: movie
61 required: true
62 in: body
63 description: a new movie details
64 schema:
65 $ref: "#/definitions/MovieBody"
66 responses:
67 "200":
68 description: a successfully stored movie details
69 schema:
70 $ref: "#/definitions/MovieBody"
71 default:
72 description: Error
73 schema:
74 $ref: "#/definitions/ErrorResponse"
75
76 /movies/{id}:
77 x-swagger-router-controller: movies
78 get:
79 description: get movie
80 operationId: show
81 parameters:
82 - name: id
83 required: true
84 in: path
85 description: get particular movie details
86 type: string
87 responses:
88 "200":
89 description: Sucess
90 schema:
91 $ref: "#/definitions/MovieBody"
92 default:
93 description: Error
94 schema:
95 $ref: "#/definitions/ErrorResponse"
96
97 put:
98 description: Update Movie
99 operationId: update
100 parameters:
101 - name: id
102 required: true
103 in: path
104 type: string
105 - name: movie
106 required: true
107 in: body
108 description: an updated movie details
109 schema:
110 $ref: "#/definitions/MovieBody"
111 responses:
112 "200":
113 description: Sucess
114 schema:
115 $ref: "#/definitions/MovieBody"
116 default:
117 description: Error
118 schema:
119 $ref: "#/definitions/ErrorResponse"
120
121
122 delete:
123 description: Delete Single Record
124 operationId: deleted
125 parameters:
126 - name: id
127 required: true
128 in: path
129 description: remove single record in db
130 type: string
131 responses:
132 "200":
133 description: Sucess
134 schema:
135 $ref: "#/definitions/MovieBody"
136 default:
137 description: Error
138 schema:
139 $ref: "#/definitions/ErrorResponse"
140
141 /login:
142 x-swagger-router-controller: movies
143 post:
144 description: Get Jwt Authentication Token
145 operationId: login
146 parameters:
147 - name: Userdetails
148 required: true
149 in: body
150 description: Jwt Auth token
151 schema:
152 $ref: "#/definitions/LoginBody"
153 responses:
154 "200":
155 description: Sucess
156 schema:
157 $ref: "#/definitions/LoginBody"
158 default:
159 description: Error
160 schema:
161 $ref: "#/definitions/ErrorResponse"
162
163
164
165
166
167definitions:
168 MovieListBody:
169 required:
170 - movies
171 properties:
172 movies:
173 type: array
174 items:
175 $ref: "#/definitions/Movie"
176
177 Movie:
178 required:
179 - title
180 - gener
181 - year
182 properties:
183 title:
184 type: string
185 gener:
186 type: string
187 year:
188 type: integer
189
190
191 Login:
192 required:
193 - id
194 - name
195 - company
196 properties:
197 id:
198 type: integer
199 name:
200 type: string
201 company:
202 type: string
203
204
205 MovieBody:
206 required:
207 - movies
208 properties:
209 movies:
210 $ref: "#/definitions/Movie"
211
212 LoginBody:
213 required:
214 - details
215 properties:
216 details:
217 $ref: "#/definitions/Login"
218
219
220 ErrorResponse:
221 required:
222 - message
223 properties:
224 message:
225 type: string
226
227'use strict';
228
229var Movie = require('../models/movies')
230var MongoClient = require('mongodb').MongoClient;
231var jwt = require('jsonwebtoken')
232const redis = require('redis');
233
234
235
236const client = redis.createClient()
237client.on('connect', function () {
238 console.log('Redis client connected');
239});
240
241client.on('error', function (err) {
242 console.log('Something went wrong ' + err);
243});
244
245var db;
246
247
248module.exports = {index, create, show, update, deleted};
249
250
251//Get Method:
252function index(req,res,next)
253{
254 console.log("hai")
255 var token = VerifyToken(req,res,next)
256 jwt.verify(req.token, 'secretkey', (err, authdata) => {
257 if (err) {
258 console.log(err)
259 }
260 else {
261 client.hgetall('products', (err, results) => {
262 if (results) {
263 res.send(results)
264 }
265 else {
266 db.collection('Ecommerce').find(30).toArray((err, results) => {
267 const ttl = 0
268 client.hmset('products', results, ttl)
269
270 res.send(results)
271 });
272 }
273 })
274 // db.collection('Ecommerce').find().toArray( (err, results) => {
275 // res.send(results)
276 // });
277 }
278 })
279}
280
281//Post Method:
282function create(req,res,next)
283{
284 var movie = res.json(req.body)
285 //res.json(movie)
286 db.collection('Ecommerce').save(movie, (err, result) => {
287 if (err) return console.log(err)
288
289 res.send("Inserted Scessfully")
290 })
291}
292
293
294//Get Particulardata
295function show(req,res,next)
296{
297 var number = parseInt(req.swagger.params.id.value)
298 db.collection('Ecommerce').find({ "id":number}).toArray((err, result) => {
299 console.log(result)
300 res.send(result)
301 })
302}
303
304//Update Method
305function update(req,res,next)
306{
307 var number = parseInt(req.swagger.params.id.value)
308 db.collection("Ecommerce").update({ "id": number }, { $set: { 'title': req.body.movies.title } }, (err, result) => {
309 res.send('user updated sucessfully');
310 });
311}
312
313
314//Delete Method
315function deleted(req,res,next)
316{
317 var number = parseInt(req.swagger.params.id.value)
318 db.collection('Ecommerce').deleteOne({ "id": number }, (err, result) => {
319
320
321 });
322}
323
324
325//Login Method
326function login(req,res,next)
327{
328 const user = req.body.details
329 jwt.sign({ user }, 'secretkey', { expiresIn: '30m' }, (err, token) => {
330 res.json({ token })
331 console.log({ token })
332 })
333
334}
335
336{
337 "message": "unknown security handler: Bearer",
338 "code": "server_error",
339 "statusCode": 403
340}