<commit_before>import { postgresConnection as cn } from '../../config/helpers.js';
import { QueryFile } from 'pg-promise';
const pgp = require('pg-promise')();
const db = pgp(cn);

function sql(file) {
  return new QueryFile(`${__dirname}/${file}`, { minify: true });
}


// get user follows count for any user
// UNUSED
// const getUserFollowCount = (request, response) => {
//   console.log(request.query);
//   // in query: .user
//   db.query(sql('getUserFollowCount.sql'), request.query)
//     .then(data => { response.json(data); })
//     .catch(data => { response.json(data); });
// };

// get recipe follows count for any recipe
// UNUSED
// const getRecipeFollowCount = (request, response) => {
//   console.log(request.query);
//   // in params: .recipe_id
//   db.query(sql('getRecipeFollowCount.sql'), request.params)
//     .then(data => { response.json(data); })
//     .catch(data => { response.json(data); });
// };

// get user follows count and user follow status for logged in user
// HTTP GET
export const getUserFollowState = (request, response) => {
  console.log(request.query);
  // in query: .follower, .target
  db.query(sql('getUserFollowState.sql'), request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// get recipe follows count and recipe follow status for logged in user
// HTTP GET
export const getRecipeFollowState = (request, response) => {
  console.log(request.query);
  // in query: .user_id, .recipe_id
  db.query(sql('getRecipeFollowState.sql'), request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// add new user follow for logged in user
// HTTP POST
const addUserFollow = (request, response) => {
  // TODO: does not verify that both users exist before
  //       inserting a new user follow
  console.log(request.body);
  // body: .follower, .target
  db.query(sql('addUserFollow.sql'), request.body)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// add new recipe follow for logged in user
// HTTP POST
const addRecipeFollow = (request, response) => {
  // TODO: does not verify that a user or a recipe exists before
  //       inserting a new recipe follow
  console.log(request.body);
  // body: .user_id, .recipe_id
  db.query(sql('addRecipeFollow.sql'), request.body)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// remove user follow for logged in user
const removeUserFollow = (request, response) => {
  console.log(request.query);
  // in query: .follower, .target
  db.query(sql('removeUserFollow.sql'), request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// remove recipe follow for logged in user
const removeRecipeFollow = (request, response) => {
  console.log(request.query);
  // in query: .user_id, .recipe_id
  db.query(sql('removeRecipeFollow.sql'), request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// decide whether to add or remove user follow, based on user intent
export const addOrRemoveUserFollow = (request, response) => {
  if (request.query.followState) {
    removeUserFollow(request, response);
  } else {
    addUserFollow(request, response);
  }
};

// decide whether to add or remove recipe follow, based on user intent
export const addOrRemoveRecipeFollow = (request, response) => {
  if (request.query.followState) {
    removeRecipeFollow(request, response);
  } else {
    addRecipeFollow(request, response);
  }
};

// get a list of every user the current user follows
// HTTP GET
export const getAllFollowedUsers = (request, response) => {
  // TODO: pagination
  console.log(request.query);
  // in query: .user_id
  db.query('getAllFollowedUsers.sql', request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.data(error); });
};

export const getAllFollowedRecipes = (request, response) => {
  console.log(request.query);
  // in query: .user_id
  db.query('getAllFollowedRecipes.sql', request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.data(error); });
};<commit_msg>Update follows controller: fix variable names, remove console logs<commit_after>import { postgresConnection as cn } from '../../config/helpers.js';
import { QueryFile } from 'pg-promise';
const pgp = require('pg-promise')();
const db = pgp(cn);

function sql(file) {
  return new QueryFile(`${__dirname}/${file}`, { minify: true });
}

// get user follows count and user follow status for logged in user
// HTTP GET
export const getUserFollowState = (request, response) => {
  console.log(request.query);
  // in query: .userID, .targetID
  // or in body: .userID, .targetID
  db.query(sql('getUserFollowState.sql'), request.query || request.body)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// get recipe follows count and recipe follow status for logged in user
// HTTP GET
export const getRecipeFollowState = (request, response) => {
  // in query: .userID, .targetID
  // or in body: .userID, .targetID
  const queryObj = Object.keys(request.query).length !== 0 ? request.query : request.body;
  console.log('query', request.query,' or body', request.body);
  console.log('queryobj', queryObj);

  db.query(sql('getRecipeFollowState.sql'), queryObj)
    .then(data => { response.json(data); })
    .catch(error => { response.json(error); });
};

// add new user follow for logged in user
// HTTP POST
const addUserFollow = (request, response) => {
  // TODO: does not verify that both users exist before
  //       inserting a new user follow
  console.log(request.body);
  // body: .userID, .targetID
  db.query(sql('addUserFollow.sql'), request.body)
    .then(data => { getUserFollowState(request, response); })
    .catch(error => { response.json(error); });
};

// add new recipe follow for logged in user
// HTTP POST
const addRecipeFollow = (request, response) => {
  // TODO: does not verify that a user or a recipe exists before
  //       inserting a new recipe follow
  // body: .user_id, .recipe_id
  db.query(sql('addRecipeFollow.sql'), request.body)
    .then(data => { getRecipeFollowState(request, response); })
    .catch(error => { response.json(error); });
};

// remove user follow for logged in user
const removeUserFollow = (request, response) => {
  console.log(request.query);
  // in query: .follower, .target
  // or in body: .userID, .targetID
  const queryObj = Object.keys(request.query).length !== 0 ? request.query : request.body;
  db.query(sql('removeUserFollow.sql'), queryObj)
    .then(data => { getUserFollowState(request, response); })
    .catch(error => { response.json(error); });
};

// remove recipe follow for logged in user
const removeRecipeFollow = (request, response) => {
  // in query: .user_id, .recipe_id
  // or in body: ., .targetID
  const queryObj = Object.keys(request.query).length !== 0 ? request.query : request.body;
  db.query(sql('removeRecipeFollow.sql'), queryObj)
    .then(data => { getRecipeFollowState(request, response); })
    .catch(error => { response.json(error); });
};

// decide whether to add or remove user follow, based on user intent
export const addOrRemoveUserFollow = (request, response) => {
  if (request.query.followState) {
    removeUserFollow(request, response);
  } else {
    addUserFollow(request, response);
  }
};

// decide whether to add or remove recipe follow, based on user intent
export const addOrRemoveRecipeFollow = (request, response) => {
  if (request.body.toggleFollow) {
    removeRecipeFollow(request, response);
  } else {
    addRecipeFollow(request, response);
  }
};

// get a list of every user the current user follows
// HTTP GET
export const getAllFollowedUsers = (request, response) => {
  // TODO: pagination
  // in query: .user_id
  db.query('getAllFollowedUsers.sql', request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.data(error); });
};

export const getAllFollowedRecipes = (request, response) => {
  // TODO: pagination
  // in query: .user_id
  db.query('getAllFollowedRecipes.sql', request.query)
    .then(data => { response.json(data); })
    .catch(error => { response.data(error); });
};
{'fraction_non_alphanumeric': 0.15894988066825777, 'fraction_numerical': 0.00035799522673031026, 'mean_word_length': 3.65352581898945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, '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': '6259181', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2581, 'n_words': 694}