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
64 changes: 58 additions & 6 deletions api/cars/cars-middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,67 @@
const checkCarId = (req, res, next) => {
// DO YOUR MAGIC
const Car = require('./cars-model')
const vin = require('vin-validator')

const checkCarId = async (req, res, next) => {
try{
const car = await Car.getById(req.params.id)
if(!car){
next({ status:404, message: 'not found'})
} else
req.car = car
next()
} catch(err){
next(err)
}
}

const checkCarPayload = (req, res, next) => {
// DO YOUR MAGIC
if(!req.body.vin) return next({
status:400,
message:'vin is missing',
})
if(!req.body.make) return next({
status:400,
message:'make is missing',
})
if(!req.body.model) return next({
status:400,
message:'model is missing',
})
if(!req.body.mileage) return next({
status:400,
message:'mileage is missing',
})
next()
}


const checkVinNumberValid = (req, res, next) => {
// DO YOUR MAGIC
if(vin.validate(req.body.vin)){
next()
} else{
next({
status:400,
message:`vin ${req.body.vin} is invalid`
})
}
}

const checkVinNumberUnique = async(req, res, next) => {
try{
const existing = await Car.getByVin(req.body.vin)
if(!existing) {
next()
} else {
next({ status:400, message: `vin ${req.body.vin} already exists`})
}
} catch(err) {
next(err)
}
}

const checkVinNumberUnique = (req, res, next) => {
// DO YOUR MAGIC
module.exports = {
checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique
}
25 changes: 20 additions & 5 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
const db = require('../../data/db-config')

const getAll = () => {
// DO YOUR MAGIC
return db('cars')
}

const getById = (id) => {
return db('cars').where('id', id).first()
}

const getById = () => {
// DO YOUR MAGIC
const getByVin = (vin) => {
return db('cars').where('vin',vin).first()
}

const create = () => {
// DO YOUR MAGIC
const create = (car) => {
return db('cars').insert(car)
.then(([id]) =>
getById(id))
}

module.exports = {
getAll,
getById,
create,
getByVin,
}
40 changes: 39 additions & 1 deletion api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
// DO YOUR MAGIC
const express = require('express')
const Car = require('./cars-model')
const {
checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique
} = require('./cars-middleware')

const router = express.Router()

router.get('/', async (req,res,next) => {
try{
const cars= await Car.getAll()
res.json(cars)
} catch (err){
next(err)
}
})

router.get('/:id', checkCarId, (req,res,next) => {
res.json(req.car)
})

router.post(
'/',
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique,
async( req, res, next) => {
try {
const car = await Car.create(req.body)
res.json(car)
} catch(err) {
next(err)
}
})

module.exports = router
19 changes: 17 additions & 2 deletions data/migrations/01-make_cars_table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
exports.up = function (knex) {
// DO YOUR MAGIC
return knex.schema.createTable('cars', tbl => {
tbl.increments()

tbl.string('vin', 17). notNullable().unique()

tbl.string('make', 128)

tbl.string('model', 256).notNullable()

tbl.integer('mileage').unsigned().notNullable()

tbl.string('title', 128)

tbl.string('transmission', 128)
})
};


exports.down = function (knex) {
// DO YOUR MAGIC
return knex.schema.dropTableIfExists('cars')
};
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.