{"size": 3497, "ext": "js", "max_stars_repo_path": "public/scripts/apiRequests.js", "max_stars_repo_name": "brycedugger/budget", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "public/scripts/apiRequests.js", "max_issues_repo_name": "brycedugger/budget", "max_issues_repo_issues_event_min_datetime": "2021-05-10T22:51:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-10T22:51:28.000Z", "max_forks_repo_path": "public/scripts/apiRequests.js", "max_forks_repo_name": "brycedugger/budget", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 28.4308943089, "max_line_length": 91, "alphanum_fraction": 0.6494137832}
/**
 * function update a category
 * @param {number} id the id of the category
 * @param {string} name the description of the category
 * @param {number} goal the amount of the category
 */
const updateCategory = (id, name, goal) => {
  // make put request to update a single category
  axios.put(`/api/category/`, { id, name, goal }).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to delete a single category
 * @param {number} categoryId the id of the expense to be deleted
 */
const deleteCategory = categoryId => {
  // send delete request to delete a single expense
  axios.delete(`/api/category/${categoryId}`).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to create a new category
 * @param {number} userId the id of the user
 * @param {string} name the name of the category
 * @param {string} goal the goal of the category
 */
const postCategory = (userId, name, goal) => {
  // send post request to create a single category
  axios.post(`/api/category/${userId}`, { name, goal }).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to delete a single expense
 * @param {number} expenseId the id of the expense to be deleted
 */
const deleteExpense = expenseId => {
  // send delete request to delete a single expense
  axios.delete(`/api/expense/${expenseId}`).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to get all categories and append a dropdown the the parent element
 * @param {number} userId the id of the user
 * @param {string} parentElement the element to append this to
 * @param {string} defaultValue the default value for the dropdown
 */
const getCategories = (userId, parentElement, defaultValue) => {
  // send get request to retrieve all categories
  axios.get(`/api/category/${userId}`).then(res => {
    // render dropdown button
    const dropdown = renderDropdown("categories");

    // for each category, create a dropdown option
    res.data.forEach(row => {
      dropdown.append(renderDropdownCategories(row.name, row.id));
    });

    // set defaults for the value if one is defined
    if (defaultValue !== undefined) {
      dropdown.val(defaultValue);
    }

    // append it to the modal
    $(parentElement).append(dropdown);
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to update the expense by sending a put request
 * @param {number} expenseId the id of the expense
 * @param {string} description the description of the expense
 * @param {number} amount the amount of the expense
 * @param {number} CategoryId the id of the category
 */
const updateExpense = (expenseId, description, amount, CategoryId) => {
  // make put request to update a single expense
  axios.put(`/api/expense/${expenseId}`, { description, amount, CategoryId }).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};

/**
 * function to create a new expense
 * @param {string} amount the expense amount
 * @param {string} description the expense description
 * @param {number} CategoryId the id of the category
 */
const postExpense = (amount, description, CategoryId) => {
  // send post request to create a single expense
  axios.post(`/api/expense/`, { amount, description, CategoryId }).then(res => {
    location.reload();
  }),
    err => {
      console.log(err);
    };
};
