SHARE
TWEET

Untitled

a guest Dec 6th, 2016 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = angular.module('logARunApp', ['ngRoute','ngResource']).run(function($rootScope) {
  2.     $rootScope.authenticated = false;
  3.     $rootScope.user_currently_logged_in = '';
  4.     $rootScope.dateSelected = '';
  5.     $rootScope.dateFiltered = '';
  6.  
  7.     $rootScope.signout = function(){
  8.         $http.get('auth/signout');
  9.         $rootScope.authenticated = false;
  10.         $rootScope.user_currently_logged_in = '';
  11.     };
  12. });
  13.  
  14. app.config(function($routeProvider){
  15.     $routeProvider
  16.         //the timeline display
  17.         .when('/', {
  18.             templateUrl: 'main.html',
  19.             controller: 'mainController'
  20.         })
  21.         .when('/calendar',{
  22.             templateUrl: 'calendar.html',
  23.             controller: 'calController'
  24.         })
  25.         .when('/todaysRun',{
  26.             templateUrl: 'logForm.html',
  27.             controller: 'loggingController'
  28.         })
  29.         .when('/todaysRun/:dateSelected',{
  30.             templateUrl: 'logForm.html',
  31.             controller: 'loggingController'
  32.             //reloadOnSearch: true
  33.         })
  34.  
  35.         //the login display
  36.         .when('/login', {
  37.             templateUrl: 'login.html',
  38.             controller: 'authController'
  39.         })
  40.         //the signup display
  41.         .when('/register', {
  42.             templateUrl: 'register.html',
  43.             controller: 'authController'
  44.         });
  45.     ;
  46. });
  47.  
  48. app.factory('loggingService', function($resource){
  49.     return $resource('/api/todaysRun/:dateSelected');
  50. });
  51.  
  52. app.factory('calWeek', function($http){
  53.     var Week = function(initial_day){
  54.         this.sunday = initial_day.getSunday();
  55.     }
  56.  
  57.     Week.prototype.nextWeek = function(){
  58.         return new Week(this.sunday.deltaDays(7));
  59.     }
  60.  
  61.     Week.prototype.prevWeek = function () {
  62.         return new Week(this.sunday.deltaDays(-7));
  63.     };
  64.  
  65.     Week.prototype.contains = function (d) {
  66.         return (this.sunday.valueOf() === d.getSunday().valueOf());
  67.     };
  68.  
  69.     Week.prototype.getDates = function () {
  70.         var dates = [];
  71.         for(var i=0; i<7; i++){
  72.             dates.push(this.sunday.deltaDays(i));
  73.         }
  74.         return dates;
  75.     };
  76.  
  77.     return Week;
  78.  
  79. });
  80.  
  81. app.factory('calMonth', function($http, $q, calWeek){
  82.     var Month = function(year, month){
  83.         this.year = year;
  84.         this.month = month;
  85.     }
  86.  
  87.     Month.prototype.nextMonth = function(){
  88.         var self = this;
  89.         return new Month(self.year+Math.floor((self.month+1)/12), (self.month+1)%12);
  90.     }
  91.  
  92.     Month.prototype.prevMonth = function(){
  93.         var self = this
  94.         return new Month(this.year+Math.floor((this.month-1)/12),(this.month+11)%12);
  95.     }
  96.  
  97.     Month.prototype.getdateObject = function(d){
  98.         return new Date(this.year, this.month, d);
  99.     }
  100.  
  101.     Month.prototype.getWeeks = function(){
  102.         var firstDay = this.getDateObject(1);
  103.         var lastDay = this.nextMonth().getDateObject(0);
  104.  
  105.         var weeks = [];
  106.         var currweek = new Week(firstDay);
  107.         weeks.push(currweek);
  108.         while(!currweek.contains(lastDay)){
  109.             currweek = currweek.nextWeek();
  110.             weeks.push(currweek);
  111.         }
  112.  
  113.         return weeks;
  114.     }
  115.  
  116.     return Month;
  117.  
  118. });
  119.  
  120. app.controller('calController', function($scope, $rootScope, $location, calMonth, calWeek){
  121.  
  122.     Date.prototype.deltaDays = function(numDiff){
  123.         return new Date(this.getFullYear(), this.getMonth(), this.getDate()+numDiff);
  124.     }
  125.     Date.prototype.getSunday = function(){
  126.         return this.deltaDays(-1*this.getDay());
  127.     }
  128.  
  129.     $scope.date = new Date();
  130.     $scope.currentMonth = new Month($scope.date.getFullYear(), $scope.date.getMonth());
  131.     $scope.weeksArr = $scope.currentMonth.getWeeks();
  132.     //$scope.dateSelected = '';
  133.     $scope.updateCalendar = function(){
  134.  
  135.  
  136.     }
  137.  
  138.     $scope.nextMonthControl = function(){
  139.         $scope.currentMonth = $scope.currentMonth.nextMonth();
  140.         $scope.weeksArr = $scope.currentMonth.getWeeks();
  141.         //The following used for troubleshooting
  142.         //var curr = $scope.currentMonth;
  143.         //var monthNumber = curr.getDateObject(1).getMonth();
  144.         //alert(monthNumber);
  145.     }
  146.  
  147.     $scope.prevMonthControl = function(){
  148.         $scope.currentMonth = $scope.currentMonth.prevMonth();
  149.         $scope.weeksArr = $scope.currentMonth.getWeeks();
  150.         //The following used for troubleshooting
  151.         //var curr = $scope.currentMonth;
  152.         //var monthNumber = curr.getDateObject(1).getMonth();
  153.         //alert(monthNumber);
  154.     }
  155.  
  156.     $scope.createLog = function(day){
  157.         //alert(day);
  158.         $rootScope.dateSelected = day;
  159.  
  160.         //alert($scope.dateSelected);
  161.         $location.path('/todaysRun/' + $rootScope.dateSelected);
  162.  
  163.         //alert($scope.dateSelected);
  164.     }  
  165.  
  166. });
  167.  
  168. app.controller('loggingController', function(loggingService, $scope, $rootScope, $routeParams, $location){
  169.     $scope.dateSelectedAgain = $routeParams.dateSelected;
  170.     $scope.logs = loggingService.query();
  171.     $scope.newLog = {username: '', mileage: '', timeMinutes: '', pace: '', description: '', comments: '', date: ''};
  172.  
  173.     $scope.insertNewLog = function() {
  174.       $scope.newLog.username = $rootScope.user_currently_logged_in;
  175.       $scope.newLog.date = $rootScope.dateSelected;
  176. //    $scope.newLog.date = $rootScope.dateFiltered;
  177.  
  178. //    $scope.filterDate = function(date){
  179. //      return (string)this.date ===(string) dateFiltered;
  180. //    }
  181.  
  182.       //$scope.newLog.created_at = Date.now();
  183.       loggingService.save($scope.newLog, function(){
  184.         $scope.logs = loggingService.query();
  185.         $scope.newLog = {username: '', mileage: '', timeMinutes: '', pace: '', description: '', comments: '', date: ''};
  186.       });
  187.       alert('Success');
  188.       //$location.path('/calendar');
  189.     };
  190.  
  191.  
  192.  
  193.  
  194. });
  195.  
  196.  
  197. app.controller('authController', function($scope, $http, $rootScope, $location){
  198.   $scope.user = {username: '', password: ''};
  199.   $scope.error_message = '';
  200.  
  201.   $scope.login = function(){
  202.     $http.post('/auth/login', $scope.user).success(function(data){
  203.       if(data.state == 'success'){
  204.         $rootScope.authenticated = true;
  205.         $rootScope.user_currently_logged_in = data.user.username;
  206.         $location.path('/');
  207.       }
  208.       else{
  209.         $scope.error_message = data.message;
  210.       }
  211.     });
  212.   };
  213.  
  214.   $scope.register = function(){
  215.     $http.post('/auth/signup', $scope.user).success(function(data){
  216.       if(data.state == 'success'){
  217.         $rootScope.authenticated = true;
  218.         $rootScope.user_currently_logged_in = data.user.username;
  219.         $location.path('/');
  220.       }
  221.       else{
  222.         $scope.error_message = data.message;
  223.       }
  224.     });
  225.   };
  226. });
  227.    
  228. var express = require('express');
  229. var router = express.Router();
  230. var mongoose = require( 'mongoose' );
  231. //var Post = mongoose.model('Post');
  232. var DailyLogModel = mongoose.model('dailyLogModel');
  233. //Used for routes that must be authenticated.
  234. function isAuthenticated (req, res, next) {
  235.     // if user is authenticated in the session, call the next() to call the next request handler
  236.     // Passport adds this method to request object. A middleware is allowed to add properties to
  237.     // request and response objects
  238.  
  239.     //allow all get request methods
  240.     if(req.method === "GET"){
  241.         return next();
  242.     }
  243.     if (req.isAuthenticated()){
  244.         return next();
  245.     }
  246.  
  247.     // if the user is not authenticated then redirect him to the login page
  248.     return res.redirect('/#login');
  249. };
  250.  
  251. //Register the authentication middleware
  252. router.use('/todaysRun', isAuthenticated);
  253. router.use('/todaysRun/:dateSelected', isAuthenticated);
  254.  
  255. router.route('/todaysRun')
  256.     //To create a new run
  257.     .post(function(req, res){
  258.         console.log('Is it here???');
  259.         //NOT SURE WHERE THE .body comes from???????
  260.         var log = new DailyLogModel();
  261.         log.username = req.body.username;
  262.         log.mileage = req.body.mileage;
  263.         log.timeMinutes = req.body.timeMinutes;
  264.         log.description = req.body.description;
  265.         log.date = req.body.date;
  266.         console.log(req.body.date);
  267.         //Saves to the mongoDB database, if error sends 500 error        
  268.         log.save(function(err, log) {
  269.             if (err){
  270.                 return res.send(500, err);
  271.             }
  272.             console.log('No error');
  273.             return res.json(log);
  274.         });
  275.     })
  276.  
  277.     //To get all runs
  278.     .get(function(req, res){
  279.         console.log('debug1');
  280.         DailyLogModel.find(function(err, logs){
  281.             console.log('debug2');
  282.             if(err){
  283.                 return res.send(500, err);
  284.             }
  285.             return res.send(200,logs);
  286.         });
  287.     });
  288.  
  289. //run-specific commands
  290. router.route('/todaysRun/:dateSelected')
  291.  
  292.     //gets specified run
  293.     .get(function(req, res){
  294.         console.log('WE ARE ACTUALLY HERE THIS TIME');
  295.         //log.findOne({ 'date' :  date }
  296.         DailyLogModel.findById(req.params.id, function(err, log){
  297.             if(err)
  298.                 res.send(err);
  299.             res.json(log);
  300.         });
  301.     })
  302.     //updates specified run
  303.     .put(function(req, res){
  304.         DailyLogModel.findById(req.params.id, function(err, log){
  305.             if(err)
  306.                 res.send(err);
  307.  
  308.         log.username = req.body.username;
  309.         log.mileage = req.body.mileage;
  310.         log.timeMinutes = req.body.timeMinutes;
  311.         log.description = req.body.description;
  312.         log.date = req.body.date;
  313.  
  314.             log.save(function(err, log){
  315.                 if(err)
  316.                     res.send(err);
  317.                 res.json(log);
  318.             });
  319.         });
  320.     })
  321.     //deletes the run
  322.     .delete(function(req, res) {
  323.         DailyLogModel.remove({
  324.             _id: req.params.id
  325.         }, function(err) {
  326.             if (err)
  327.                 res.send(err);
  328.             res.json("deleted run from " + log.date);
  329.         });
  330.     });
  331.  
  332. module.exports = router;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top