Skip to content

Commit

Permalink
Refactored the code to use routers. Closes #18.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peppe L-G committed Oct 5, 2021
1 parent c7bdc8a commit 0d1bf87
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 211 deletions.
214 changes: 5 additions & 209 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require('express')
const expressHandlebars = require('express-handlebars')
const expressSession = require('express-session')
const validators = require('./validators')
const db = require('./database')
const productRouter = require('./routers/product-router')
const authRouter = require('./routers/auth-router')

const app = express()

Expand All @@ -29,6 +29,9 @@ app.engine('hbs', expressHandlebars({
defaultLayout: 'main.hbs'
}))

app.use('/products', productRouter)
app.use('/auth', authRouter)

app.get('/', function(request, response){
response.render('start.hbs')
})
Expand All @@ -37,211 +40,4 @@ app.get('/about', function(request, response){
response.render('about.hbs')
})

app.get('/products', function(request, response){

db.getAllProducts(function(error, products){

if(error){

const model = {
hasDatabaseError: true,
products: []
}
response.render('products.hbs', model)

}else{

const model = {
hasDatabaseError: false,
products
}
response.render('products.hbs', model)

}

})

})

app.get('/products/create', function(request, response){
response.render('create-product.hbs')
})

app.post('/products/create', function(request, response){

const name = request.body.name
const description = request.body.description

const errors = validators.getValidationErrorsForProduct(name, description)

if(!request.session.isLoggedIn){
errors.push("Not logged in.")
}

if(errors.length == 0){

db.createProduct(name, description, function(error, productId){

if(error){

errors.push("Internal server error.")

const model = {
errors,
name,
description
}

response.render('create-product.hbs', model)

}else{

response.redirect('/products/'+productId)

}

})

}else{

const model = {
errors,
name,
description
}

response.render('create-product.hbs', model)

}

})

// /products/3
app.get('/products/:id', function(request, response){

const id = request.params.id

db.getProductById(id, function(error, product){
// TODO: Handle error.
const model = {
product
}
response.render('product.hbs', model)
})

})

app.get('/products/:id/update', function(request, response){

const id = request.params.id

db.getProductById(id, function(error, product){

// TODO: Handle error.

const model = {
product
}

response.render('update-product.hbs', model)

})

})

app.post('/products/:id/update', function(request, response){

const id = request.params.id
const name = request.body.name
const description = request.body.description

const errors = validators.getValidationErrorsForProduct(name, description)

if(!request.session.isLoggedIn){
errors.push("Not logged in.")
}

if(errors.length == 0){

db.updateProductById(id, name, description, function(error){

// TODO: Handle error.

response.redirect('/products/'+id)

})

}else{

const model = {
errors,
product: {
id,
name,
description
}
}

response.render('update-product.hbs', model)

}

})

app.get('/products/:id/delete', function(request, response){

const id = request.params.id

db.getProductById(id, function(error, product){

// TODO: Handle error.

const model = {
product
}

response.render('delete-product.hbs', model)

})

})

app.post('/products/:id/delete', function(request, response){

const id = request.params.id

// TODO: Check if the user is logged in, and only carry
// out the request if the user is.

db.deleteProductById(id, function(error){

// TODO: Handle error.

response.redirect('/products')

})

})

app.get('/login', function(request, response){
response.render('login.hbs')
})

app.post('/login', function(request, response){

const username = request.body.username
const password = request.body.password

// TODO: Don't use hardcoded values.
if(username == 'Alice' && password == 'abc123'){
request.session.isLoggedIn = true
// TODO: Do something better than redirecting to start page.
response.redirect('/')
}else{
// TODO: Display error message to the user.
response.render('login.hbs')
}

})

app.listen(8080)
28 changes: 28 additions & 0 deletions routers/auth-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require('express')

const ADMIN_USERNAME = 'Alice'
const ADMIN_PASSWORD = 'abc123'

const router = express.Router()

router.get('/login', function(request, response){
response.render('login.hbs')
})

router.post('/login', function(request, response){

const username = request.body.username
const password = request.body.password

if(username == ADMIN_USERNAME && password == ADMIN_PASSWORD){
request.session.isLoggedIn = true
// TODO: Do something better than redirecting to start page.
response.redirect('/')
}else{
// TODO: Display error message to the user.
response.render('login.hbs')
}

})

module.exports = router
Loading

0 comments on commit 0d1bf87

Please sign in to comment.