<commit_before>var express = require('express');
var router = express.Router();
var models = require('../models');


//function to convert row from db into geoJSON feature
function toGeoJSON(project, features) {
  var feature = {
    type: "Feature",
    geometry: project.Geometry,
    properties: project
  }
  features.push(feature)
}

//Renders the new project page where logged in users can add projects
//If user is not logged in they are redirected to the home page
router.get('/new', function(req, res) {
    if (req.session.logged_in){
        res.render('projects/new-project', {
            logged_in: req.session.logged_in,
            adminclearance: req.session.adminclearance,
            id: req.session.user_id,
            email: req.session.email,
            firstname: req.session.firstname,
            lastname: req.session.lastname,
            phonenumber: req.session.phonenumber,
            admin: req.session.admin
        });
    }
    else {
        res.redirect("/");
    }
});

//Returns ALL projects from the DB
router.get('/all', function(req, res) {
    //Create an empty geoJSON feature collections
    //The features array will be populated when toGeoJSON is run
    var featureCollection = {
        "type": "FeatureCollection",
        features: []
    };
    //If the user is logged in return all projects
    if (req.session.logged_in) {
      models.Project.findAll().then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
      }).then(function() {
          res.send(featureCollection);
      });
      //If the user is not logged in return all projects where Access is public
    } else {
      models.Project.findAll({
        where: {
          Access: 'Public'
        }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
      }).then(function() {
          res.send(featureCollection);
      });
    }
});

//Endpoint to get projects by specific funding and project types
router.get('/funding/:status/type/:type', function(req, res) {
  //Create an empty geoJSON feature collections
  //The features array will be populated when toGeoJSON is run
    var featureCollection = {
        "type": "FeatureCollection",
        features: []
    };
    //get the funding statuses requested
    var status = req.params.status;
    //get the project types requested
    var type = req.params.type;
    //string manipulations to separate funding and project types into arrays
    status = status.split('&');
    type = type.split('&');
    //create the search array that sequelize while use as the $or parameter
    var searchArr = [];

    //If the user is logged in
    if (req.session.logged_in) {
      for (var i = 0; i < status.length; i++) {
          for (var j = 0; j < type.length; j++) {
              var searchObj = {
                  Fund_St: {
                      ilike: status[i]
                  },
                  Proj_Ty: {
                      ilike: type[j]
                  }
              };
              searchArr.push(searchObj);
          }
      }
      models.Project.findAll({
          where: {
              $or: searchArr
          }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
          res.send(featureCollection);
      });

      //If the user is not logged in only return publicy accessible projects
    } else {
      for (var i = 0; i < status.length; i++) {
          for (var j = 0; j < type.length; j++) {
              var searchObj = {
                  Fund_St: {
                      ilike: status[i]
                  },
                  Proj_Ty: {
                      ilike: type[j]
                  },
                  Access: 'Public'
              };
              searchArr.push(searchObj);
          }
      }
      models.Project.findAll({
          where: {
              $or: searchArr
          }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
          res.send(featureCollection);
      });
    }
});

