{"size": 2284, "ext": "js", "max_stars_repo_path": "nodejs/cards/src/controllers/card-controller.js", "max_stars_repo_name": "JGuilhermeCoelho/Hackaton-orbitall", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "nodejs/cards/src/controllers/card-controller.js", "max_issues_repo_name": "JGuilhermeCoelho/Hackaton-orbitall", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "nodejs/cards/src/controllers/card-controller.js", "max_forks_repo_name": "JGuilhermeCoelho/Hackaton-orbitall", "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": 25.9545454545, "max_line_length": 67, "alphanum_fraction": 0.5958844133}
const { request, response } = require("express");
const neDB = require("../configurations/database");
const api = {};

api.findAll = (request, response) => {
  neDB.find({}).exec((exception, cards) => {
    if (exception) {
      const setence = "It was not possible to list the cards!";
      console.log(setence, exception);
      response.status(exception.status | 400);
      response.json({ mensagem: setence });
    }
    response.status(201);
    response.json(cards);
  });
};

api.save = (request, response) => {
  const canonical = request.body;
  neDB.insert(canonical, (exception, card) => {
    if (exception) {
      const setence = "The card could not be saved!";
      console.log(setence, exception);
      response.status(exception.status | 400);
      response.json({ mensagem: setence });
    }
    response.status(201);
    response.json(card);
  });
};

api.find = (request, response) => {
  neDB
    .findOne({
      _id: parseInt(request.params.id),
    })
    .exec((exception, card) => {
      if (exception) {
        const setence = "It was not possible to list the card!";
        console.log(setence, exception);
        response.status(exception.status | 400);
        response.json({ mensagem: setence });
      }
      response.status(201);
      response.json(card);
    });
};

api.remove = (request, response) => {
  neDB.remove(
    { _id: parseInt(request.params.id) },
    {},
    (exception, cardRemoved) => {
      if (exception) {
        const setence = "It was not possible to remove the card!";
        console.log(setence, exception);
        response.status(exception.status | 400);
        response.json({ mensagem: setence });
      }
      response.status(201);
      response.json(cardRemoved);
    }
  );
};

api.update = (request, response) => {
  const canonical = request.body;

  neDB.update(
    { _id: parseInt(request.params.id) },
    canonical,
    {},
    (exception, cardUpdated) => {
      if (exception) {
        const sentence = "It was not possible to update the card!";
        console.log(sentence, exception);

        response.status(exception.status | 400);
        response.json({ message: sentence });
      }
      response.status(201);
      response.json(cardUpdated);
    }
  );
};

module.exports = api;
