diff --git a/db/index.js b/db/index.js index 873910d..4d5269f 100644 --- a/db/index.js +++ b/db/index.js @@ -5,12 +5,15 @@ const mongoose = require("mongoose"); // ℹ️ Sets the MongoDB URI for our app to have access to it. // If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app -const MONGO_URI = process.env.MONGODB_URI || "mongodb://localhost/lab-express-drones"; +const MONGO_URI = + process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/lab-express-drones"; mongoose .connect(MONGO_URI) .then((x) => { - console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`); + console.log( + `Connected to Mongo! Database name: "${x.connections[0].name}"` + ); }) .catch((err) => { console.error("Error connecting to mongo: ", err); diff --git a/models/Drone.model.js b/models/Drone.model.js index 93e3ea9..fcc995b 100644 --- a/models/Drone.model.js +++ b/models/Drone.model.js @@ -1 +1,21 @@ -// Iteration #1 \ No newline at end of file +// Iteration #1 + +const { Schema, model } = require("mongoose"); + +const droneSchema = new Schema({ + name: { + type: String, + required: true, + }, + propellers: { + type: Number, + required: true, + }, + maxSpeed: { + type: Number, + required: true, + }, +}); + +const Drone = model("Drone", droneSchema); +module.exports = Drone; diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 05a1470..760e75c 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,6 +1,7 @@ body { padding: 50px; - font: 14px 'Lucida Grande', Helvetica, Arial, sans-serif; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; + text-align: center; } a { diff --git a/routes/drones.js b/routes/drones.js index 51da515..162dce9 100644 --- a/routes/drones.js +++ b/routes/drones.js @@ -1,36 +1,93 @@ -const express = require('express'); +const express = require("express"); const router = express.Router(); // require the Drone model here +const Drone = require("../models/Drone.model"); -router.get('/drones', (req, res, next) => { +const bodyParser = require("body-parser"); +router.use(bodyParser.urlencoded({ extended: true })); + +router.get("/drones", (req, res, next) => { // Iteration #2: List the drones // ... your code here + Drone.find() + .then((listOfDrones) => { + console.log("Retrieved the list of drones from DB", listOfDrones); + res.render("drones/list", { dronesList: listOfDrones }); + }) + .catch((err) => { + console.log("Error while getting the Drones from the DB", err); + next(err); + }); }); -router.get('/drones/create', (req, res, next) => { +router.get("/drones/create", (req, res, next) => { // Iteration #3: Add a new drone // ... your code here + res.render("drones/create-form"); }); -router.post('/drones/create', (req, res, next) => { +router.post("/drones/create", (req, res, next) => { // Iteration #3: Add a new drone // ... your code here + const { droneName, dronePropellers, droneSpeed } = req.body; + Drone.collection + .insertOne({ + name: droneName, + propellers: dronePropellers, + maxSpeed: droneSpeed, + }) + .then(() => { + console.log("created"); + res.redirect("/drones"); + }) + .catch((err) => { + console.log("Error while inserting the new drone into DB", err); + next(err); + }); }); -router.get('/drones/:id/edit', (req, res, next) => { +router.get("/drones/:id/edit", (req, res, next) => { // Iteration #4: Update the drone // ... your code here + Drone.findById(req.params.id).then((d) => { + console.log(d); + res.render("drones/update-form", { d }); + }); }); -router.post('/drones/:id/edit', (req, res, next) => { +router.post("/drones/:id/edit", (req, res, next) => { // Iteration #4: Update the drone // ... your code here + const { droneName, dronePropellers, droneSpeed } = req.body; + const updateDrone = { + name: droneName, + propellers: dronePropellers, + maxSpeed: droneSpeed, + }; + Drone.findByIdAndUpdate(req.params.id, updateDrone) + .then(() => { + console.log("updated"); + res.redirect("/drones"); + }) + .catch((err) => { + console.log("Error while updating drone details", err); + next(err); + }); }); -router.post('/drones/:id/delete', (req, res, next) => { +router.post("/drones/:id/delete", (req, res, next) => { // Iteration #5: Delete the drone // ... your code here + Drone.findByIdAndDelete(req.params.id) + .then(() => { + console.log("deleted"); + res.redirect("/drones"); + }) + .catch((err) => { + console.log("Error while deleting the drone", err); + next(err); + }); }); module.exports = router; diff --git a/seeds/drones.seed.js b/seeds/drones.seed.js index 4b29ecb..1b58df5 100644 --- a/seeds/drones.seed.js +++ b/seeds/drones.seed.js @@ -1 +1,32 @@ // Iteration #1 +const dronesArray = [ + { name: "Creeper XL 500", propellers: 3, maxSpeed: 12 }, + + { name: "Racer 57", propellers: 4, maxSpeed: 20 }, + + { name: "Courier 3000i", propellers: 6, maxSpeed: 18 }, +]; + +const mongoose = require("mongoose"); + +const Drone = require("../models/Drone.model"); + +const MONGO_URI = + process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/lab-express-drones"; + +mongoose + .connect(MONGO_URI) + .then((x) => { + console.log( + `Connected to Mongo! Database name: "${x.connections[0].name}"` + ); + }) + .then(() => { + Drone.create(dronesArray).then((drones) => { + console.log(drones.length); + mongoose.connection.close(); + }); + }) + .catch((err) => { + console.error("Error connecting to mongo: ", err); + }); diff --git a/views/drones/create-form.hbs b/views/drones/create-form.hbs index 1a8197f..f48dc7a 100644 --- a/views/drones/create-form.hbs +++ b/views/drones/create-form.hbs @@ -1 +1,22 @@ -

Create a new drone

\ No newline at end of file +

Create a new drone

+ +
+ + + + + + + + +
\ No newline at end of file diff --git a/views/drones/list.hbs b/views/drones/list.hbs index a2830d3..af1092c 100644 --- a/views/drones/list.hbs +++ b/views/drones/list.hbs @@ -1 +1,31 @@ -

Available drones

\ No newline at end of file +

Available drones

+ + + + + + + + + + + + {{#each dronesList}} + + + + + + + + {{/each}} + +
NamePropellersMax. SpeedUpdateDelete
{{name}}{{propellers}}{{maxSpeed}}
+ +
+ + \ No newline at end of file diff --git a/views/drones/update-form.hbs b/views/drones/update-form.hbs index 0a27dc3..7a92587 100644 --- a/views/drones/update-form.hbs +++ b/views/drones/update-form.hbs @@ -1 +1,30 @@ -

Update the drone

\ No newline at end of file +

Update the drone

+ +
+ + + + + + + + +
\ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs index 1f308fd..75eb39f 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,2 +1,4 @@ -

{{title}}

-

Welcome to {{title}}

+ \ No newline at end of file diff --git a/views/layout.hbs b/views/layout.hbs index 73199c1..be5c13e 100644 --- a/views/layout.hbs +++ b/views/layout.hbs @@ -1,12 +1,17 @@ - - - - + + + {{title}} +