Skip to content

Commit

Permalink
Merge pull request #1 from Electiventure/Try-first-for-approve
Browse files Browse the repository at this point in the history
Thing to Measurement
  • Loading branch information
Electiventure authored Mar 11, 2024
2 parents 19016e5 + 65c6652 commit 010fdc1
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 99 deletions.
6 changes: 3 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const expressHandlebars = require('express-handlebars').create({ /* your configu
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const session = require('express-session');
const User = require('./models/user'); // Create this model for user authentication
const User = require('./models/User'); // Create this model for user authentication

const app = express();

Expand Down Expand Up @@ -34,8 +34,8 @@ passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// Include the 'Thing' model
const Thing = require('./models/thing'); // Corrected lowercase 'thing' here
// Include the 'Measurement' model
const Measurement = require('./models/measurement'); //

// Routes setup (to be implemented later)
const indexRoutes = require('./routes/index');
Expand Down
14 changes: 7 additions & 7 deletions models/Thing.js → models/Measurement.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// models/thing.js
// models\Measurement.js
const mongoose = require('mongoose');

// Define the 'Thing' schema
const thingSchema = new mongoose.Schema({
// Define the 'Measurement' schema
const measurementSchema = new mongoose.Schema({
place: {
type: String,
required: true,
Expand All @@ -26,8 +26,8 @@ const thingSchema = new mongoose.Schema({
},
});

// Create the 'Thing' model based on the schema
const Thing = mongoose.model('Thing', thingSchema);
// Create the 'Measurement' model based on the schema
const Measurement = mongoose.model('Measurement', measurementSchema);

// Export the 'Thing' model for use in other parts of the application
module.exports = Thing;
// Export the 'Measurement' model for use in other parts of the application
module.exports = Measurement;
56 changes: 28 additions & 28 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const express = require('express');
const passport = require('passport');
const router = express.Router();
const User = require('../models/user');
const Thing = require('../models/thing');
const User = require('../models/User');
const Measurement = require('../models/measurement');

// Register
router.get('/register', (req, res) => {
Expand Down Expand Up @@ -62,38 +62,38 @@ router.get('/', (req, res) => {



// CRUD operations for 'Thing' model
// CRUD operations for 'Measurement' model
// routes/index.js

// ... (existing code)

// CRUD operations for 'Thing' model
router.get('/things', async (req, res) => {
// CRUD operations for 'Measurement' model
router.get('/measurements', async (req, res) => {
try {
const things = await Thing.find({});
res.render('things/index', { things });
const measurements = await Measurement.find({});
res.render('measurements/index', { measurements });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});

router.get('/things/new', (req, res) => {
res.render('things/new');
router.get('/measurements/new', (req, res) => {
res.render('measurements/new');
});


//Create new thing// Inside your POST route for creating a new 'Thing'
router.post('/things', (req, res) => {
//Create new measurement// Inside your POST route for creating a new 'Measurement'
router.post('/measurements', (req, res) => {
// Ensure required fields are provided in the request body
if (!req.body.thing.place || !req.body.thing.date || !req.body.thing.value || !req.body.thing.type) {
if (!req.body.measurement.place || !req.body.measurement.date || !req.body.measurement.value || !req.body.measurement.type) {
return res.status(400).send('All fields are required');
}

// Create the 'Thing'
Thing.create(req.body.thing)
.then(newThing => {
res.redirect('/things');
// Create the 'Measurement'
Measurement.create(req.body.measurement)
.then(newMeasurement => {
res.redirect('/measurements');
})
.catch(err => {
console.error(err);
Expand All @@ -105,44 +105,44 @@ router.post('/things', (req, res) => {


// Read......!!
router.get('/things/:id', async (req, res) => {
router.get('/measurements/:id', async (req, res) => {
try {
const foundThing = await Thing.findById(req.params.id);
res.render('things/show', { thing: foundThing });
const foundMeasurement = await Measurement.findById(req.params.id);
res.render('measurements/show', { measurement: foundMeasurement });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});

// Edit......!!
router.get('/things/:id/edit', async (req, res) => {
router.get('/measurements/:id/edit', async (req, res) => {
try {
// Ensure req.params.id is properly handled
const foundThing = await Thing.findById(req.params.id);
res.render('things/edit', { thing: foundThing });
const foundMeasurement = await Measurement.findById(req.params.id);
res.render('measurements/edit', { measurement: foundMeasurement });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});

//Update
router.put('/things/:id', async (req, res) => {
router.put('/measurements/:id', async (req, res) => {
try {
await Thing.findByIdAndUpdate(req.params.id, req.body.thing);
res.redirect('/things/' + req.params.id);
await Measurement.findByIdAndUpdate(req.params.id, req.body.measurement);
res.redirect('/measurements/' + req.params.id);
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});

//Delete
router.delete('/things/:id', async (req, res) => {
router.delete('/measurements/:id', async (req, res) => {
try {
await Thing.findByIdAndRemove(req.params.id);
res.redirect('/things');
await Measurement.findByIdAndRemove(req.params.id);
res.redirect('/measurements');
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
Expand Down
10 changes: 5 additions & 5 deletions views/index.handlebars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- views/things/index.handlebars -->
<!-- views/measurements/index.handlebars -->
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -8,13 +8,13 @@
</head>
<body>
<h1>List of Measurements</h1>
{{#if things.length}}
{{#if measurements.length}}
<ul>
{{#each things}}
{{#each measurements}}
<li>
<strong>{{this.place}}</strong> - {{this.date}} - {{this.value}} - {{this.type}}
<a href="/things/{{this._id}}">View</a>
<a href="/things">View Measurements</a>
<a href="/measurements/{{this._id}}">View</a>
<a href="/measurements">View Measurements</a>
</li>
{{/each}}
</ul>
Expand Down
9 changes: 4 additions & 5 deletions views/layouts/main.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
<ul class="navbar-nav">
{{#if user}}
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/things/new">Add Measurement</a></li>
<li class="nav-item"><a class="nav-link" href="/things/show">View Measurements</a></li>
<li class="nav-item"><a class="nav-link" href="/things/index">List of Measurements</a></li>
<li class="nav-item"><a class="nav-link" href="/things/edit">Update Measurement</a></li>
<li class="nav-item"><a class="nav-link" href="/measurements/new">Add Measurement</a></li>
<li class="nav-item"><a class="nav-link" href="/measurements/show">View Measurements</a></li>
<li class="nav-item"><a class="nav-link" href="/measurements/index">List of Measurements</a></li>
<li class="nav-item"><a class="nav-link" href="/measurements/edit">Update Measurement</a></li>
<li class="nav-item"><a class="nav-link" href="/logout">Logout</a></li>
{{else}}
<li class="nav-item"><a class="nav-link" href="/login">Login</a></li>
Expand All @@ -37,7 +37,6 @@
</div>
</nav>
</header>

<div>
<nav>
<h5 align="center">This is the navigation section.</h5>
Expand Down
40 changes: 40 additions & 0 deletions views/measurements/edit.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Measurement</title>
</head>

<body>
<h1>Edit Measurement</h1>

<form action="/measurements/{{measurement._id}}?_method=PUT" method="POST">
<label for="place">Place:</label>
<input type="text" id="place" name="measurement[place]" value="{{measurement.place}}" required>

<label for="date">Date:</label>
<input type="text" id="date" name="measurement[date]" value="{{measurement.date}}" required>

<label for="value">Value:</label>
<input type="number" id="value" name="measurement[value]" value="{{measurement.value}}" step="0.1" required>

<label for="type">Type:</label>
<input type="text" id="type" name="measurement[type]" value="{{measurement.type}}" required>

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

<!-- Ensure measurement._id is defined before generating the link -->
{{#if measurement._id}}
<!-- Use Express's url function to construct the URL -->
<a href="{{url '/measurements/' measurement._id '/edit'}}">Edit Measurement</a>
{{else}}
<p>Measurement _id is not defined</p>
{{/if}}

<a href="/measurements/{{measurement._id}}">Cancel</a>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
<body>
<h1>List of Measurements</h1>

{{#if things.length}}
{{#if measurements.length}}
<ul>
{{#each things}}
{{#each measurements}}
<li>
<strong>{{place}}</strong> - {{date}} - {{value}} - {{type}}
<a href="/things/{{_id}}">View</a>
<a href="/things/{{_id}}/edit">Edit</a>
<form action="/things/{{_id}}?_method=DELETE" method="POST">
<a href="/measurements/{{_id}}">View</a>
<a href="/measurements/{{_id}}/edit">Edit</a>
<form action="/measurements/{{_id}}?_method=DELETE" method="POST">
<!-- Include CSRF token if applicable -->
<input type="hidden" name="_csrf" value="{{csrfToken}}">
<button type="submit" onclick="return confirm('Are you sure?')">Delete</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body align ="center">
<h1>New Measurement</h1>

<form action="/things" method="POST">
<form action="/measurements" method="POST">
<label for="place">Place:</label>
<input type="text" id="place" name="measurement[place]" required>
<br><br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<h1>Measurement Details</h1>

<p>
<strong>Place:</strong> {{thing.place}}<br>
<strong>Date:</strong> {{thing.date}}<br>
<strong>Value:</strong> {{thing.value}}<br>
<strong>Type:</strong> {{thing.type}}
<strong>Place:</strong> {{measurement.place}}<br>
<strong>Date:</strong> {{measurement.date}}<br>
<strong>Value:</strong> {{measurement.value}}<br>
<strong>Type:</strong> {{measurement.type}}
</p>

<a href="/things">Back to List</a>
<a href="/measurements">Back to List</a>
</body>

</html>
40 changes: 0 additions & 40 deletions views/things/edit.handlebars

This file was deleted.

0 comments on commit 010fdc1

Please sign in to comment.