{"size": 3068, "ext": "js", "max_stars_repo_path": "server/src/service/AvariasControllerService.js", "max_stars_repo_name": "gestao-avarias/dispatching-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/src/service/AvariasControllerService.js", "max_issues_repo_name": "gestao-avarias/dispatching-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/src/service/AvariasControllerService.js", "max_forks_repo_name": "gestao-avarias/dispatching-api", "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": 23.4198473282, "max_line_length": 206, "alphanum_fraction": 0.5658409387}
'use strict';
var sql = require('../utils/db.js');

/**
 * Create Avaria
 *
 * body Avaria  (optional)
 * returns Avaria
 **/
exports.createAvaria = function (body) {
  console.log(body);

  return new Promise(function (resolve, reject) {
    sql.query(
      'INSERT INTO avaria (nome, id_utilizador, id_antena, status, data_abertura, detalhe) Values(?, ?, ?, ?, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()), ?)',
      [body.nome, body.id_utilizador, body.id_antena, 1, body.detalhe],
      function (err, res) {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log(res.insertId);
          resolve(res.insertId);
        }
      },
    );
  });
};

/**
 * Delete Avaria
 *
 * id Long
 * no response value expected for this operation
 **/
exports.deleteAvaria = function (id) {
  return new Promise(function (resolve, reject) {
    sql.query('DELETE FROM avaria WHERE id = ?', [id], function (err, res) {
      if (err || !res.affectedRows) {
        console.log(err);
        console.log(res);
        reject();
      } else {
        console.log(res);
        resolve({ deleted: id });
      }
    });
  });
};

/**
 * Retrieve Avaria
 *
 * id Long
 * returns Avaria
 **/
exports.retrieveAvaria = function (id) {
  return new Promise(function (resolve, reject) {
    sql.query(
      'SELECT avaria.*, utilizador.nome as nome_utilizador, antena.nome as nome_antena FROM avaria join utilizador on id_utilizador = utilizador.id join antena on id_antena = antena.id WHERE avaria.id = ?',
      [id],
      function (err, res) {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log(res);
          resolve(res[0]);
        }
      },
    );
  });
};

/**
 * Retrieve Avarias
 *
 * returns List
 **/
exports.retrieveAvarias = function () {
  return new Promise(function (resolve, reject) {
    const callback = function (err, res) {
      if (err) {
        console.log(err);
        reject(err);
      } else {
        console.log(res);
        resolve(res);
      }
    };

    let queryString =
      'SELECT avaria.*, utilizador.nome as nome_utilizador, antena.nome as nome_antena FROM avaria join utilizador on avaria.id_utilizador = utilizador.id join antena on avaria.id_antena =  antena.id ';

    sql.query(queryString, callback);
  });
};

/**
 * Update Avaria
 *
 * body Avaria
 * id Long
 * no response value expected for this operation
 **/
exports.updateAvaria = function (body, id) {
  return new Promise(function (resolve, reject) {
    sql.query(
      'UPDATE avaria set nome = ?, id_utilizador = ?, id_antena = ?, status = ?, data_conclusao = ?, detalhe = ?  WHERE id = ?',
      [
        body.nome,
        body.id_utilizador,
        body.id_antena,
        body.status,
        body.data_conclusao,
        body.detalhe,
        id,
      ],
      function (err, res) {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log(res);
          resolve(id);
        }
      },
    );
  });
};
