You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.6 KiB

  1. //File: controllers/tvshows.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. /* */
  5. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  6. var express = require("express");
  7. var app = express();
  8. var config = require('../config'); // get our config file
  9. app.set('superSecret', config.secret); // secret variable
  10. /* */
  11. //GET - Return all tvshows in the DB
  12. exports.findAllUsers = function(req, res) {
  13. userModel.find(function(err, users) {
  14. if(err) res.send(500, err.message);
  15. console.log('GET /users');
  16. res.status(200).jsonp(users);
  17. });
  18. };
  19. //GET - Return a TVShow with specified ID
  20. exports.findById = function(req, res) {
  21. userModel.findById(req.params.id, function(err, user) {
  22. if(err) return res.send(500, err.message);
  23. console.log('GET /users/' + req.params.id);
  24. res.status(200).jsonp(user);
  25. });
  26. };
  27. exports.findUserByUsername = function(req, res) {
  28. userModel.find({
  29. username: req.params.username
  30. }, function(err, user) {
  31. if (err) throw err;
  32. if (!user) {
  33. res.json({ success: false, message: 'no user found' });
  34. } else if (user) {
  35. console.log(user);
  36. // return the information including token as JSON
  37. res.jsonp(user);
  38. }
  39. });
  40. };
  41. //POST - Insert a new TVShow in the DB
  42. exports.addUser = function(req, res) {
  43. console.log('POST new user, name: ' + req.body.username);
  44. console.log(req.body);
  45. var user = new userModel({
  46. username: req.body.username,
  47. password: req.body.password,
  48. description: req.body.description,
  49. avatar: req.body.avatar,
  50. mail: req.body.mail,
  51. admin: req.body.admin
  52. });
  53. user.save(function(err, user) {
  54. if(err) return res.send(500, err.message);
  55. res.status(200).jsonp(user);
  56. });
  57. };
  58. //PUT - Update a register already exists
  59. exports.updateActivity = function(req, res) {
  60. ActivityModel.findById(req.params.id, function(err, tvshow) {
  61. tvshow.title = req.body.petId;
  62. tvshow.year = req.body.year;
  63. tvshow.country = req.body.country;
  64. tvshow.poster = req.body.poster;
  65. tvshow.seasons = req.body.seasons;
  66. tvshow.genre = req.body.genre;
  67. tvshow.summary = req.body.summary;
  68. tvshow.save(function(err) {
  69. if(err) return res.send(500, err.message);
  70. res.status(200).jsonp(tvshow);
  71. });
  72. });
  73. };
  74. //DELETE - Delete a TVShow with specified ID
  75. exports.deleteActivity = function(req, res) {
  76. ActivityModel.findById(req.params.id, function(err, activity) {
  77. activity.remove(function(err) {
  78. if(err) return res.send(500, err.message);
  79. res.status(200).jsonp(req.params.id);
  80. console.log('DELETE /activities/' + req.params.id);
  81. })
  82. });
  83. };
  84. //POST - auth user
  85. exports.login = function(req, res) {
  86. // find the user
  87. userModel.findOne({
  88. username: req.body.username
  89. }, function(err, user) {
  90. if (err) throw err;
  91. if (!user) {
  92. res.json({ success: false, message: 'Authentication failed. User not found.' });
  93. } else if (user) {
  94. // check if password matches
  95. if (user.password != req.body.password) {
  96. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  97. } else {
  98. // if user is found and password is right
  99. // create a token
  100. var token = jwt.sign(user, app.get('superSecret'), {
  101. //expiresInMinutes: 1440 // expires in 24 hours
  102. expiresIn: '60m'
  103. });
  104. console.log(user);
  105. // return the information including token as JSON
  106. res.json({
  107. success: true,
  108. message: 'Enjoy your token!',
  109. token: token,
  110. avatar: user.avatar
  111. });
  112. }
  113. }
  114. });
  115. };