Advertisement
Guest User

Untitled

a guest
Dec 7th, 2015
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.42 KB | None | 0 0
  1. //Start of Node modules
  2. var express = require("express");
  3. var app = express();
  4. var port = process.env.PORT || 8888;
  5. var server = app.listen(port);
  6. var session = require("express-session");
  7. var bodyParser = require("body-parser");
  8. var mongo = require("mongodb");
  9. var monk = require("monk");
  10. var db = monk("localhost:27017/dynamiccomps");
  11. var uuid = require("uuid-v4");
  12. //var db = monk("mongodb://helloworld:[email protected]:31882/dynamiccomps");
  13. var multer = require("multer");
  14. var upload = multer({dest:"./public/images/profile_pictures"});
  15. var fs = require("fs");
  16. var lolApi = require("leagueapi");
  17. //end of node modules
  18.  
  19. //Declare Variables
  20. var sess;
  21. var database;
  22. var loginCounter = 0;
  23. var mimetypes = ["image/jpeg", "image/jpg", "image/png", "image/big"];
  24. var isItImage = 0;
  25. var champsArray = [];
  26. var arrayToUser = [];
  27. var champsArrayToUser = [];
  28. var userArray = [];
  29. var roles = ["Top", "Jungle", "Mid", "ADC", "Support"];
  30. var imgNumber = [0,1,2,3,4];
  31. var itemsArrayKey = [];
  32. var itemsArrayValue = [];
  33. var trinketsArray = [];
  34. var itemsArrayKeyUser = [];
  35. var itemsArrayValueUser = [];
  36. //End of Declare Variables
  37.  
  38.  
  39. //Middleware
  40.  
  41. app.use(session({
  42. secret:"HelloWorld"
  43. }));
  44. app.use(express.static(__dirname + "/public")); //Changes all static files to directory
  45. app.use(bodyParser.urlencoded({extended:true})); //Used to get POST values
  46.  
  47.  
  48. //Configuring Handlebars
  49. var handlebars = require("express-handlebars").create({defaultLayout:"main"});
  50. app.engine("handlebars", handlebars.engine);
  51. app.set("view engine", "handlebars");
  52. //End of configuring handlebars
  53.  
  54.  
  55. //Configuring League API
  56. lolApi.init("dcb37a5f-c21d-4a33-9bd1-b0c40737bc54","oce");
  57.  
  58.  
  59. //append to champsArray
  60. lolApi.Static.getChampionList(true, function(err, champs) {
  61. for (var key in champs.data) {
  62. if (champs.data.hasOwnProperty(key)) {
  63. champsArray.push(key);
  64. }
  65. }
  66.  
  67. for (var key in champs.data) {
  68. if (champs.data.hasOwnProperty(key)) {
  69. userArray.push(key);
  70. }
  71. }
  72. });
  73.  
  74.  
  75. //Getting Itemlist from Riot Games API
  76. lolApi.Static.getItemList(true, function (err, items) {
  77. for (var key in items.data) {
  78. if (items.data.hasOwnProperty(key)) {
  79. itemsArrayKey.push(key);
  80. itemsArrayValue.push(items.data[key].name);
  81. }
  82. }
  83. for (var i = 0;i<itemsArrayValue.length;i++) {
  84. var index = itemsArrayValue[i].indexOf("(Trinket)");
  85. if (index > -1) {
  86. trinketsArray.push(itemsArrayKey[i]);
  87. }
  88. }
  89.  
  90. for (var i = 0;i<itemsArrayValue.length;i++) {
  91. var index = itemsArrayValue[i].indexOf("(Trinket)");
  92. if (index > -1) {
  93.  
  94. }
  95. else {
  96. itemsArrayKeyUser.push(itemsArrayKey[i]);
  97. itemsArrayValueUser.push(itemsArrayValue[i]);
  98. }
  99. }
  100.  
  101. });
  102.  
  103.  
  104.  
  105.  
  106. //End of configuring APi
  107.  
  108. //End of middleware
  109.  
  110.  
  111.  
  112.  
  113.  
  114. app.use(function (req, res, next) { //Connects db to every page
  115.  
  116. req.db = db;
  117. database = req.db;
  118. next(); //Passes on to the next router
  119.  
  120. });
  121.  
  122. /////////////FUNCTIONS FOR TIME START HERE/////////////////////////
  123. function secsFunction(s) {
  124. var ms = s % 1000;
  125. s = (s - ms) / 1000;
  126. var secs = s % 60;
  127. s = (s - secs) / 60;
  128. var mins = s % 60;
  129. s = (s - mins) / 60;
  130. var hrs = s % 24;
  131. var days = (s - hrs) / 24;
  132.  
  133. return secs;
  134. }
  135.  
  136.  
  137. function minsFunction(s) {
  138. var ms = s % 1000;
  139. s = (s - ms) / 1000;
  140. var secs = s % 60;
  141. s = (s - secs) / 60;
  142. var mins = s % 60;
  143. s = (s - mins) / 60;
  144. var hrs = s % 24;
  145. var days = (s - hrs) / 24;
  146.  
  147. return mins;
  148. }
  149.  
  150. function hrsFunction(s) {
  151. var ms = s % 1000;
  152. s = (s - ms) / 1000;
  153. var secs = s % 60;
  154. s = (s - secs) / 60;
  155. var mins = s % 60;
  156. s = (s - mins) / 60;
  157. var hrs = s % 24;
  158. var days = (s - hrs) / 24;
  159.  
  160. return hrs;
  161. }
  162.  
  163. function daysFunction(s) {
  164. var ms = s % 1000;
  165. s = (s - ms) / 1000;
  166. var secs = s % 60;
  167. s = (s - secs) / 60;
  168. var mins = s % 60;
  169. s = (s - mins) / 60;
  170. var hrs = s % 24;
  171. var days = (s - hrs) / 24;
  172.  
  173. return days;
  174. }
  175.  
  176.  
  177. /////////////FUNCTIONS FOR TIME ENDS HERE/////////////////////////
  178.  
  179. app.get("/", function (req, res) {
  180. sess = req.session;
  181. if (sess.user) {
  182. app.locals.signedYesOrNo = sess.user;
  183. }
  184. else {
  185.  
  186. }
  187.  
  188.  
  189. var newDate = new Date();
  190. var utcMilli = newDate.getTime();
  191. var upvoteArray = [];
  192. var guides = database.get("guides");
  193. guides.find({}).on("success", function (doc) {
  194. console.log("doc.length is" + doc.length);
  195. for (var i = 0;i<doc.length;i++) {
  196. var xAgo = 0;
  197. var whatType = "";
  198. var milliDifference = utcMilli - doc[i].userUTC;
  199. var algoNum = (doc[i].likes/(milliDifference)) * 100;
  200.  
  201.  
  202. if (daysFunction(milliDifference) != 0) {
  203. xAgo = daysFunction(milliDifference);
  204. if (xAgo == 1) {
  205. whatType = "day";
  206. }
  207. else {
  208. whatType = "days";
  209. }
  210. }
  211. else {
  212. if (hrsFunction(milliDifference) != 0) {
  213. xAgo = hrsFunction(milliDifference);
  214. if (xAgo == 1) {
  215. whatType = "hour";
  216. }
  217. else {
  218. whatType = "hours";
  219. }
  220. }
  221. else {
  222. if (minsFunction(milliDifference) != 0) {
  223. xAgo = minsFunction(milliDifference);
  224. if (xAgo == 1) {
  225. whatType = "min";
  226. }
  227. else {
  228. whatType = "mins";
  229. }
  230. }
  231. else {
  232. if (secsFunction(milliDifference) != 0) {
  233. xAgo = secsFunction(milliDifference);
  234. if (xAgo == 1) {
  235. whatType = "sec";
  236. }
  237. else {
  238. whatType = "secs";
  239. }
  240. }
  241. }
  242. }
  243. }
  244. var likesHome = null;
  245. var dislikesHome = null;
  246.  
  247. var obj = doc[i].likesLog.filter(function (obj) {
  248. return obj.user === sess.user;
  249. })[0];
  250.  
  251.  
  252. if (obj) {
  253. if (obj.val == 1) {
  254. likesHome = "true";
  255. }
  256. else if (obj.val == -1) {
  257. dislikesHome = "true";
  258. }
  259. }
  260. else {
  261. console.log("The post with the id:" + doc[i].guideId + "does not have any upvotes or downvotes by you");
  262. }
  263.  
  264.  
  265. upvoteArray.push({name:doc[i].guideName,likeNum:algoNum,guide:doc[i],howLong:xAgo,longType:whatType,likess:likesHome,dislikess:dislikesHome});
  266. console.log("upvoteArray after push is:" + upvoteArray);
  267. if (i == (doc.length -1)) {
  268.  
  269. upvoteArray.sort(function(a, b){
  270. return b.likeNum-a.likeNum;
  271. })
  272.  
  273.  
  274. res.render("home", {guideArray:upvoteArray});
  275. }
  276. }
  277.  
  278.  
  279. });
  280. });
  281.  
  282. app.get("/signup", function (req, res) {
  283. sess = req.session;
  284. if (sess.user) {
  285. app.locals.signedYesOrNo = sess.user;
  286. }
  287. else {
  288.  
  289. }
  290. res.render("signup");
  291. });
  292.  
  293. app.get("/account", function (req, res) {
  294. sess = req.session;
  295. if (sess.user) {
  296. app.locals.signedYesOrNo = sess.user;
  297. }
  298. else {
  299.  
  300. }
  301. if (!sess.user) {
  302. res.redirect("/");
  303. }
  304. var account = database.get("account");
  305. var guides = database.get("guides");
  306. account.find({"name":sess.user}).on("success", function (doc) {
  307. guides.find({"by":sess.user}).on("success", function (doc2) {
  308. var dateTime = new Date();
  309. var currentTime = dateTime.getTime();
  310. var accountArray = [];
  311. for (var i = 0;i<doc2.length;i++) {
  312. var xAgo = 0;
  313. var whatType = "";
  314. var milliDifference = currentTime - doc2[i].userUTC;
  315. var algoNum = (doc2[i].likes/(milliDifference)) * 100;
  316.  
  317.  
  318. if (daysFunction(milliDifference) != 0) {
  319. xAgo = daysFunction(milliDifference);
  320. if (xAgo == 1) {
  321. whatType = "day";
  322. }
  323. else {
  324. whatType = "days";
  325. }
  326. }
  327. else {
  328. if (hrsFunction(milliDifference) != 0) {
  329. xAgo = hrsFunction(milliDifference);
  330. if (xAgo == 1) {
  331. whatType = "hour";
  332. }
  333. else {
  334. whatType = "hours";
  335. }
  336. }
  337. else {
  338. if (minsFunction(milliDifference) != 0) {
  339. xAgo = minsFunction(milliDifference);
  340. if (xAgo == 1) {
  341. whatType = "min";
  342. }
  343. else {
  344. whatType = "mins";
  345. }
  346. }
  347. else {
  348. if (secsFunction(milliDifference) != 0) {
  349. xAgo = secsFunction(milliDifference);
  350. if (xAgo == 1) {
  351. whatType = "sec";
  352. }
  353. else {
  354. whatType = "secs";
  355. }
  356. }
  357. }
  358. }
  359. }
  360. accountArray.push({name:doc2[i].guideName,likeNum:algoNum,guide:doc2[i],howLong:xAgo,longType:whatType});
  361. }//END OF FOR LOOP
  362. accountArray.sort(function(a, b){
  363. return b.likeNum-a.likeNum;
  364. })
  365.  
  366. res.render("account",{user:doc[0],accountArray:accountArray});
  367. });
  368. });
  369. });
  370.  
  371.  
  372. app.post("/account", upload.single("changeAvatar") ,function (req, res) {
  373. sess = req.session;
  374. if (sess.user) {
  375. app.locals.signedYesOrNo = sess.user;
  376. }
  377. else {
  378.  
  379. }
  380.  
  381. var extension;
  382. for (var i = 0;i<mimetypes.length;i++) {
  383. if (req.file.mimetype === mimetypes[i]) {
  384. isItImage = 1;
  385. extension = req.file.mimetype.slice(6,req.file.mimetype.length);
  386.  
  387. }
  388. }
  389. if (isItImage == 0) {
  390. console.log("Please upload a legit image");
  391. }
  392. else {
  393. var account = database.get("account");
  394. var fileName = req.file.filename + "." + extension;
  395.  
  396. fs.rename(__dirname + "/public/images/profile_pictures/" + req.file.filename,__dirname + "/public/images/profile_pictures/" + sess.user + "." + extension, function (error) {
  397. if (error) {
  398. console.log("There was an error");
  399. }
  400. else {
  401. account.findAndModify({query:{"name":sess.user} , update:{"$set":{"profilePic":sess.user + ".png"}}});
  402. res.redirect("/account");
  403. }
  404. });
  405. }
  406.  
  407.  
  408.  
  409. });
  410.  
  411. app.get("/signup", function (req, res) {
  412. sess = req.session;
  413. if (sess.user) {
  414. app.locals.signedYesOrNo = sess.user;
  415. }
  416. else {
  417.  
  418. }
  419. if (sess.failLogin) {
  420. app.locals.failLogin = "This username is already taken";
  421. loginCounter++;
  422. }
  423. if (loginCounter == 2) {
  424. delete app.locals.failLogin;
  425. loginCounter = 0;
  426. }
  427. res.render("signup");
  428. });
  429.  
  430. app.get("/signupProc", function (req, res) {
  431. sess = req.session;
  432. if (sess.user) {
  433. app.locals.signedYesOrNo = sess.user;
  434. }
  435. else {
  436.  
  437. }
  438. res.redirect("/signup");
  439. });
  440.  
  441.  
  442. app.get("/create", function (req, res) {
  443. sess = req.session;
  444. if (sess.user) {
  445. app.locals.signedYesOrNo = sess.user;
  446. }
  447. else {
  448.  
  449. }
  450.  
  451. if (!sess.user) {
  452. res.redirect("/");
  453. }
  454. else {
  455. //Find guides
  456. var guides = database.get("guides");
  457.  
  458.  
  459. res.render("create", {champArray:champsArray, roles: roles, itemsArrayKey:itemsArrayKeyUser,itemsArrayValue:itemsArrayValueUser,trinketsArray:trinketsArray});
  460.  
  461. }
  462. });
  463.  
  464.  
  465.  
  466.  
  467. app.get("/showChamps", function (req, res) {
  468. sess = req.session;
  469. if (sess.user) {
  470. app.locals.signedYesOrNo = sess.user;
  471. }
  472. else {
  473.  
  474. }
  475. var champText = req.query.textInput;
  476. var sendChamps = req.query.sendChampsArray;
  477. var sendChampsArray = JSON.parse(sendChamps);
  478. arrayToUser = [];
  479.  
  480. for (var i = 0;i<champsArray.length;i++) {
  481. if (champsArray[i].toLowerCase().indexOf(champText.toLowerCase()) > -1 ) {
  482. arrayToUser.push(champsArray[i]);
  483. }
  484. }
  485.  
  486.  
  487. for (var i = 0;i<sendChampsArray.length;i++) {
  488. var index = arrayToUser.indexOf(sendChampsArray[i]);
  489. if (index > -1) {
  490. arrayToUser.splice(index, 1);
  491. }
  492. }
  493.  
  494. res.send(arrayToUser);
  495. });
  496.  
  497.  
  498. app.post("/sendChamps", function (req, res) {
  499. sess = req.session;
  500. if (sess.user) {
  501. app.locals.signedYesOrNo = sess.user;
  502. }
  503. else {
  504.  
  505. }
  506. var checkChamps;
  507. var error = 0;
  508. var guides = database.get("guides");
  509. var guideName = req.body.guideName;
  510. var sendChampsArrayString = JSON.parse(req.body.sendChampsArrayString);
  511. var notesTextArrayString = JSON.parse(req.body.notesTextArrayString);
  512. var itemsAndTrinketArrayString = JSON.parse(req.body.itemsAndTrinketArrayString);
  513. var gamePlayArrayString = JSON.parse(req.body.gamePlayArrayString);
  514.  
  515. console.log("Guidename: " + guideName);
  516. console.log("sendChampsArraySTring: " + sendChampsArrayString);
  517. console.log("notesTextArrayString: " + notesTextArrayString);
  518. console.log("itemsAndTrinketArraySTring: " + itemsAndTrinketArrayString);
  519. console.log("gamePlayArrayString: " + gamePlayArrayString);
  520.  
  521. //ERROR CHECKING
  522. guides.find({"guideName":guideName}).on("success", function (doc) {
  523. if (doc == 0) {
  524. if (guideName === "" || !guideName.replace(/\s/g, '').length) {
  525. console.log("The guidename is either NULL or just whitespace");
  526. error = 1;
  527. res.send("false");
  528. }
  529. else {
  530. if (guideName.length > 50) {
  531. console.log("The guidename is too long");
  532. error = 1;
  533. res.send("false");
  534. }
  535. else {
  536. var check = sendChampsArrayString.some(function (el) {
  537. return el !== null;
  538. }); // true
  539.  
  540. if (check == true) {
  541. var guideId = database.get("guideId");
  542.  
  543.  
  544. guideId.find({}).on("success", function (doc) {
  545. if (doc == 0) {
  546. console.log("Oops");
  547. }
  548. else {
  549. var id = doc[0].id;
  550. var currentdate = new Date();
  551. var utcUser = currentdate.getTime();
  552. guides.insert({"guideName":guideName,"by":sess.user,"userUTC":utcUser,"likesLog":[{"user":sess.user,"val":1}],"champs":sendChampsArrayString, "champNotes":notesTextArrayString,"itemsTrinkets":itemsAndTrinketArrayString,"gamePlay":gamePlayArrayString,likes:1,"guideId":id,"roles":["Top","Jungle","Mid","Adc","Support"],"userLiked":"true","userDisliked":null});
  553. guideId.findAndModify({query:{"id":id} , update:{"$set":{"id":id+1}}});
  554. res.send("true");
  555. }
  556. });
  557. }
  558. else {
  559. res.send("false");
  560. }
  561. }
  562. }
  563. }
  564. else {
  565. res.send("false");
  566. }
  567. });
  568. });
  569.  
  570.  
  571. app.get("/showItemSugg", function (req, res) {
  572. sess = req.session;
  573. if (sess.user) {
  574. app.locals.signedYesOrNo = sess.user;
  575. }
  576. else {
  577.  
  578. }
  579. var input = req.query.input;
  580. var itemArrayToUser = [];
  581. for (var i = 0;i<itemsArrayKeyUser.length;i++) {
  582. if (itemsArrayValueUser[i].toLowerCase().indexOf(input.toLowerCase()) > -1) {
  583. itemArrayToUser.push(itemsArrayKeyUser[i]);
  584. }
  585. }
  586. res.send(itemArrayToUser);
  587. });
  588.  
  589. app.post("/upvote", function (req, res) {
  590. sess = req.session;
  591. if (sess.user) {
  592. app.locals.signedYesOrNo = sess.user;
  593. }
  594. else {
  595.  
  596. }
  597. if (!sess.user) {
  598. res.send("login");
  599. }
  600. else {
  601. var upvoteId = req.body.upvoteId;
  602. var guides = database.get("guides");
  603. var upvoteError = 0;
  604. console.log(upvoteId);
  605. guides.find({"guideId":Number(upvoteId)}).on("success", function (doc) {
  606. for (var i = 0;i<doc[0].likesLog.length;i++) {
  607. if (doc[0].likesLog[i].val == "1" && doc[0].likesLog[i].user == sess.user) {
  608. console.log("YOU CAN't UPVOPTE YOU FUCKING RETARD");
  609. upvoteError = 1;
  610. break;
  611. }
  612. else if (doc[0].likesLog[i].val == "-1" && doc[0].likesLog[i].user == sess.user) {
  613. upvoteError = 2;
  614. }
  615. }
  616.  
  617. if (upvoteError == 0) {
  618. var newNum = doc[0].likes + 1;
  619. guides.update({"guideId":Number(upvoteId)},{"$push":{likesLog:{"user":sess.user,"val":1}}});
  620. guides.update({"guideId":Number(upvoteId)},{ "$set" : {"likes": newNum}});
  621. guides.update({"guideId":Number(upvoteId)}, {"$set":{"userLiked":"true"}});
  622. console.log("You upvoted that isn't really nice");
  623. res.send(JSON.stringify({likes:newNum,newImg:"true"}));
  624.  
  625. }
  626.  
  627. else if (upvoteError == 1) {
  628. var newNum = doc[0].likes - 1;
  629. guides.update({"guideId":Number(upvoteId)},{"$pull":{"likesLog":{"user":sess.user}}});
  630. guides.update({"guideId":Number(upvoteId)},{"$set":{"likes":newNum}});
  631. guides.update({"guideId":Number(upvoteId)}, {"$set":{"userLiked":null}});
  632. res.send(JSON.stringify({likes:newNum,backToNormal:"true"}));
  633. }
  634. else if (upvoteError == 2) {
  635. console.log("You are on error 2");
  636. var newNum = doc[0].likes + 2;
  637. guides.update({"guideId":Number(upvoteId),"likesLog.user":sess.user},{"$set":{"likesLog.$.val":1}});
  638. guides.update({"guideId":Number(upvoteId)},{ "$set" : {"likes": newNum}});
  639. guides.update({"guideId":Number(upvoteId)}, {"$set":{"userLiked":"true"}});
  640. guides.update({"guideId":Number(upvoteId)}, {"$set":{"userDisliked":null}});
  641.  
  642. res.send(JSON.stringify({likes:newNum,newImg:"true"}));
  643. }
  644.  
  645. });
  646. }
  647.  
  648.  
  649. });
  650.  
  651. app.post("/downvote", function (req, res) {
  652. sess = req.session;
  653. if (sess.user) {
  654. app.locals.signedYesOrNo = sess.user;
  655. }
  656. else {
  657.  
  658. }
  659. if (!sess.user) {
  660. res.send("login");
  661. }
  662. else {
  663. var downvoteError = 0;
  664. var downvoteId = req.body.downvoteId;
  665. var guides = database.get("guides");
  666. guides.find({"guideId":Number(downvoteId)}).on("success", function (doc) {
  667. for (var i = 0;i<doc[0].likesLog.length;i++) {
  668. if (doc[0].likesLog[i].val == "-1" && doc[0].likesLog[i].user == sess.user) {
  669. console.log("YOU CANNOT UPLOAD");
  670. downvoteError = 1;
  671. break;
  672. }
  673. else if (doc[0].likesLog[i].val == "1" && doc[0].likesLog[i].user == sess.user) {
  674. downvoteError = 2;
  675. break;
  676. }
  677. }
  678.  
  679. if (downvoteError == 0) {
  680. var newNum = doc[0].likes -1;
  681. guides.update({"guideId":Number(downvoteId)},{"$push":{likesLog:{"user":sess.user,"val":-1}}});
  682. guides.update({"guideId":Number(downvoteId)},{ "$set" : {"likes": newNum}});
  683. guides.update({"guideId":Number(downvoteId)}, {"$set":{"userDisliked":"true"}});
  684. console.log("YOUR UPVOTE GOT ADDEED TO THE LIKESLOG LIST");
  685. res.send(JSON.stringify({likes:newNum,newImg:"true"}));
  686. }
  687.  
  688. else if (downvoteError == 1) {
  689. var newNum = doc[0].likes + 1;
  690. guides.update({"guideId":Number(downvoteId)},{"$pull":{"likesLog":{"user":sess.user}}});
  691. guides.update({"guideId":Number(downvoteId)},{"$set":{"likes":newNum}});
  692. guides.update({"guideId":Number(downvoteId)}, {"$set":{"userDisliked":null}});
  693. res.send(JSON.stringify({likes:newNum,backToNormal:"true"}));
  694. }
  695.  
  696. else if (downvoteError == 2) {
  697. var newNum = doc[0].likes + -2;
  698. guides.update({"guideId":Number(downvoteId),"likesLog.user":sess.user},{"$set":{"likesLog.$.val":-1}});
  699. guides.update({"guideId":Number(downvoteId)},{ "$set" : {"likes": newNum}});
  700. guides.update({"guideId":Number(downvoteId)}, {"$set":{"userDisliked":"true"}});
  701. guides.update({"guideId":Number(downvoteId)}, {"$set":{"userLiked":null}});
  702.  
  703. res.send(JSON.stringify({likes:newNum,newImg:"true"}));
  704. }
  705.  
  706. });
  707. }
  708. });
  709.  
  710.  
  711. app.get("/guide", function (req, res) {
  712. sess = req.session;
  713. if (sess.user) {
  714. app.locals.signedYesOrNo = sess.user;
  715. }
  716. else {
  717.  
  718. }
  719. var gId = req.query.id;
  720.  
  721. if (!gId) {
  722. console.log("Please put in paramater");
  723. res.redirect("back");
  724. }
  725. else {
  726. var guides = database.get("guides");
  727. guides.find({"guideId":Number(gId)}).on("success", function (doc) {
  728. if (doc == 0) {
  729. console.log(doc);
  730. res.redirect("back");
  731. }
  732. else {
  733.  
  734. var likesHome = null;
  735. var dislikesHome = null;
  736.  
  737. var obj = doc[0].likesLog.filter(function (obj) {
  738. return obj.user === sess.user;
  739. })[0];
  740.  
  741.  
  742. if (obj) {
  743. if (obj.val == 1) {
  744. likesHome = "true";
  745. }
  746. else if (obj.val == -1) {
  747. dislikesHome = "true";
  748. }
  749. }
  750. else {
  751. console.log("Wocka Flocka Flamage");
  752. }
  753.  
  754. res.render("guide", {guide:doc[0], likes:likesHome,dislikes:dislikesHome});
  755. }
  756. });
  757. }
  758. });
  759.  
  760.  
  761. //SignupProc route
  762. app.post("/signupProc", function (req, res) {
  763. sess = req.session;
  764. if (sess.user) {
  765. app.locals.signedYesOrNo = sess.user;
  766. }
  767. else {
  768.  
  769. }
  770. var username = req.body.username; //Username the client entered
  771. var password = req.body.password; //Password the client entered
  772.  
  773. if (username === "" || password === "") {
  774. res.redirect("/signup");
  775. }
  776. else {
  777. var users = database.get("users"); //Checks for the users table
  778. var account = database.get("account");
  779. users.find({"name":username}).on('success', function (doc) {
  780. if (doc == 0) {
  781. users.insert({"name":username,"password":password});
  782. account.insert({"name":username, "profilePic":"profile_default.png"});
  783. sess.user = username;
  784. res.redirect("/");
  785. }
  786. else {
  787.  
  788. sess.failLogin = "Failed Login";
  789. res.redirect("/signup");
  790. }
  791. });
  792. }
  793. });
  794.  
  795.  
  796.  
  797.  
  798. //Sign in route
  799. app.post("/signinProc", function (req, res) {
  800. sess = req.session;
  801. if (sess.user) {
  802. app.locals.signedYesOrNo = sess.user;
  803. }
  804. else {
  805.  
  806. }
  807. var username = req.body.username;
  808. var password = req.body.password;
  809.  
  810. var users = database.get("users");
  811. users.find({name:username}).on("success", function (doc) {
  812. if (doc == 0) {
  813. console.log("This user does not exist");
  814. }
  815. else {
  816. if (doc[0].password == password) {
  817. sess.user = username;
  818. res.redirect("/");
  819. }
  820. else {
  821. console.log("it doesn't exist");
  822. }
  823. }
  824. });
  825. });
  826.  
  827.  
  828.  
  829.  
  830.  
  831. //routes end here
  832.  
  833. //404 custom message
  834. app.use(function (req, res) {
  835. res.status(404);
  836. res.render("404", {layout:"error"});
  837. });
  838. //end of 404 message
  839.  
  840.  
  841. //500 custom message
  842. app.use(function (req, res) {
  843. res.status(500);
  844. res.render("500", {layout:"error"});
  845. });
  846. //end of 500 message
  847.  
  848. //log message for terminal
  849. app.listen(app.get("port"),function () {
  850. console.log("Application started");
  851. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement