{"size": 983, "ext": "js", "max_stars_repo_path": "backend/src/controllers/dev.controller.js", "max_stars_repo_name": "leognmotta/devtinder", "max_stars_repo_stars_event_min_datetime": "2019-09-23T12:46:49.000Z", "max_stars_repo_stars_event_max_datetime": "2019-09-23T12:46:49.000Z", "max_issues_repo_path": "backend/src/controllers/dev.controller.js", "max_issues_repo_name": "leomotta121/devtinder", "max_issues_repo_issues_event_min_datetime": "2020-09-06T20:03:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T16:17:27.000Z", "max_forks_repo_path": "backend/src/controllers/dev.controller.js", "max_forks_repo_name": "leognmotta/devtinder", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 21.8444444444, "max_line_length": 81, "alphanum_fraction": 0.5625635809}
const { default: axios } = require('axios');
const Dev = require('../models/Dev');

exports.index = async (req, res, next) => {
  try {
    const { user } = req.headers;

    const loggedDev = await Dev.findById(user);

    const devs = await Dev.find({
      $and: [
        { _id: { $ne: user } },
        { _id: { $nin: loggedDev.likes } },
        { _id: { $nin: loggedDev.dislikes } }
      ]
    });

    return res.json(devs);
  } catch (error) {
    next(error);
  }
};

exports.store = async (req, res, next) => {
  try {
    const { username } = req.body;

    const userExists = await Dev.findOne({ user: username });

    if (userExists) {
      return res.json(userExists);
    }

    const response = await axios.get(`https://api.github.com/users/${username}`);

    const { name, bio, avatar_url: avatar } = response.data;

    const dev = await Dev.create({ name, user: username, bio, avatar });

    return res.json(dev);
  } catch (error) {
    next(error);
  }
};
