{"size": 3032, "ext": "js", "max_stars_repo_path": "gem-server/api/server/controllers/CategoryController.js", "max_stars_repo_name": "Vaselek/gems-shop", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "gem-server/api/server/controllers/CategoryController.js", "max_issues_repo_name": "Vaselek/gems-shop", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "gem-server/api/server/controllers/CategoryController.js", "max_forks_repo_name": "Vaselek/gems-shop", "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": 28.3364485981, "max_line_length": 91, "alphanum_fraction": 0.625}
import CategoryService from '../services/CategoryService';
import Util from '../utils/Utils';
import {improveMessage} from "./controllerUtils";

const util = new Util();

class CategoryController {
  static async getAllCategories(req, res) {
    try {
      const sortField = req.query.sortField;
      const sortOrder = req.query.sortOrder;
      const allCategories = await CategoryService.getAllCategories({sortField, sortOrder});
      if (allCategories.length > 0) {
        util.setSuccess(200, 'Categories retrieved', allCategories);
      } else {
        util.setSuccess(200, 'No category found');
      }
      return util.send(res);
    } catch (error) {
      util.setError(400, error);
      return util.send(res);
    }
  }

  static async addCategory(req, res) {
    const newCategory = req.body;
    try {
      const createdCategory = await CategoryService.addCategory(newCategory);
      util.setSuccess(201, 'Category Added!', createdCategory);
      return util.send(res);
    } catch (error) {
      const message = improveMessage(error.message)
      util.setError(400, message);
      return util.send(res);
    }
  }

  static async updatedCategory(req, res) {
    const alteredCategory = req.body;
    const { id } = req.params;
    if (!Number(id)) {
      util.setError(400, 'Please input a valid numeric value');
      return util.send(res);
    }
    try {
      const updateCategory = await CategoryService.updateCategory(id, alteredCategory);
      if (!updateCategory) {
        util.setError(404, `Cannot find category with the id: ${id}`);
      } else {
        util.setSuccess(200, 'Category updated', updateCategory);
      }
      return util.send(res);
    } catch (error) {
      const message = improveMessage(error.message);
      util.setError(404, message);
      return util.send(res);
    }
  }

  static async getACategory(req, res) {
    const { id } = req.params;

    if (!Number(id)) {
      util.setError(400, 'Please input a valid numeric value');
      return util.send(res);
    }

    try {
      const theCategory = await CategoryService.getACategory(id);

      if (!theCategory) {
        util.setError(404, `Cannot find category with the id ${id}`);
      } else {
        util.setSuccess(200, 'Found Category', theCategory);
      }
      return util.send(res);
    } catch (error) {
      util.setError(404, error);
      return util.send(res);
    }
  }

  static async deleteCategory(req, res) {
    const { id } = req.params;

    if (!Number(id)) {
      util.setError(400, 'Please provide a numeric value');
      return util.send(res);
    }

    try {
      const categoryToDelete = await CategoryService.deleteCategory(id);

      if (categoryToDelete) {
        util.setSuccess(200, 'Category deleted');
      } else {
        util.setError(404, `Category with the id ${id} cannot be found`);
      }
      return util.send(res);
    } catch (error) {
      util.setError(400, error);
      return util.send(res);
    }
  }
}

export default CategoryController;