#3 Пересмотрел проект

Yhdistetty
GrauKatze yhdistetty 5 committia lähteestä GrauKatze/clearstart kohteeseen GrauKatze/master 2 vuotta sitten

+ 38 - 0
Controllers/createDBController.js

@@ -0,0 +1,38 @@
+const {
+    selectDataAll,
+    Vendor,
+    insertData,
+    Processor,
+    VideoCard,
+} = require("./dataBase");
+
+function createDataBase(req, res) {
+    selectDataAll(Vendor)
+        .then((Vendor) => {
+            res.render("create.hbs", {
+                title: "create",
+                vendor: Vendor,
+            });
+        })
+        .catch((err) => console.log(err));
+}
+function postCreateDataBase(req, res) {
+    if (!req.body) return res.sendStatus(400);
+    console.log(req.body);
+    let newNote = {
+        modelName: req.body.modelName,
+        vendor_id: req.body.hardVendor,
+    };
+    let Modul;
+    if (req.body.hardType === "Processor") {
+        Modul = Processor;
+    } else if (req.body.hardType === "VideoCard") Modul = VideoCard;
+    insertData(Modul, newNote);
+    setTimeout(() => {
+        res.redirect("/dataBase/hards");
+    }, 1000);
+}
+module.exports = {
+    createDataBase,
+    postCreateDataBase,
+};

+ 267 - 0
Controllers/dataBase.js

