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

21.56% Statements 11/51
8.33% Branches 2/24
11.11% Functions 1/9
21.56% Lines 11/51

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 1172x 2x 2x 2x                                                               2x                                                   2x                                   2x                                     2x 4x   2x           2x                  
const Simulator = require("../models/simulator.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() });
    }
    var utcVersion = luxon.DateTime.fromSQL(req.body.version).toISO({ includeOffset: false, suppressMilliseconds: true });
    // Create a Simulator
    console.log(req.body);
    const simulator = {
        Registration: req.body.reg,
        Manufacturer: req.body.manufacturer,
        PilotID: req.body.pilotID,
        SimDeviceType: req.body.simDeviceType,
        Model: req.body.model,
        AircraftType: req.body.aircraftType,
        Version: utcVersion,
        Name: req.body.name,
        Status: 5
    };
 
    Simulator.create(simulator, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while creating the aircraft."
            });
        else res.send(data);
    });
};
 
exports.searchByRegByPilotID = (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        console.log(errors.array());
        return res.status(422).json({ errors: errors.array() });
    }
    var keyword = req.body.keyword;
    var pilotID = req.body.pilotID;
    var exact = req.body.exact;
    Simulator.searchByReg(keyword, pilotID, exact, (err, data) => {
        if (err) {
            if (err.kind === "not_found") {
                res.status(404).send({
                    message: `Not found Aircraft with keyword ${keyword}.`
                });
            } else {
                res.status(500).send({
                    message: "Error retrieving Aircraft with keyword " + keyword
                });
            }
        }
        else res.send(data);
    });
};
 
 
exports.getAllByPilotID = (req, res) => {
    var pilotID = req.query.pilotID;
    Simulator.get(pilotID, (err, data) => {
        if (err) {
            if (err.kind === "not_found") {
                res.status(404).send({
                    message: `Not found Simulators`
                });
            } else {
                res.status(500).send({
                    message: "Error retrieving Simulators"
                });
            }
        }
        else res.send(data);
    });
};
 
exports.sync = (req, res) => {
    var version = req.query.version;
    var utcVersion = luxon.DateTime.fromSQL(version).toISO({ includeOffset: false, suppressMilliseconds: true });
 
    var pilotID = req.query.pilotID;
    if (version === "") {
        version = 0;
    }
    Simulator.sync(pilotID, utcVersion, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while syncing simulators."
            });
        else res.send(data);
    });
 
}
 
exports.validate = (method) => {
    switch (method) {
        case 'searchByReg': {
            return [
                body('keyword', "keyword is empty").exists(),
                body('pilotID', "pilotID is invalid").exists().isInt(),
            ]
        }
        case 'create': {
            return [
                body('reg', "reg is empty").exists(),
                body('pilotID', "pilotID is invalid").exists().isInt(),
                body('simDeviceType', "simDeviceType is invalid").exists().isInt(),
                body('manufacturer', "manufacturer is empty").exists(),
                body('model', "model is empty").exists(),
            ]
        }
    }
};