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
73 changes: 67 additions & 6 deletions api/cars/cars-middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
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: `car with id ${car} is 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 = (req, res, next) => {
// DO YOUR MAGIC
const checkVinNumberUnique = async (req, res, next) => {
try{
const existingVin = await Car.getByVin(req.body.vin)
if(!existingVin){
next()
}else{
next({
status: 400,
message: `vin ${req.body.vin} already exists`
})
}
}catch(err){
next(err)
}
}
module.exports = {
checkCarId,
checkCarPayload,
checkVinNumberUnique,
checkVinNumberValid,
}
36 changes: 31 additions & 5 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
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 getByVin = (vin) => {
return db('cars').where('vin', vin).first()
}

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

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

const getById = () => {
// DO YOUR MAGIC
const deleteById = (id) => {
return db('cars').where('id', id).del()
}

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

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,
checkVinNumberUnique,
checkVinNumberValid,
async (req, res, next) => {
try{
const newCar = await Car.create(req.body)
res.status(201).json(newCar)
}catch(err){
next(err)
}
})

router.put('/:id',
checkCarId,
checkCarPayload,
async (req, res, next) => {
try{
const updatedCar = await Car.updateById(req.params.id, req.body)
res.status(200).json(updatedCar)
}catch(err){
next(err)
}
})

router.delete('/:id', checkCarId, (req, res, next) => {
Car.deleteById(req.params.id)
.then(deletedCar => {
res.status(200).json(deletedCar)
})
.catch(err => {
next(err)
})
})

router.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,
stack: err.stack,
});
});
module.exports = router
16 changes: 14 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
const express = require("express")

const carsRouter = require('./cars/cars-router')
const server = express()

// DO YOUR MAGIC
server.use(express.json())
server.use('/api/cars', carsRouter)

server.use('*', (req, res, next) => {
next({
status: 404,
message: 'not found'
})
})
server.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message
})
})
module.exports = server
Binary file added data/dealer.db3
Binary file not shown.
12 changes: 10 additions & 2 deletions data/migrations/01-make_cars_table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
exports.up = function (knex) {
// DO YOUR MAGIC
return knex.schema.createTable('cars', (table) => {
table.increments()
table.string('vin', 16).notNullable().unique()
table.string('make', 128).notNullable()
table.string('model', 128).notNullable()
table.integer('mileage', 128).notNullable().unsigned()
table.string('title', 128)
table.string('transmission', 128)
})
};

exports.down = function (knex) {
// DO YOUR MAGIC
return knex.schema.dropTableIfExists('cars')
};
32 changes: 31 additions & 1 deletion data/seeds/01-cars.js
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
// STRETCH
const cars = [
{
vin: 'JTEBU11F670058710',
make: 'toyota',
model: 'yaris',
mileage: 235000,
title: 'clean',
transmission: 'manual'
},
{
vin: '1D7HA18287S191814',
make: 'jeep',
model: 'grand cherokee',
mileage: 115000,
title: 'clean',
transmission: 'automatic'
},
{
vin: '5UXXW3C58F0M65560',
make: 'ford',
model: 'bronco',
mileage: 250000,
title: 'clean',
transmission: 'automatic'
}
]

exports.seed = async function(knex){
await knex('cars').truncate()
await knex('cars').insert(cars)
}
29 changes: 20 additions & 9 deletions package-lock.json

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

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"test": "cross-env NODE_ENV=testing jest --verbose --runInBand"
"test": "cross-env NODE_ENV=testing jest --verbose --runInBand",
"migrate": "knex migrate:latest",
"rollback": "knex migrate:rollback",
"migup": "knex migrate:up",
"migdown": "knex migrate:down",
"seed": "knex seed:run"
},
"devDependencies": {
"@types/jest": "^27.0.3",
Expand All @@ -16,8 +22,9 @@
},
"dependencies": {
"express": "^4.17.1",
"knex": "^0.95.14",
"sqlite3": "^5.0.2"
"knex": "^0.95.15",
"sqlite3": "^5.0.2",
"vin-validator": "^1.0.0"
},
"repository": {
"type": "git",
Expand Down