@@ -0,0 +1,267 @@
+const { Sequelize, Model, DataTypes } = require("sequelize");
+const sequelize = new Sequelize("distrohard", "graukatze", "1234", {
+    host: "localhost",
+    dialect: "postgres",
+});
+
+class Vendor extends Model {}
+Vendor.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        companyName: DataTypes.STRING(50),
+    },
+    {
+        sequelize,
+        modelName: "Vendors",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class Processor extends Model {}
+Processor.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        modelName: DataTypes.STRING(50),
+        vendor_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Vendor, key: "id" },
+        },
+    },
+    {
+        sequelize,
+        modelName: "Processors",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class VideoCard extends Model {}
+VideoCard.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        modelName: DataTypes.STRING(50),
+        vendor_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Vendor, key: "id" },
+        },
+    },
+    {
+        sequelize,
+        modelName: "VideoCards",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class DistroLinux extends Model {}
+DistroLinux.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        vendor: DataTypes.STRING(50),
+        version: DataTypes.STRING(50),
+    },
+    {
+        sequelize,
+        modelName: "DistroLinux",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class Errors extends Model {}
+Errors.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        codeCVE: {
+            type: DataTypes.STRING(20),
+        },
+    },
+    {
+        sequelize,
+        modelName: "Errors",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class ProcessorStatusOnLinux extends Model {}
+ProcessorStatusOnLinux.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        Processor_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Processor, key: "id" },
+        },
+        DistLinux_id: {
+            type: DataTypes.INTEGER,
+            references: { model: DistroLinux, key: "id" },
+        },
+        Status: DataTypes.STRING(25),
+    },
+    {
+        sequelize,
+        modelName: "ProcessorStatusOnLinux",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class VideoCardStatusOnLinux extends Model {}
+VideoCardStatusOnLinux.init(
+    {
+        id: {
+            type: DataTypes.INTEGER,
+            autoIncrement: true,
+            primaryKey: true,
+        },
+        VideoCard_id: {
+            type: DataTypes.INTEGER,
+            references: { model: VideoCard, key: "id" },
+        },
+        DistLinux_id: {
+            type: DataTypes.INTEGER,
+            references: { model: DistroLinux, key: "id" },
+        },
+        Status: DataTypes.STRING(25),
+    },
+    {
+        sequelize,
+        modelName: "VideoCardStatusOnLinux",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class ErrorStatusOnProcessor extends Model {}
+ErrorStatusOnProcessor.init(
+    {
+        Error_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Errors, key: "id" },
+        },
+        Processor_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Processor, key: "id" },
+        },
+        Status: DataTypes.STRING(25),
+    },
+    {
+        sequelize,
+        modelName: "ErrorStatusOnProcessor",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class ErrorStatusOnVideoCard extends Model {}
+ErrorStatusOnVideoCard.init(
+    {
+        Error_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Errors, key: "id" },
+        },
+        VideoCard_id: {
+            type: DataTypes.INTEGER,
+            references: { model: VideoCard, key: "id" },
+        },
+        Status: DataTypes.STRING(25),
+    },
+    {
+        sequelize,
+        modelName: "ErrorStatusOnVideoCard",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+class ErrorStatusOnLinux extends Model {}
+ErrorStatusOnLinux.init(
+    {
+        Error_id: {
+            type: DataTypes.INTEGER,
+            references: { model: Errors, key: "id" },
+        },
+        DistroLinux_id: {
+            type: DataTypes.INTEGER,
+            references: { model: DistroLinux, key: "id" },
+        },
+        Status: DataTypes.STRING(25),
+    },
+    {
+        sequelize,
+        modelName: "ErrorStatusOnLinux",
+        timestamps: false,
+        freezeTableName: true,
+    }
+);
+
+async function syncDataBase() {
+    await sequelize.sync({ alter: true }).then(() => {
+        console.log("\n================Basa was sync================");
+    });
+}
+
+//===========================
+//-----FUNC FOR DATABASE-----
+//===========================
+//work
+
+function insertData(Model, jsonData) {
+    Model.create(jsonData);
+}
+async function updateData(Model, modelID, jsonData) {
+    Model.update(jsonData, { where: { id: modelID } });
+}
+//work
+async function deleteData(Model, jsonData) {
+    Model.destroy({ where: jsonData });
+}
+//work
+async function selectDataAll(Model) {
+    return Model.findAll({ raw: true });
+}
+async function selectDataAllFiltr(Model, jsonData) {
+    return Model.findAll({ raw: true, where: jsonData });
+}
+async function selectDataOne(Model, jsonData) {
+    return Model.findOne({ raw: true, where: jsonData });
+}
+
+module.exports = {
+    sequelize,
+    syncDataBase,
+    DistroLinux,
+    selectDataAll,
+    insertData,
+    deleteData,
+    Vendor,
+    Processor,
+    VideoCard,
+    selectDataAllFiltr,
+    selectDataOne,
+    ProcessorStatusOnLinux,
+    VideoCardStatusOnLinux,
+    Errors,
+    updateData,
+    ErrorStatusOnLinux,
+    ErrorStatusOnVideoCard,
+    ErrorStatusOnProcessor,
+    VideoCardStatusOnLinux,
+    ProcessorStatusOnLinux,
+};

+ 125 - 0
Controllers/dataBaseController.js

@@ -0,0 +1,125 @@
+const {
+    selectDataAll,
+    Vendor,
+    Processor,
+    VideoCard,
+    selectDataOne,
+    selectDataAllFiltr,
+    ProcessorStatusOnLinux,
+    VideoCardStatusOnLinux,
+    DistroLinux,
+    Errors,
+} = require("./dataBase");
+
+function showHards(req, res) {
+    selectDataAll(Processor).then((proc) => {
+        selectDataAll(VideoCard).then((videoCard) => {
+            res.render("Hards.hbs", {
+                title: "Hards",
+                proc: proc,
+                videoCard: videoCard,
+            });
+        });
+    });
+}
+function showDistros(req, res) {
+    selectDataAll(DistroLinux).then((distr) => {
+        res.render("Distros.hbs", {
+            title: "Distros",
+            distr: distr,
+        });
+    });
+}
+function showErrors(req, res) {
+    selectDataAll(Errors).then((err) => {
+        res.render("Errors.hbs", {
+            title: "Errors",
+            err: err,
+        });
+    });
+}
+
+function indexDataBase(req, res) {
+    res.render("dataBase.hbs");
+}
+function searchDataBase(req, res, next) {
+    selectDataAll(Vendor)
+        .then((Vendor) => {
+            selectDataAll(Processor).then((Processor) => {
+                selectDataAll(VideoCard).then((VideoCard) => {
+                    res.render("DBsearch.hbs", {
+                        title: "Search",
+                        vendor: Vendor,
+                        processor: Processor,
+                        videoCard: VideoCard,
+                    });
+                });
+            });
+        })
+        .catch((err) => console.log(err));
+}
+
+async function postSearchDataBase(req, res) {
+    let hardStatus = [];
+    Processor.findOne({ where: { modelName: req.body.procModel } }).then(
+        (processor) => {
+            ProcessorStatusOnLinux.findAll({
+                where: { Processor_id: processor.id },
+            })
+                .then((procStatusLinux) => {
+                    procStatusLinux.forEach((pSL) => {
+                        DistroLinux.findAll({
+                            where: { id: pSL.DistLinux_id },
+                        }).then((procData) => {
+                            hardStatus.push({
+                                hardModel: processor.modelName,
+                                OS: procData[0].vendor,
+                                status: pSL.Status,
+                            });
+                        });
+                    });
+                })
+                .then(() => {
+                    VideoCard.findOne({
+                        where: { modelName: req.body.videoCardModel },
+                    }).then((videoCard) => {
+                        VideoCardStatusOnLinux.findAll({
+                            where: { VideoCard_id: videoCard.id },
+                        })
+                            .then((videoCardStatusLinux) => {
+                                videoCardStatusLinux.forEach((vcSL) => {
+                                    DistroLinux.findAll({
+                                        where: { id: vcSL.DistLinux_id },
+                                    }).then((videoCardData) => {
+                                        hardStatus.push({
+                                            hardModel: videoCard.modelName,
+                                            OS: videoCardData[0].vendor,
+                                            status: vcSL.Status,
+                                        });
+                                    });
+                                });
+                            })
+                            .then(() =>
+                                setTimeout(function () {
+                                    res.render("results.hbs", {
+                                        title: "result",
+                                        result: hardStatus,
+                                    });
+                                    console.log(hardStatus);
+                                }, 2000)
+                            );
+                    });
+                });
+        }
+    );
+}
+
+module.exports = {
+    indexDataBase,
+    searchDataBase,
+    postSearchDataBase,
+    showHards,
+    showDistros,
+    showDistros,
+    showErrors,
+};

+ 119 - 0
Controllers/deleteBDController.js

@@ -0,0 +1,119 @@
+const {
+    Vendor,
+    VideoCard,
+    deleteData,
+    Processor,
+    selectDataAll,
+    selectDataAllFiltr,
+    ErrorStatusOnProcessor,
+    ProcessorStatusOnLinux,
+    ErrorStatusOnVideoCard,
+    VideoCardStatusOnLinux,
+} = require("./dataBase");
+
+function deleteProcDB(req, res) {
+    const procID = req.params.procID;
+    selectDataAll(Vendor)
+        .then((vendor) => {
+            selectDataAllFiltr(Processor, { id: procID }).then((proc) => {
+                Vendor.findByPk(proc[0].vendor_id).then((vendorID) =>
+                    res.render("delete.hbs", {
+                        obj: proc[0],
+                        vendor: vendor,
+                        venID: vendorID.companyName,
+                    })
+                );
+            });
+        })
+        .catch((err) => console.log(err));
+}
+function deleteVideoCardDB(req, res) {
+    const videoCardID = req.params.videoCardID;
+    selectDataAll(Vendor)
+        .then((vendor) => {
+            selectDataAllFiltr(VideoCard, { id: videoCardID }).then(
+                (videoCard) => {
+                    Vendor.findByPk(videoCard[0].vendor_id).then((vendorID) =>
+                        res.render("delete.hbs", {
+                            obj: videoCard[0],
+                            vendor: vendor,
+                            venID: vendorID.companyName,
+                        })
+                    );
+                }
+            );
+        })
+        .catch((err) => console.log(err));
+}
+function postDeleteProcDB(req, res) {
+    console.log("proc");
+    if (!req.params) return res.sendStatus(400);
+    console.log(req.params);
+
+    ErrorStatusOnProcessor.findAll({
+        raw: true,
+        where: { Processor_id: req.params.procID },
+    })
+        .then((ESONP) => {
+            if (ESONP !== null)
+                ESONP.forEach((el) => {
+                    ErrorStatusOnProcessor.destroy({ where: { id: el.id } });
+                });
+        })
+        .then(
+            ProcessorStatusOnLinux.findAll({
+                where: { Processor_id: req.params.procID },
+            })
+                .then((PSOL) => {
+                    if (PSOL !== null)
+                        PSOL.forEach((el) => {
+                            ProcessorStatusOnLinux.destroy({
+                                where: { id: el.id },
+                            });
+                        });
+                })
+                .then(() =>
+                    Processor.destroy({ where: { id: req.params.procID } })
+                )
+                .then(() => res.redirect("/dataBase/hards"))
+        );
+}
+function postDeleteVideoCardDB(req, res) {
+    if (!req.params) return res.sendStatus(400);
+    console.log(req.params);
+    ErrorStatusOnVideoCard.findAll({
+        where: { VideoCard_id: req.params.videoCardID },
+    })
+        .then((EROVC) => {
+            if (EROVC !== null)
+                EROVC.forEach((el) => {
+                    ErrorStatusOnVideoCard.destroy({ where: { id: el.id } });
+                });
+        })
+        .then(() =>
+            VideoCardStatusOnLinux.findAll({
+                where: { VideoCard_id: req.params.videoCardID },
+            })
+                .then((VCSOL) => {
+                    if (VCSOL !== null)
+                        VCSOL.forEach((el) =>
+                            VideoCardStatusOnLinux.destroy({
+                                where: { id: el.id },
+                            })
+                        );
+                })
+                .then(
+                    VideoCard.destroy({
+                        where: { id: req.params.videoCardID },
+                    })
+                )
+                .then(() => res.redirect("/dataBase/hards"))
+        );
+}
+
+module.exports = {
+    deleteProcDB,
+    deleteVideoCardDB,
+    postDeleteVideoCardDB,
+    postDeleteProcDB,
+};

+ 151 - 0
Controllers/editDBController.js

@@ -0,0 +1,151 @@
+const {
+    updateData,
+    Processor,
+    selectDataAllFiltr,
+    Vendor,
+    selectDataAll,
+    selectDataOne,
+    VideoCard,
+    DistroLinux,
+    ProcessorStatusOnLinux,
+    VideoCardStatusOnLinux,
+} = require("./dataBase");
+
+function editProc(req, res) {
+    const procID = req.params.procID;
+    Vendor.findAll()
+        .then((vendors) => {
+            Processor.findOne({ where: { id: procID } }).then((proc) => {
+                Vendor.findByPk(proc.vendor_id).then((vendor) => {
+                    DistroLinux.findAll().then((linux) =>
+                        res.render("Edit.hbs", {
+                            obj: proc,
+                            linux: linux,
+                            vendor: vendors,
+                            venID: vendor.companyName,
+                        })
+                    );
+                });
+            });
+        })
+        .catch((err) => console.log(err));
+}
+function editVideoCard(req, res) {
+    const videoCardID = req.params.videoCardID;
+    Vendor.findAll()
+        .then((vendors) => {
+            VideoCard.findOne({ where: { id: videoCardID } }).then(
+                (videoCard) => {
+                    Vendor.findByPk(videoCard.vendor_id).then((vendor) => {
+                        DistroLinux.findAll().then((linux) =>
+                            res.render("Edit.hbs", {
+                                obj: videoCard,
+                                linux: linux,
+                                vendor: vendors,
+                                venID: vendor.companyName,
+                            })
+                        );
+                    });
+                }
+            );
+        })
+        .catch((err) => console.log(err));
+}
+
+function postEditProc(req, res) {
+    if (!req.body) return res.sendStatus(400);
+    console.log(req.body);
+    console.log(req.params);
+    Processor.update(
+        {
+            modelName: req.body.modelName,
+            vendor_id: req.body.Vendor,
+        },
+        {
+            where: {
+                id: req.params.procID,
+            },
+        }
+    )
+        .then(() => {
+            if (req.body.LinuxStatus !== null) {
+                ProcessorStatusOnLinux.findOne({
+                    where: {
+                        Processor_id: req.params.procID,
+                        DistLinux_id: req.body.linuxVendor,
+                    },
+                }).then((PSOL) => {
+                    if (PSOL !== null) {
+                        ProcessorStatusOnLinux.update(
+                            { Status: req.body.linuxStatus },
+                            {
+                                where: {
+                                    Processor_id: req.params.procID,
+                                    DistLinux_id: req.body.linuxVendor,
+                                },
+                            }
+                        );
+                    } else {
+                        ProcessorStatusOnLinux.create({
+                            Processor_id: req.params.procID,
+                            DistLinux_id: req.body.linuxVendor,
+                            Status: req.body.linuxStatus,
+                        });
+                    }
+                });
+            }
+        })
+        .then(() => res.redirect("/dataBase/hards"));
+}
+function postEditVideoCard(req, res) {
+    if (!req.body) return res.sendStatus(400);
+    console.log(req.body);
+    console.log(req.params);
+    VideoCard.update(
+        {
+            modelName: req.body.modelName,
+            vendor_id: req.body.Vendor,
+        },
+        {
+            where: {
+                id: req.params.videoCardID,
+            },
+        }
+    )
+        .then(() => {
+            if (req.body.LinuxStatus !== null) {
+                VideoCardStatusOnLinux.findOne({
+                    where: {
+                        VideoCard_id: req.params.videoCardID,
+                        DistLinux_id: req.body.linuxVendor,
+                    },
+                }).then((PSOL) => {
+                    if (PSOL !== null) {
+                        VideoCardStatusOnLinux.update(
+                            { Status: req.body.linuxStatus },
+                            {
+                                where: {
+                                    VideoCard_id: req.params.videoCardID,
+                                    DistLinux_id: req.body.linuxVendor,
+                                },
+                            }
+                        );
+                    } else {
+                        VideoCardStatusOnLinux.create({
+                            VideoCard_id: req.params.videoCardID,
+                            DistLinux_id: req.body.linuxVendor,
+                            Status: req.body.linuxStatus,
+                        });
+                    }
+                });
+            }
+        })
+        .then(() => res.redirect("/dataBase/hards"));
+}
+
+module.exports = {
+    editProc,
+    editVideoCard,
+    postEditProc,
+    postEditVideoCard,
+};

+ 7 - 0
Controllers/homeController.js

@@ -0,0 +1,7 @@
+exports.index = function (req, res) {
+    res.render('index.hbs')
+}
+
+exports.about = function (req, res) {
+    res.render('about.hbs')
+}

+ 14 - 0
Routers/createDBRoute.js

@@ -0,0 +1,14 @@
+const express = require("express");
+const {
+    createDataBase,
+    postCreateDataBase,
+} = require("../Controllers/createDBController");
+
+const createRouter = express.Router();
+const urlencodedParser = express.urlencoded({ extended: false });
+
+createRouter.get("/", createDataBase);
+createRouter.post("/", urlencodedParser, postCreateDataBase);
+module.exports = {
+    createRouter,
+};

+ 21 - 0
Routers/dataBaseRouter.js

@@ -0,0 +1,21 @@
+const express = require("express");
+const {
+    indexDataBase,
+    searchDataBase,
+    postSearchDataBase,
+    showHards,
+    showDistros,
+    showErrors,
+} = require("../Controllers/dataBaseController");
+const dataBaseRouter = express.Router();
+const urlencodedParser = express.urlencoded({ extended: false });
+
+dataBaseRouter.get("/", indexDataBase);
+dataBaseRouter.get("/search", searchDataBase);
+dataBaseRouter.get("/hards", showHards);
+dataBaseRouter.get("/distros", showDistros);
+dataBaseRouter.get("/errors", showErrors);
+
+dataBaseRouter.post("/search", urlencodedParser, postSearchDataBase);
+
+module.exports = { dataBaseRouter };

+ 16 - 0
Routers/deleteDBRouter.js

@@ -0,0 +1,16 @@
+const express = require("express");
+const {
+    deleteProcDB,
+    deleteVideoCardDB,
+    postDeleteVideoCardDB,
+    postDeleteProcDB,
+} = require("../Controllers/deleteBDController");
+
+const deleteBaseRouter = express.Router();
+const urlencodedParser = express.urlencoded({ extended: false });
+
+deleteBaseRouter.get("/proc/:procID", deleteProcDB);
+deleteBaseRouter.get("/videoCard/:videoCardID", deleteVideoCardDB);
+deleteBaseRouter.post("/proc/:procID", postDeleteProcDB);
+deleteBaseRouter.post("/videoCard/:videoCardID", postDeleteVideoCardDB);
+module.exports = { deleteBaseRouter };

+ 0 - 0
Routers/editDBRouter.js


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä