<commit_before>'use strict';
var bodyParser = require('body-parser')

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

const pool = require("../psql/db_setup.js");
const q = require("../psql/queries"); //import queries

var visit = {};

function _getCurrentDate(){
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1; //January is 0!
  var yyyy = today.getFullYear();

  if(dd<10) {
      dd = '0'+dd
  } 

  if(mm<10) {
      mm = '0'+mm
  } 

  today = yyyy + '-' + mm + '-' + dd;

  return today;
}

visit.getForm = function(req, res){
  let place = "";
  
  if(req.query.place != null){
    console.log(req.query.place);
    place = req.query.place;
  }
  if(req.query.date != null){
    console.log(req.query.date);
    let date = req.query.date;
  }  
  
  function _render(err, result){
    if(err) {
      console.error(err);
      res.send("ERROR" + err);
    } else {
      let currentDate = _getCurrentDate();
      let placeList = result.rows;
      let isVis = "not-visible";
      let errMsg = "";

      console.log(currentDate);

      res.render("add-visit", {
       placeList: placeList,
       place:place,
       currentDate:currentDate,
       isVis:isVis,
       errMsg:errMsg
     });
    }
  }
  console.log(q.getNames);
  pool.query(q.getNames, _render);
}

// This function works but is not robust.
// Need to figure out how to handle username
visit.post = function(req, res) {

  console.log("body", req.body);

  let name = req.body.name;
  let place = req.body.place;
  let date = req.body.date;
  const query = {
    text: q.insertVisit, //VALUES example: (travis, <NAME>, 2011-2-3 )
    values: [name, place, date]
  }
  pool.query(query)
    .then(req => {
      let successMsg = name + ", thanks for adding your visit to " + place +
      " on " + date + "!";
      console.log(successMsg);
      res.status(200).send({msg: successMsg});
    })
    .catch(error =>  {

      let isVis = "visible";
      console.error(error.code)
      let errMsg = "Something went wrong, sorry.";
      if(error.code === "23505") {

        errMsg = "Sorry, " + name + ". But you've already visited "
        + place + " on " + date + ".";

        console.log("Duplicate Error");
        res.status(400).send({msg: errMsg});
      }
    })
}

module.exports = visit;

<commit_msg>Add other errors<commit_after>'use strict';
var bodyParser = require('body-parser')

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

const pool = require("../psql/db_setup.js");
const q = require("../psql/queries"); //import queries

var visit = {};

function _getCurrentDate(){
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1; //January is 0!
  var yyyy = today.getFullYear();

  if(dd<10) {
      dd = '0'+dd
  } 

  if(mm<10) {
      mm = '0'+mm
  } 

  today = yyyy + '-' + mm + '-' + dd;

  return today;
}

visit.getForm = function(req, res){
  let place = "";
  
  if(req.query.place != null){
    console.log(req.query.place);
    place = req.query.place;
  }
  if(req.query.date != null){
    console.log(req.query.date);
    let date = req.query.date;
  }  
  
  function _render(err, result){
    if(err) {
      console.error(err);
      res.send("ERROR" + err);
    } else {
      let currentDate = _getCurrentDate();
      let placeList = result.rows;
      let isVis = "not-visible";
      let errMsg = "";

      console.log(currentDate);

      res.render("add-visit", {
       placeList: placeList,
       place:place,
       currentDate:currentDate,
       isVis:isVis,
       errMsg:errMsg
     });
    }
  }
  console.log(q.getNames);
  pool.query(q.getNames, _render);
}

// This function works but is not robust.
// Need to figure out how to handle username
visit.post = function(req, res) {

  console.log("body", req.body);

  let name = req.body.name;
  let place = req.body.place;
  let date = req.body.date;
  const query = {
    text: q.insertVisit, //VALUES example: (travis, <NAME>, 2011-2-3 )
    values: [name, place, date]
  }
  pool.query(query)
    .then(req => {
      let successMsg = name + ", thanks for adding your visit to " + place +
      " on " + date + "!";
      console.log(successMsg);
      res.status(200).send({msg: successMsg});
    })
    .catch(error =>  {

      let isVis = "visible";
      console.error(error.code)
      let errMsg = "Something went wrong, sorry.";
      //Duplicate Visit
      if(error.code === "23505") {

        errMsg = "Sorry, " + name + ". But you've already visited "
        + place + " on " + date + ".";

        console.log("Duplicate Error");
        res.status(400).send({msg: errMsg});
      }
      //Name does not exist
      else if (error.code === "23502") {
        errMsg = "Sorry, but, " + name + " is not a current user" +
        " please check that your name is entered correctly.";

        console.log("Name Error");
        res.status(400).send({msg: errMsg});
      } 
      //Place does not exist
      else if (error.code === "23505" ) {
        errMsg = "Sorry, " + name + " but " + place +
        " does not exist. Please make sure that place is entered correctly.";
         res.status(400).send({msg: errMsg});
      }
      //All other errors
      else {
        res.status(400).send({msg: errMsg});
      }
    })
}

module.exports = visit;


{'fraction_non_alphanumeric': 0.15935046395431834, 'fraction_numerical': 0.012312633832976445, 'mean_word_length': 2.405224787363305, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6114866', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1853, 'n_words': 451}