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
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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://localhost/drone-collection";

mongoose
.connect(MONGO_URI)
Expand Down
14 changes: 13 additions & 1 deletion models/Drone.model.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
// Iteration #1
// Iteration #1
const mongoose = require('mongoose');
const { Schema, model } = 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;
97 changes: 94 additions & 3 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,99 @@
/* General styles */
body {
padding: 50px;
font: 14px 'Lucida Grande', Helvetica, Arial, sans-serif;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
color: #333;
}

h1 {
text-align: center;
margin: 20px 0;
color: #444;
}

/* Navigation link */
a {
color: #00b7ff;
color: #007bff;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* Drone list */
ul {
list-style-type: none;
padding: 0;
}

li {
background-color: #fff;
margin: 10px auto;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
max-width: 600px;
display: flex;
justify-content: space-between;
align-items: center;
}

/* Buttons */
button {
background-color: #dc3545;
color: white;
border: none;
padding: 8px 12px;
border-radius: 3px;
cursor: pointer;
}

button:hover {
background-color: #c82333;
}

form {
display: inline;
}

/* Forms */
form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

form input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}

form button {
background-color: #28a745;
color: white;
padding: 10px 15px;
border: none;
border-radius: 3px;
cursor: pointer;
}

form button:hover {
background-color: #218838;
}

/* Centered form */
form {
max-width: 400px;
margin: 0 auto;
}

/* Container */
.container {
padding: 20px;
}
65 changes: 45 additions & 20 deletions routes/drones.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
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
const Drone = require('../models/Drone.model');
// GET /drones - List all drones
router.get('/drones', async (req, res, next) => {
try {
const drones = await Drone.find();
res.render('drones/list', { drones });
} catch (err) {
console.error('Error fetching drones:', err);
next(err);
}
});

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) => {
// Iteration #3: Add a new drone
// ... your code here
router.post('/drones/create', async (req, res, next) => {
try {
const { name, propellers, maxSpeed } = req.body;
await Drone.create({ name, propellers, maxSpeed });
res.redirect('/drones');
} catch (err) {
console.error('Error creating drone:', err);
res.render('drones/create-form', { error: 'Failed to create drone' });
}
});

router.get('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
router.get('/drones/:id/edit', async (req, res, next) => {
try {
const drone = await Drone.findById(req.params.id);
res.render('drones/update-form', { drone });
} catch (err) {
console.error('Error fetching drone for edit:', err);
next(err);
}
});

router.post('/drones/:id/edit', (req, res, next) => {
// Iteration #4: Update the drone
// ... your code here
router.post('/drones/:id/edit', async (req, res, next) => {
try {
const { name, propellers, maxSpeed } = req.body;
await Drone.findByIdAndUpdate(req.params.id, { name, propellers, maxSpeed });
res.redirect('/drones');
} catch (err) {
console.error('Error updating drone:', err);
res.render('drones/update-form', { error: 'Failed to update drone' });
}
});

router.post('/drones/:id/delete', (req, res, next) => {
// Iteration #5: Delete the drone
// ... your code here
router.post('/drones/:id/delete', async (req, res, next) => {
try {
await Drone.findByIdAndDelete(req.params.id);
res.redirect('/drones');
} catch (err) {
console.error('Error deleting drone:', err);
next(err);
}
});

module.exports = router;
23 changes: 22 additions & 1 deletion seeds/drones.seed.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
// Iteration #1
const mongoose = require('mongoose');
const Drone = require('../models/Drone.model'); // Import the Drone model

const drones = [
{ name: "Creeper XL 500", propellers: 3, maxSpeed: 12 },
{ name: "Racer 57", propellers: 4, maxSpeed: 20 },
{ name: "Courier 3000i", propellers: 6, maxSpeed: 18 },
];

mongoose
.connect('mongodb://127.0.0.1:27017/droneApp') // Connect to your MongoDB
.then(() => {
console.log('Connected to the database');
return Drone.create(drones); // Seed the drones into the database
})
.then((createdDrones) => {
console.log(`Seeded ${createdDrones.length} drones.`);
return mongoose.connection.close(); // Close the connection
})
.catch((err) => {
console.error('Error seeding drones:', err);
});
14 changes: 13 additions & 1 deletion views/drones/create-form.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<h2>Create a new drone</h2>
<h2>Create a new drone</h2>
<form action="/drones/create" method="POST">
<label>Name:</label>
<input type="text" name="name" required>

<label>Propellers:</label>
<input type="number" name="propellers" required>

<label>Max Speed:</label>
<input type="number" name="maxSpeed" required>

<button type="submit">Create</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>
<h1>Available Drones</h1>

<a href="/drones/create">Add a new drone</a>

<ul>
{{#each drones}}
<li>
<strong>Name:</strong> {{this.name}} <br>
<strong>Propellers:</strong> {{this.propellers}} <br>
<strong>Max Speed:</strong> {{this.maxSpeed}} m/s
<form action="/drones/{{this._id}}/delete" method="POST" style="display:inline;">
<button type="submit">Delete</button>
</form>
<a href="/drones/{{this._id}}/edit">Edit</a>
</li>
{{/each}}
</ul>
14 changes: 13 additions & 1 deletion views/drones/update-form.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<h2>Update the drone</h2>
<h2>Update the drone</h2>
<form action="/drones/{{drone._id}}/edit" method="POST">
<label>Name:</label>
<input type="text" name="name" value="{{drone.name}}" required>

<label>Propellers:</label>
<input type="number" name="propellers" value="{{drone.propellers}}" required>

<label>Max Speed:</label>
<input type="number" name="maxSpeed" value="{{drone.maxSpeed}}" required>

<button type="submit">Update</button>
</form>