All files restful_regions.js

26.32% Statements 15/57
15.38% Branches 4/26
25% Functions 1/4
26.32% Lines 15/57

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      1x                                                                                                                         1x           3x 3x           3x 3x 1x   2x           2x 2x 1x   1x               1x                                                                                                     1x                                                             1x  
/*       */
"use strict";
 
const getRegion = async (
    res     ,
    dbConn     ,
    country         ,
    region         
) => {
    try {
        let sqlQuery = "";
        if (country == null) {
            sqlQuery = `
                SELECT rr.* 
                FROM regions rr 
                JOIN countries cc 
                ON cc.countryCode=rr.countryCode
                ORDER BY cc.countryCode, rr.regionCode
        `;
        } else if (region == null) {
            sqlQuery = `
                SELECT 1
                FROM countries
                WHERE countryCode="${country}"
            `;
 
            const countries = await dbConn.query(sqlQuery);
            if (countries.length === 0) {
                return res.status(404).send("Country not found");
            }
 
            sqlQuery = `
                SELECT rr.* 
                FROM regions rr 
                JOIN countries cc 
                ON cc.countryCode=rr.countryCode
                WHERE rr.countryCode="${country}"
                ORDER BY rr.regionCode
            `;
        } else {
            sqlQuery = `
                SELECT rr.* 
                FROM regions rr 
                JOIN countries cc 
                ON cc.countryCode=rr.countryCode
                WHERE rr.countryCode="${country}" 
                AND rr.regionCode="${region}"
            `;
        }
 
        const regions = await dbConn.query(sqlQuery);
        if (regions.length > 0 || region === null) {
            res
                .status(200)
                .set("Content-Type", "application/json")
                .send(JSON.stringify(regions));
        } else {
            res.status(404).send("Not found");
        }
    } catch (e) {
        res.status(500).send("Server error");
    }
};
 
const deleteRegion = async (
    res     ,
    dbConn     ,
    country        ,
    region        
) => {
    try {
        const sqlCities = `
            SELECT 1 FROM cities 
            WHERE countryCode="${country}" 
            AND regionCode="${region}" 
            LIMIT 1     
        `;
        const cities = await dbConn.query(sqlCities);
        if (cities.length > 0) {
            res.status(405).send("Cannot delete a region with cities");
        } else {
            const deleteRegion = `
                DELETE FROM regions 
                WHERE countryCode="${country}" 
                AND regionCode="${region}"
            `;
 
            const result = await dbConn.query(deleteRegion);
            if (result.info.affectedRows > 0) {
                res.status(204).send();
            } else {
                res.status(404).send("Region not found");
            }
        }
    } catch (e) {
        res.status(500).send("Server error");
    }
};
 
const postRegion = async (
    res     ,
    dbConn     ,
    country        ,
    name        
) => {
    if (!name) {
        return res.status(400).send("Missing name");
    }
 
    try {
        const sqlCountry = `
            SELECT 1 
            FROM countries
            WHERE countryCode="${country}" 
        `;
        const countries = await dbConn.query(sqlCountry);
        if (countries.length === 0) {
            res.status(403).send("Country must exist");
        }
 
        const sqlGetId = `
            SELECT MAX(CAST(regionCode AS INTEGER)) AS maxr 
            FROM regions
            WHERE countryCode="${country}" 
        `;
        const regions = await dbConn.query(sqlGetId);
        const newId =
            regions.length === 0 ? 1 : 1 + Number(regions[0].maxr);
 
        const sqlAddRegion = `
            INSERT INTO regions SET 
            countryCode="${country}",
            regionCode="${newId}",
            regionName="${name}"
        `;
 
        const result = await dbConn.query(sqlAddRegion);
        if (result.info.affectedRows > 0) {
            res
                .status(201)
                .header("Location", `/regions/${country}/${newId}`)
                .send("Region created");
        } else {
            res.status(409).send("Region not created");
        }
    } catch (e) {
        res.status(500).send("Server error");
    }
};
 
const putRegion = async (
    res     ,
    dbConn     ,
    country        ,
    region        ,
    name        
) => {
    if (!name) {
        return res.status(400).send("Missing name");
    }
 
    try {
        const sqlUpdateRegion = `
            UPDATE regions
            SET regionName="${name}"
            WHERE countryCode="${country}" 
            AND regionCode="${region}" 
        `;
 
        const result = await dbConn.query(sqlUpdateRegion);
 
        if (result.info.affectedRows > 0) {
            res.status(204).send();
        } else {
            res.status(409).send("Region not updated");
        }
    } catch (e) {
        res.status(500).send("Server error");
    }
};
 
module.exports = { getRegion, deleteRegion, postRegion, putRegion };