//Saves a new project to the DB only if user is logged in
router.post('/new', function(req, res) {
    console.log('Route Hit');
    //If the user is logged in
    if (req.session.logged_in) {
      console.log('User is logged in.');
      var newProject = req.body;

      //Parse the projects geometry which was stringified on the front end
      var geometry = JSON.parse(newProject.Geometry);
      var coordinates = JSON.parse(geometry.coordinates);
      var parsedGeometry = {
          type: geometry.type,
          coordinates: coordinates
      }
      newProject.Geometry = parsedGeometry;
      console.log('Geometry has been converted.');
      //Parse the contact info
      var contactInfo = JSON.parse(newProject.Contact_info);
      newProject.Contact_info = contactInfo;
      console.log('Working up to cross streets!');
      console.log(newProject);
      //Check if the project has cross streets
      if (newProject.hasOwnProperty('Cross_Streets')) {
        console.log('Inside cross streets.');
        //parse the cross streets
        var crossStreets = JSON.parse(newProject.Cross_Streets);
        newProject.Cross_Streets = crossStreets;
      }
      console.log('Past the cross streets section');
      //If newProject has the propery flagged then the user has already chosen to flag it true or false
      if (newProject.hasOwnProperty('Flagged')) {
        console.log('Inside has own propery flagged');
        models.Project.create(newProject).then(function() {
            res.send({"status": "saved"});<commit_msg>testing<commit_after>var express = require('express');
var router = express.Router();
var models = require('../models');


//function to convert row from db into geoJSON feature
function toGeoJSON(project, features) {
  var feature = {
    type: "Feature",
    geometry: project.Geometry,
    properties: project
  }
  features.push(feature)
}

//Renders the new project page where logged in users can add projects
//If user is not logged in they are redirected to the home page
router.get('/new', function(req, res) {
    if (req.session.logged_in){
        res.render('projects/new-project', {
            logged_in: req.session.logged_in,
            adminclearance: req.session.adminclearance,
            id: req.session.user_id,
            email: req.session.email,
            firstname: req.session.firstname,
            lastname: req.session.lastname,
            phonenumber: req.session.phonenumber,
            admin: req.session.admin
        });
    }
    else {
        res.redirect("/");
    }
});

//Returns ALL projects from the DB
router.get('/all', function(req, res) {
    //Create an empty geoJSON feature collections
    //The features array will be populated when toGeoJSON is run
    var featureCollection = {
        "type": "FeatureCollection",
        features: []
    };
    //If the user is logged in return all projects
    if (req.session.logged_in) {
      models.Project.findAll().then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
      }).then(function() {
          res.send(featureCollection);
      });
      //If the user is not logged in return all projects where Access is public
    } else {
      models.Project.findAll({
        where: {
          Access: 'Public'
        }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
      }).then(function() {
          res.send(featureCollection);
      });
    }
});

//Endpoint to get projects by specific funding and project types
router.get('/funding/:status/type/:type', function(req, res) {
  //Create an empty geoJSON feature collections
  //The features array will be populated when toGeoJSON is run
    var featureCollection = {
        "type": "FeatureCollection",
        features: []
    };
    //get the funding statuses requested
    var status = req.params.status;
    //get the project types requested
    var type = req.params.type;
    //string manipulations to separate funding and project types into arrays
    status = status.split('&');
    type = type.split('&');
    //create the search array that sequelize while use as the $or parameter
    var searchArr = [];

    //If the user is logged in
    if (req.session.logged_in) {
      for (var i = 0; i < status.length; i++) {
          for (var j = 0; j < type.length; j++) {
              var searchObj = {
                  Fund_St: {
                      ilike: status[i]
                  },
                  Proj_Ty: {
                      ilike: type[j]
                  }
              };
              searchArr.push(searchObj);
          }
      }
      models.Project.findAll({
          where: {
              $or: searchArr
          }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
          res.send(featureCollection);
      });

      //If the user is not logged in only return publicy accessible projects
    } else {
      for (var i = 0; i < status.length; i++) {
          for (var j = 0; j < type.length; j++) {
              var searchObj = {
                  Fund_St: {
                      ilike: status[i]
                  },
                  Proj_Ty: {
                      ilike: type[j]
                  },
                  Access: 'Public'
              };
              searchArr.push(searchObj);
          }
      }
      models.Project.findAll({
          where: {
              $or: searchArr
          }
      }).then(function(projects) {
          for (var i = 0; i < projects.length; i++) {
              toGeoJSON(projects[i], featureCollection.features);
          }
          res.send(featureCollection);
      });
    }
});

//Saves a new project to the DB only if user is logged in
router.post('/new', function(req, res) {
    console.log('Route Hit');
    //If the user is logged in
    if (req.session.logged_in) {
      console.log('User is logged in.');
      var newProject = req.body;

      //Parse the projects geometry which was stringified on the front end
      var geometry = JSON.parse(newProject.Geometry);
      var coordinates = JSON.parse(geometry.coordinates);
      var parsedGeometry = {
          type: geometry.type,
          coordinates: coordinates
      }
      newProject.Geometry = parsedGeometry;
      console.log('Geometry has been converted.');
      //Parse the contact info
      var contactInfo = JSON.parse(newProject.Contact_info);
      newProject.Contact_info = contactInfo;
      console.log('Working up to cross streets!');
      console.log(newProject);
      console.log('Has own property: ' + newProject.hasOwnProperty('Flagged'));
      //Check if the project has cross streets
      if (newProject.hasOwnProperty('Cross_Streets')) {
        console.log('Has cross streets');
        console.log('Inside cross streets.');
        //parse the cross streets
        var crossStreets = JSON.parse(newProject.Cross_Streets);
        newProject.Cross_Streets = crossStreets;
      }
      console.log('Past the cross streets section');
      //If newProject has the propery flagged then the user has already chosen to flag it true or false
      if (newProject.hasOwnProperty('Flagged')) {
        console.log('Inside has own propery flagged');
        models.Project.create(newProject).then(function() {
            res.send({"status": "saved"});
{'fraction_non_alphanumeric': 0.11329103356152948, 'fraction_numerical': 0.0013357822674903992, 'mean_word_length': 1.9555884529977794, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6940268', 'n_tokens_mistral': 3280, 'n_tokens_neox': 3224, 'n_words': 948}