Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion models/Drone.model.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
// Iteration #1
// Iteration #1
const {Schema, model} = require('mongoose');

const droneSchema = new Schema( {
name: String,
propellers: Number,
maxSpeed: Number,
});

const drone = model("drones", droneSchema);

module.exports = drone;
109 changes: 73 additions & 36 deletions routes/drones.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,73 @@
const express = require('express');
const router = express.Router();

// require the Drone model here

router.get('/drones', (req, res, next) => {
// Iteration #2: List the drones
// ... your code here
});

router.get('/drones/create', (req, res, next) => {
// Iteration #3: Add a new drone
// ... your code here
});

router.post('/drones/create', (req, res, next) => {
// Iteration #3: Add a new drone
// ... your code here
});

router.get('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
});

router.post('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
});

router.post('/drones/:id/delete', (req, res, next) => {
// Iteration #5: Delete the drone
// ... your code here
});

module.exports = router;
const express = require('express');
const router = express.Router();

// require the Drone model here

router.get('/drones', (req, res, next) => {
// Iteration #2: List the drones
// ... your code here
droneModel.find()
.then((drones) => {
res.render("drones/list.hbs", {drones})
}).catch((err) => {
console.log('Failed!')
});
});

router.get('/drones/create', (req, res, next) => {
// Iteration #3: Add a new drone
// ... your code here
res.render("drones/create-form.hbs")
});

router.post('/drones/create', (req, res, next) => {
// Iteration #3: Add a new drone
// ... your code here
const {name,propellers,maxSpeed} = req.body
droneModel.create({name,propellers,maxSpeed})
.then((result) => {
res.redirect("/drones")
}).catch((err) => {
console.log('Failed!')
});
});


router.get('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
const {id} = req.params;
droneModel.findById(id)
.then((drones) => {
res.render('drones/update-form.hbs',{drones})
}).catch((err) => {
console.log('Failed!')
});
});

router.post('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
const {id} = req.params
const {name,propellers,maxSpeed} = req.body
droneModel.findByIdAndUpdate(id, {name,propellers,maxSpeed})
.then((result) => {
res.redirect('/drones')
}).catch((err) => {
console.log('Failed!')
});
});

router.post('/drones/:id/delete', (req, res, next) => {
// Iteration #5: Delete the drone
// ... your code here
const {id} = req.params
droneModel.findByIdAndDelete(id)
.then((result) => {
res.redirect('/drones')
}).catch((err) => {
console.log('Failed!')
});
});

module.exports = router;
14 changes: 7 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require("express");
const router = express.Router();

/* GET home page */
router.get("/", (req, res, next) => res.render("index"));

module.exports = router;
const express = require("express");
const router = express.Router();
/* GET home page */
router.get("/", (req, res, next) => res.render("index"));
module.exports = router;
27 changes: 26 additions & 1 deletion seeds/drones.seed.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
// Iteration #1
// Iteration #1
require ('../db')


const mongoose = require('mongoose');
const droneModel = require ('../models/Drone.model')

const dronesData = [

{ name: "Creeper XL 500", propellers: 3, maxSpeed: 12 },

{ name: "Racer 57", propellers: 4, maxSpeed: 20 },

{ name: "Courier 3000i", propellers: 6, maxSpeed: 18 }

];

droneModel.create(dronesData)
.then((result) => {
console.log('Success!')
}).catch((err) => {
console.log('Failed!',err)
})
.finally(() => {
mongoose.connection.close();
});
18 changes: 17 additions & 1 deletion views/drones/create-form.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<h2>Create a new drone</h2>
<h2>Create a new drone</h2>
<form action="/drones/create" method="post">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input name="name" type="text" class="form-control" id="name">
</div>
<div class="mb-3">
<label for="propellers" class="form-label">Propellers</label>
<input name="propellers" type="text" class="form-control" id="propellers">
</div>
<div class="mb-3">
<label for="maxSpeed" class="form-label">Max speed</label>
<input name="maxSpeed" type="text" class="form-control" id="maxSpeed">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default pull-right" onclick="history.back()">Cancel</button>
</form>
18 changes: 17 additions & 1 deletion views/drones/list.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<h2>Available drones</h2>
<h2>Available drones</h2>
<br>
{{#each drones}}
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">{{name}}</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">No. of propellers {{propellers}}</li>
<li class="list-group-item">Max speed {{maxSpeed}}</li>
</ul>
<div class="card-body">
<a href="/drones/{{_id}}/edit" class="card-link">Update</a>
<a href="/drones/{{_id}}/delete" class="card-link">Delete</a>
</div>
</div>
{{/each}}
19 changes: 18 additions & 1 deletion views/drones/update-form.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
<h2>Update the drone</h2>
<h2>Update the drone</h2>
<br>
<form action="/drones/{{drones._id}}/edit" method="post">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input name="name" class="form-control" id="name" value="{{drones.name}}" />
</div>
<div class="mb-3">
<label for="propellers" class="form-label">Propellers</label>
<input name="propellers" type="text" class="form-control" id="propellers" value="{{drones.propellers}}"/>
</div>
<div class="mb-3">
<label for="maxSpeed" class="form-label">Max Speed</label>
<input name="maxSpeed" type="text" class="form-control" id="maxSpeed" value="{{drones.maxSpeed}}"/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default pull-right" onclick="history.back()">Cancel</button>
</form>
4 changes: 2 additions & 2 deletions views/error.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>Error!</h1>
<h2>There was an error on the server! Check the console!</h2>
<h1>Error!</h1>
<h2>There was an error on the server! Check the console!</h2>
4 changes: 2 additions & 2 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
36 changes: 18 additions & 18 deletions views/layout.hbs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{title}}</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
</head>

<body>

{{{body}}}

<script src="/js/script.js"></script>
</body>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{title}}</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
</head>
<body>
{{{body}}}
<script src="/js/script.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion views/not-found.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>404 Error</h1>
<h1>404 Error</h1>
<h2>This route does not exist on the app! Check if everything is ok!</h2>