{"size": 1795, "ext": "js", "max_stars_repo_path": "controllers/userController.js", "max_stars_repo_name": "Bastion-BaaS/admin", "max_stars_repo_stars_event_min_datetime": "2022-03-26T18:00:41.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T18:00:41.000Z", "max_issues_repo_path": "controllers/userController.js", "max_issues_repo_name": "Bastion-BaaS/admin", "max_issues_repo_issues_event_min_datetime": "2022-03-25T16:20:15.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T16:20:15.000Z", "max_forks_repo_path": "controllers/userController.js", "max_forks_repo_name": "Bastion-BaaS/admin-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 31.4912280702, "max_line_length": 58, "alphanum_fraction": 0.6718662953}
const axios = require('axios');
const HttpError = require('../models/httpError');
const { createURL } = require('../utils/helper');

const getUsers = (req, res, next) => {
  const stackName = req.params.stackName;

  const url = createURL(stackName, '/users');
  return axios.get(url, req.axiosConfig)
    .then(response => res.json(response.data))
    .catch(err => next(new HttpError(err, 500)));
};

const getUser = (req, res, next) => {
  const stackName = req.params.stackName;
  const id = req.params.id;

  const url = createURL(stackName, `/users/${id}`);
  return axios.get(url, req.axiosConfig)
    .then(response => res.json(response.data))
    .catch(err => next(new HttpError(err, 500)));
};

const createUser = (req, res, next) => {
  const stackName = req.params.stackName;

  const url = createURL(stackName, '/users');
  return axios.post(url, req.body, req.axiosConfig)
    .then(response => res.status(201).json(response.data))
    .catch(err => next(new HttpError(err, 500)));
};

const putUser = (req, res, next) => {
  const stackName = req.params.stackName;
  const id = req.params.id;

  const url = createURL(stackName, `/users/${id}`);
  return axios.put(url, req.body, req.axiosConfig)
    .then(response => res.status(201).json(response.data))
    .catch(err => next(new HttpError(err, 500)));
};

const deleteUser = (req, res, next) => {
  const stackName = req.params.stackName;
  const id = req.params.id;

  const url = createURL(stackName, `/users/${id}`);
  return axios.delete(url, req.axiosConfig)
    .then(response => res.status(204).json(response.data))
    .catch(err => next(new HttpError(err, 500)));
};

exports.getUsers = getUsers;
exports.getUser = getUser;
exports.createUser = createUser;
exports.putUser = putUser;
exports.deleteUser = deleteUser;