{"size": 3222, "ext": "js", "max_stars_repo_path": "app/src/apis/root/root.ctrl.js", "max_stars_repo_name": "rrgks6221/wooahan-agile-task", "max_stars_repo_stars_event_min_datetime": "2021-03-01T02:34:05.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-02T09:59:23.000Z", "max_issues_repo_path": "app/src/apis/root/root.ctrl.js", "max_issues_repo_name": "rrgks6221/wooahan-agile-task", "max_issues_repo_issues_event_min_datetime": "2021-02-27T17:09:08.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-05T14:12:43.000Z", "max_forks_repo_path": "app/src/apis/root/root.ctrl.js", "max_forks_repo_name": "rrgks6221/wooahan-agile-task", "max_forks_repo_forks_event_min_datetime": "2021-02-28T05:31:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-17T08:37:50.000Z"}
{"max_stars_count": 2, "max_issues_count": 5, "max_forks_count": 17, "avg_line_length": 23.1798561151, "max_line_length": 82, "alphanum_fraction": 0.5363128492}
"use strict";

let db = require("../../config/db");
const crypto = require("crypto");

const todolist = {
  read: (req, res) => {
    const query = "SELECT * FROM todos;";

    db.query(query, (err, posts) => {
      if (err) throw err;
      return res.json(posts);
    });
  },

  create: (req, res) => {
    const client = req.body;
    const index = crypto.randomBytes(20).toString("hex").slice(0, 20); // token 생성

    const query = "INSERT INTO todos(id, name, description) VALUES (?, ?, ?);";

    db.query(query, [index, client.name, client.description], (err, result) => {
      if (err) throw err;
      return res.status(201).send(index);
    });
  },

  update: (req, res) => {
    const index = req.params.index;
    const client = req.body;

    const query = "UPDATE todos SET name=?, description=? WHERE id=?;";

    db.query(query, [client.name, client.description, index], (err, result) => {
      if (err) throw err;
      if (result.affectedRows) {
        return res.status(201).send(index);
      }
      return res.status(200).send("수정되지 않았습니다.");
    });
  },

  delete: (req, res) => {
    const index = req.params.index;

    const query = "DELETE FROM todos WHERE id=?;";

    db.query(query, [index], (err, result) => {
      if (err) throw err;
      if (result.affectedRows) {
        return res.status(201).send(index);
      }
      return res.status(200).send("삭제되지 않았습니다.");
    });
  },
};

const selectbox = {
  isAnswer: (req, res) => {
    const client = req.body;
    if (client.answer !== "개발") {
      return res.json(false);
    }
    return res.json(true);
  },
};

const checkbox = {
  isAnswer: (req, res) => {
    const client = req.body;
    const answers = ["열정", "기술력", "인성"];

    if (!client.answers || client.answers.length !== 3) {
      return res.send(false);
    }

    let cnt = 0;
    for (let clnt of client.answers) {
      if (answers.includes(clnt)) cnt++;
    }

    if (cnt === 3) {
      return res.json(true);
    }
    return res.json(false);
  },
};

const login = {
  login: (req, res) => {
    const client = req.body;
    if (client.id !== "test" || client.password !== "test") {
      return res
        .status(400)
        .json({ msg: "아이디와 패스워드를 제대로 입력하십시오" });
    }

    const query = "SELECT * FROM users WHERE id=? AND psword=?;";

    db.query(query, [client.id, client.password], (err, users) => {
      if (err) throw err;
      return res.status(200).json({ msg: "success", code: users[0].code });
    });
  },

  check: (req, res) => {
    const code = req.query.code;
    if (!code)
      return res.status(400).json({ msg: "정보확인에 실패하셨습니다." });

    const query = "SELECT * FROM users WHERE code=?;";

    db.query(query, [code], (err, users) => {
      if (err) throw err;
      const user = users[0];
      if (user) {
        const response = {
          msg: "success",
          user: {
            id: user.id,
            password: user.psword,
            name: user.name,
            code: user.code,
          },
        };
        return res.status(200).json(response);
      }
      return res.status(400).json({ msg: "정보확인에 실패하셨습니다." });
    });
  },
};

module.exports = {
  todolist,
  selectbox,
  checkbox,
  login,
};
