const NotificationErrorSparepart = require('../services/notification_error_sparepart.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { updateNotificationErrorSparepartSchema, insertNotificationErrorSparepartSchema } = require('../validate/notification_error_sparepart.schema');

class NotificationErrorSparepartController {

  static async getAll(req, res) {
    const { contact_id } = req.body;
    const queryParams = req.query;
    const results = await NotificationErrorSparepart.getAll(queryParams, contact_id);
    const response = await setResponsePaging(queryParams, results, 'Notification Error Sparepart found');

    res.status(response.statusCode).json(response);
  }

  static async getById(req, res) {
    const { id } = req.params;
    const results = await NotificationErrorSparepart.getById(id);
    const response = await setResponse(results, 'Notification Error Sparepart found');

    res.status(response.statusCode).json(response);
  }

  static async create(req, res) {
    const { error, value } = await checkValidate(insertNotificationErrorSparepartSchema, req);

    if (error) {
      return res.status(400).json(setResponse(error, 'Validation failed', 400));
    }

    const results = await NotificationErrorSparepart.create(value);
    const response = await setResponse(results, 'Notification Error Sparepart created successfully');

    res.status(response.statusCode).json(response);
  }

  static async update(req, res) {
    const { id } = req.params;
    const { error, value } = await checkValidate(updateNotificationErrorSparepartSchema, req);

    if (error) {
      return res.status(400).json(setResponse(error, 'Validation failed', 400));
    }

    const results = await NotificationErrorSparepart.update(id, value);
    const response = await setResponse(results, 'Notification Error Sparepart updated successfully');

    res.status(response.statusCode).json(response);
  }

  static async delete(req, res) {
    const { id } = req.params;
    const { contact_id } = req.body;

    const results = await NotificationErrorSparepart.delete(id, contact_id);
    const response = await setResponse(results, 'Notification Error Sparepart deleted successfully');

    res.status(response.statusCode).json(response);
  }
}

module.exports = NotificationErrorSparepartController;
