-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
var passport = require('passport'); | ||
var express = require('express'); | ||
var config = require('../../config/main'); | ||
var jwt = require('jsonwebtoken'); | ||
var _ = require('underscore') | ||
var Hero = require('../models/hero'); | ||
var User = require('../models/user'); | ||
|
||
module.exports = function (app) { | ||
var apiRoutes = express.Router(); | ||
|
||
apiRoutes.get('/', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
var page_param = parseInt(req.query.page); | ||
var page = _.isNaN(page_param) | page_param < 0 ? 0 : page_param; | ||
const HEROES_PER_PAGE = 20; | ||
|
||
Hero.find({}, { _id: 0, __v: 0 }, function (err, heroes) { | ||
if (err) { | ||
res.json(err); | ||
} else { | ||
res.json(heroes); | ||
} | ||
}).skip(parseInt(req.query['offset'] || page * HEROES_PER_PAGE)).limit(parseInt(req.query['limit'] || HEROES_PER_PAGE)); | ||
}); | ||
|
||
|
||
|
||
apiRoutes.post('/favorite/:id', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
User.findOne({ email: req.user.email }, function (err, doc) { | ||
if (err) throw err; | ||
|
||
var id = req.params.id; | ||
if (doc.favorites.filter(x => x == id).length == 0) { | ||
doc.favorites.push(id); | ||
} | ||
|
||
doc.save(); | ||
return res.status(200).json({ success: true, message: 'Successfully marked hero as favorite' }); | ||
}); | ||
}); | ||
|
||
apiRoutes.delete('/favorite/:id', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
User.findOne({ email: req.user.email }, function (err, doc) { | ||
if (err) throw err; | ||
|
||
var index = doc.favorites.indexOf(req.params.id); | ||
|
||
if (index >= 0) { | ||
doc.favorites.splice(index, 1); | ||
} | ||
|
||
doc.save(); | ||
return res.status(200).json({ success: true, message: 'Successfully unmarked hero as favorite' }); | ||
}); | ||
}); | ||
|
||
|
||
apiRoutes.get('/favorite', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
User.findOne({ email: req.user.email }, function (err, doc) { | ||
if (err) throw err; | ||
|
||
return res.status(200).json({ success: true, favorites: doc.favorites }); | ||
}); | ||
}); | ||
|
||
<<<<<<< HEAD | ||
apiRoutes.get('/search/:params', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
var name = req.query.name; | ||
======= | ||
apiRoutes.get('/search', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
let name = req.query.name; | ||
>>>>>>> 2c283eecb1ae39f1d51214ac55cc428a6722a2a2 | ||
Hero.find({ name: {$regex: name, $options: 'i'}}, function (err, heroes) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
return res.status(200).json({ success: true, result: heroes }); | ||
} | ||
}) | ||
}); | ||
|
||
apiRoutes.get('/recommendation/:params', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
var name = req.query.name.split(" "); | ||
var id = req.query.id; | ||
/* var condition = ""; | ||
for(var i = 0; i <name.length; i++){ | ||
condition += "this.name == " + name[i] | ||
if(i + 1 < name.length){ | ||
condition += " || " | ||
} | ||
}*/ | ||
Hero.find({ name: {$regex: name[0], $options: 'i'}, $where: "this.id != " + id}, function (err, heroes) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
return res.status(200).json({ success: true, result: heroes }); | ||
} | ||
}).limit(3) | ||
}); | ||
|
||
apiRoutes.get('/:id', passport.authenticate('jwt', { session: false }), function (req, res) { | ||
let id = req.params.id; | ||
Hero.findOne({ id: id }, function (err, hero) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
return res.status(200).json({ success: true, hero: hero }); | ||
} | ||
}); | ||
}); | ||
|
||
// Set url for API group routes | ||
app.use('/api/heroes', apiRoutes); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters