{"size": 3768, "ext": "js", "max_stars_repo_path": "backend/src/api/resources/groups/groupsController.js", "max_stars_repo_name": "EBM2018/teamy", "max_stars_repo_stars_event_min_datetime": "2019-01-23T08:14:32.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-30T14:56:56.000Z", "max_issues_repo_path": "backend/src/api/resources/groups/groupsController.js", "max_issues_repo_name": "EBM2018/teamy", "max_issues_repo_issues_event_min_datetime": "2019-01-23T09:38:49.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-27T04:10:56.000Z", "max_forks_repo_path": "backend/src/api/resources/groups/groupsController.js", "max_forks_repo_name": "EBM2018/teamy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 27, "max_forks_count": null, "avg_line_length": 29.6692913386, "max_line_length": 89, "alphanum_fraction": 0.6220806794}
const GroupData = require('../../../services/groups/data');

const controller = {

  async getAll(req, res) {
    try {
      const result = await GroupData.getAll();
      return res.status(200).json(result);
    } catch (err) {
      return res.status(500).json(err);
    }
  },
  async getById(req, res) {
    if (!req.params.GroupId) {
      return res.status(404).send('Bad Request');
    }
    try {
      const result = await GroupData.getGroupsById(req.params.GroupId);
      return res.status(200).send(result);
    } catch (error) {
      return res.status(500).json(error);
    }
  },
  async createGroup(req, res) {
    if (!req.body) {
      return res.status(400).send('No body');
    }
    if (!req.body.groupName) {
      return res.status(400).send('missing or wrong fields');
    }
    try {
      const result = await GroupData.createGroup(req.body);
      return res.status(200).json(result);
    } catch (err) {
      return res.status(500).json(err);
    }
  },
  async createSeance(req, res) {
    if (!req.body || !req.params.GroupId) {
      return res.status(200).send('Bad Request');
    }
    const result = await GroupData.createSeance(req.params.GroupId, req.body);
    return res.status(200).json(result);
  },
  async getSeances(req, res) {
    if (req.params.GroupId) {
      const result = await GroupData.getSeancesById(req.params.GroupId);
      return res.status(200).send(result);
    }
    return res.status(404).send('Bad Request');
  },
  async getOneSeance(req, res) {
    if (!req.params.SeanceId || !req.params.GroupId) {
      return res.status(404).send('Bad Request');
    }
    const result = await GroupData.getOneSeanceById(
      req.params.GroupId,
      req.params.SeanceId,
    );
    return res.status(200).json(result);
  },
  async getRepartition(req, res) {
    if (!req.params.GroupId || !req.params.SeanceId) {
      return res.status(404).send('Bad Request');
    }
    const result = await GroupData.getRepartitionById(
      req.params.GroupId,
      req.params.SeanceId,
    );
    return res.status(200).json(result);
  },
  async getOneRepartition(req, res) {
    if (!req.params.GroupId || !req.params.SeanceId || !req.params.RepartitionId) {
      return res.status(404).send('Bad Request');
    }
    const result = await GroupData.getOneRepartitionById(
      req.params.GroupId,
      req.params.SeanceId,
      req.params.RepartitionId,
    );
    return res.status(200).json(result);
  },
  async updateGroup(req, res) {
    if (!req.body) {
      return res.status(404).send('Bad Request');
    }
    try {
      const result = await GroupData.updateGroup(req.params.GroupId, req.body);
      return res.status(200).json(result);
    } catch (e) {
      return res.status(500).json(e);
    }
  },
  async updateSeance(req, res) {
    if (!req.body || !req.params.GroupId || !req.params.SeanceId) {
      return res.status(404).send('Bad Request');
    }
    try {
      const result = await GroupData.updateSeance(
        req.params.GroupId,
        req.params.SeanceId,
        req.body,
      );
      return res.status(200).json(result);
    } catch (e) {
      return res.status(500).send(e);
    }
  },
  async deleteGroup(req, res) {
    if (req.params.GroupId) {
      const result = await GroupData.deleteGroup(req.params.GroupId);
      return res.status(200).json(result);
    }
    return res.status(404).send('Bad Request');
  },
  async deleteSeance(req, res) {
    if (!req.params.GroupId || !req.params.SeanceId) {
      return res.status(404).send('Bad Request');
    }
    const result = await GroupData.deleteSeance(req.params.GroupId, req.params.SeanceId);
    return res.status(200).send(result);
  },
};
// TODO: Add new Repartition & groups

module.exports = controller;
