film.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. var User = require('../models/User');
  3. var Watch = require('../models/Watch');
  4. var Comment = require('../models/Comment');
  5. var Category = require('../models/Category');
  6. var Interaction = require('../models/Interaction');
  7. var Reference = require('../models/Reference');
  8. var mongoose = require('mongoose');
  9. const bodyParser = require('body-parser');
  10. /**
  11. * Get the Film info page
  12. */
  13. exports.filmGet = function(req, res){
  14. Watch.findOne({ 'permalink': req.params.permalink }, function(err, film){
  15. if (!film) {
  16. return res.redirect('/404');
  17. }
  18. if (req.user) {
  19. var phash = req.user.id + film.id
  20. } else {
  21. var phash = '_' + film.id
  22. }
  23. if (film) {
  24. Category.find({}, function(err, categories ){
  25. Comment.find({ 'attachedToWatch': film.id }, function(err, comments){
  26. Interaction.find({ 'attachedToWatch': film.id }, function(err, all_rating) {
  27. Interaction.findOne({ proofhash: phash }, function(err, interaction) {
  28. Reference.find({ 'attachedToWatch': film.id }, function(err, reference) {
  29. res.render('film', {
  30. film: film,
  31. comments: comments,
  32. categories: categories,
  33. interaction: interaction,
  34. all_rating: all_rating,
  35. reference: reference,
  36. });
  37. })
  38. })
  39. })
  40. }).populate('creator');
  41. })
  42. }
  43. }).populate('criador');
  44. };
  45. /**
  46. * Post the Film info page
  47. */
  48. exports.filmPost = function(req, res, next) {
  49. // Test if there is no commentary, so it is a rating post, so rating can not be zero
  50. if (!req.body.comment_body && req.body.stars == 0) {
  51. req.assert('stars', 'Sua avaliação não pode ficar em branco').notEmpty();
  52. }
  53. // para retornar depois do erro
  54. var form = {
  55. };
  56. var errors = req.validationErrors();
  57. if (errors) {
  58. req.flash('error', errors);
  59. return res.redirect(req.get('referer'));
  60. }
  61. Watch.findOne({ permalink: req.params.permalink }, function(err, watch) {
  62. if (req.user) {
  63. var phash = req.user.id + watch.id
  64. } else {
  65. var phash = '_' + watch.id
  66. }
  67. Interaction.findOne({ proofhash: phash }, function(err, interaction) {
  68. // If it is only a comment
  69. if (req.body.comment_body) {
  70. // Para salvar no BD
  71. var comment = new Comment({
  72. attachedToWatch: watch.id,
  73. creator: req.user.id,
  74. date: Date.now(),
  75. type:'public',
  76. body: req.body.comment_body
  77. });
  78. if (req.body.comment_body.trim() != "" || req.body.stars > 0) {
  79. comment.save(function(err) {
  80. //req.logIn(campanha, function(err) {
  81. req.flash('success', { msg: 'Seu comentário e/ou avaliação foram postados com sucesso.' });
  82. res.redirect(req.get('referer'));
  83. //});
  84. });
  85. } else {
  86. req.flash('error', { msg: 'Seu comentário e/ou avaliação estão vazios.' });
  87. res.redirect(req.get('referer'));
  88. }
  89. }
  90. // if is a star rating
  91. else if (req.body.stars) {
  92. // Para salvar no BD
  93. if (interaction) {
  94. // so we need to update
  95. interaction.stars = req.body.stars;
  96. interaction.alreadyWatched = true;
  97. interaction.save(function(err) {
  98. //req.logIn(campanha, function(err) {
  99. req.flash('success', { msg: 'Sua avaliação foi atualizada com sucesso.' });
  100. res.redirect(req.get('referer'));
  101. //});
  102. });
  103. } else {
  104. interaction = new Interaction({
  105. attachedToWatch: watch.id,
  106. creator: req.user.id,
  107. proofhash: phash,
  108. alreadyWatched: true,
  109. favorite: false,
  110. stars: req.body.stars,
  111. date: Date.now()
  112. });
  113. interaction.save(function(err) {
  114. //req.logIn(campanha, function(err) {
  115. req.flash('success', { msg: 'Sua avaliação foi postada com sucesso.' });
  116. res.redirect(req.get('referer'));
  117. //});
  118. });
  119. }
  120. }
  121. });
  122. })
  123. };
  124. exports.favoriteGet = function(req, res, next) {
  125. if (req.xhr || req.accepts('json,html') === 'json') {
  126. console.log('OI');
  127. console.log(req.body.u);
  128. Watch.findOne({ 'permalink': req.params.permalink }, function(err, watch){
  129. var phash = req.user.id + watch.id
  130. Interaction.findOne({'proofhash': phash}, function(err, interaction){
  131. // If there is a interaction...
  132. // Para salvar no BD
  133. if (interaction) {
  134. // so we need to update
  135. if (interaction.favorite) {
  136. interaction.favorite = false;
  137. }
  138. else {
  139. interaction.favorite = true;
  140. }
  141. interaction.save(function(err) {
  142. res.send({success: true})
  143. });
  144. } else {
  145. var phash = req.body.u + watch.id
  146. interaction = new Interaction({
  147. attachedToWatch: watch.id,
  148. alreadyWatched: false,
  149. favorite: true,
  150. stars: 0,
  151. creator: req.body.u,
  152. proofhash: phash,
  153. date: Date.now()
  154. });
  155. interaction.save(function(err) {
  156. res.send({success: true})
  157. });
  158. }
  159. })
  160. })
  161. }
  162. }
  163. exports.alreadyWatchedGet = function(req, res, next) {
  164. if (req.xhr || req.accepts('json,html') === 'json') {
  165. console.log('OI');
  166. console.log(req.body.u);
  167. Watch.findOne({ 'permalink': req.params.permalink }, function(err, watch){
  168. var phash = req.user.id + watch.id
  169. Interaction.findOne({'proofhash': phash}, function(err, interaction){
  170. // If there is a interaction...
  171. // Para salvar no BD
  172. if (interaction) {
  173. // so we need to update
  174. if (interaction.alreadyWatched) {
  175. interaction.alreadyWatched = false;
  176. interaction.stars = 0;
  177. }
  178. else {
  179. interaction.alreadyWatched = true;
  180. }
  181. interaction.save(function(err) {
  182. res.send({success: true})
  183. });
  184. } else {
  185. var phash = req.body.u + watch.id
  186. interaction = new Interaction({
  187. attachedToWatch: watch.id,
  188. alreadyWatched: true,
  189. favorite: false,
  190. stars: 0,
  191. creator: req.body.u,
  192. proofhash: phash,
  193. date: Date.now()
  194. });
  195. interaction.save(function(err) {
  196. res.send({success: true})
  197. });
  198. }
  199. })
  200. })
  201. }
  202. }
  203. exports.newTags = function(req, res, next) {
  204. if (req.xhr || req.accepts('json,html') === 'json') {
  205. Watch.findOne({ 'permalink': req.params.permalink }, function(err, watch){
  206. // If there is a interaction...
  207. // Para salvar no BD
  208. if (watch) {
  209. // so we need to update
  210. var phash = watch.tags + ',' + req.body.newtagsinput
  211. // If bad use of the tags recommendation, we can change tags to usertags
  212. watch.tags = phash
  213. watch.save(function(err) {
  214. res.send({success: true})
  215. });
  216. }
  217. })
  218. }
  219. }
  220. exports.downloadGet = function(req, res) {
  221. Watch.findOne({'permalink': req.params.permalink}, function(err, film) {
  222. res.render('download', {
  223. film: film
  224. })
  225. })
  226. }
  227. /**
  228. * DELETE a comment
  229. */
  230. exports.commentDelete = function(req, res, next) {
  231. Comment.findOne({ _id: req.body.del }, function(err, comment){
  232. if(comment.creator == req.user.id || req.user.adm ){
  233. Comment.remove({ _id: req.body.del }, function(err) {
  234. req.flash('info', { msg: 'O comentário foi excluída com sucesso.' });
  235. res.redirect(req.originalUrl.replace("?_method=DELETE","") + "#comments");
  236. })
  237. }
  238. })
  239. };