{"size": 3359, "ext": "js", "max_stars_repo_path": "server/routes/users/users-dao.js", "max_stars_repo_name": "MishkaZi/vacations", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/routes/users/users-dao.js", "max_issues_repo_name": "MishkaZi/vacations", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/routes/users/users-dao.js", "max_forks_repo_name": "MishkaZi/vacations", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.0387596899, "max_line_length": 85, "alphanum_fraction": 0.6567430783}
const connection = require('../connection-wrapper');
let ServerError = require('../../middleware/errors/server-error');
let ErrorType = require('../../middleware/errors/error-type');

//Login
const login = async (userLoginDetails) => {
  let sql =
    'SELECT id, is_admin as isAdmin FROM users WHERE username=? AND password=?;';

  let parameters = [userLoginDetails.username, userLoginDetails.password];

  try {
    const userLoginResult = await connection.executeWithParameters(
      sql,
      parameters
    );

    //Check if username and password match ones in DB
    if (userLoginResult == null || userLoginResult.length == 0) {
      throw new ServerError(ErrorType.UNAUTHORIZED);
    }

    return userLoginResult[0];
  } catch (error) {
    throw new ServerError(ErrorType.UNAUTHORIZED);
  }
};

//Register
const register = async (userRegistrationDetails) => {
  let sql =
    'INSERT INTO users (username, password,first_name, last_name) VALUES (?,?,?,?);';

  let parameters = [
    userRegistrationDetails.username,
    userRegistrationDetails.password,
    userRegistrationDetails.firstName,
    userRegistrationDetails.lastName,
  ];

  try {
    const userRegistrationResult = await connection.executeWithParameters(
      sql,
      parameters
    );
    return userRegistrationResult.insertId;
  } catch (error) {
    throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
  }
};

//Helping function
const isUsernameExist = async (username) => {
  let sql = 'SELECT username FROM users WHERE username = ?;';
  let parameters = [username];

  try {
    const userExistResult = await connection.executeWithParameters(
      sql,
      parameters
    );

    if (userExistResult == null || userExistResult.length === 0) {
      return false;
    }

    return true;
  } catch (error) {
    throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
  }
};

//For future
// const getOneUser = async (id) => {
//   let sql = 'SELECT * FROM users WHERE users.id=?;';

//   try {
//     const userDetails = await connection.executeWithParameters(sql, id);
//     return userDetails;
//   } catch (error) {
//     throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
//   }
// };

// const getAllUsers = async () => {
//   let sql = 'SELECT * FROM users;';

//   try {
//     const usersDetails = await connection.executeWithParameters(sql);
//     return usersDetails;
//   } catch (error) {
//     throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
//   }
// };

// const update = async (userUpdateDetails, id) => {
//   let sql = 'UPDATE users SET username=?, password=? WHERE users.id=?;';

//   let parameters = [userUpdateDetails.username, userUpdateDetails.password, id];

//   try {
//     const result = await connection.executeWithParameters(sql, parameters);
//     return result;
//   } catch (error) {
//     throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
//   }
// };

// const deleteUser = async (id) => {
//   let sql = 'DELETE FROM users WHERE users.id=?;';

//   try {
//     const result = await connection.executeWithParameters(sql, id);
//     return result;
//   } catch (error) {
//     throw new ServerError(ErrorType.GENERAL_ERROR, sql, error);
//   }
// };

module.exports = {
  register,
  login,
  isUsernameExist,
  // getOneUser,
  // getAllUsers,
  // deleteUser,
  // update,
};
