All files / mylog-api/controllers aircraft.controller.js

20.73% Statements 17/82
4.54% Branches 2/44
5.88% Functions 1/17
20.73% Lines 17/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 1882x 2x 2x 2x 2x 2x                                                         2x                                                 2x                                 2x                                   2x                                   2x                                               2x                                   2x                                   2x 4x   2x         2x              
const Aircraft = require("../models/aircraft.model.js");
const Manufacturer = require("../models/manufacturer.model.js");
const AircraftType = require("../models/aircraftType.model.js");
const { body, validationResult } = require('express-validator')
const luxon = require("luxon");
exports.create = (req, res) => {
  // Validate request
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    console.log(errors.array());
    return res.status(422).json({ errors: errors.array() });
  }
 
  // Create a Pilot
  const aircraft = {
    Registration: req.body.reg,
    Manufacturer: req.body.manufacturer,
    Model: req.body.model,
    AircraftType: req.body.aircraftType,
    Status: 5
  };
 
  Aircraft.create(aircraft, (err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the aircraft."
      });
    else res.send(data);
  });
};
 
 
 
exports.searchByReg = (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    console.log(errors.array());
    return res.status(422).json({ errors: errors.array() });
  }
  var reg = req.body.reg;
  var exact = req.body.exact;
  Aircraft.searchByReg(reg, exact, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Aircraft with reg ${reg}.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Aircraft with reg " + reg
        });
      }
    }
    else res.send(data);
  });
};
 
 
exports.getAll = (req, res) => {
  Aircraft.get((err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Aircrafts`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Aircrafts"
        });
      }
    }
    else res.send(data);
  });
};
 
exports.getManufacturers = (req, res) => {
  Manufacturer.get((err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Aircraft Manufacturers.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Aircraft Manufacturers"
        });
      }
    }
    else res.send(data);
  });
};
 
 
exports.getAllTypes = (req, res) => {
  AircraftType.get((err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Aircraft Types`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Aircraft Types"
        });
      }
    }
    else res.send(data);
  });
};
 
 
exports.getByManufacturer = (req, res) => {
  var manufacturer = req.params.manufacturer;
 
  if (!manufacturer) {
    console.log("manufacturer is empty");
    return res.status(422).json({ errors: { error: "manufacturer is empty" } });
  }
 
  AircraftType.getByManufacturer(manufacturer, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Aircraft Types with reg ${manufacturer}.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Aircraft Types with reg " + manufacturer
        });
      }
    }
    else res.send(data);
  });
};
 
exports.sync = (req, res) => {
  var version = req.query.version;
  var utcVersion = luxon.DateTime.fromSQL(version).toISO({ includeOffset: false, suppressMilliseconds: true });
 
  if (version === "") {
    version = 0;
  }
  Aircraft.sync(utcVersion, (err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while syncing aircrafts."
      });
    else res.send(data);
  });
 
}
 
exports.syncTypes = (req, res) => {
  var version = req.query.version;
  var utcVersion = luxon.DateTime.fromSQL(version).toISO({ includeOffset: false, suppressMilliseconds: true });
 
  if (version === "") {
    version = 0;
  }
  AircraftType.sync(utcVersion, (err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while syncing aircraft types."
      });
    else res.send(data);
  });
 
}
 
exports.validate = (method) => {
  switch (method) {
    case 'searchByReg': {
      return [
        body('reg', "reg is empty").exists(),
      ]
    }
    case 'create': {
      return [
        body('reg', "reg is empty").exists(),
        body('manufacturer', "manufacturer is empty").exists(),
        body('model', "model is empty").exists(),
      ]
    }
  }
};