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
55 changes: 54 additions & 1 deletion api/cars/cars-middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
const Car = require('./cars-model')
var vinValidator = require('vin-validator');
// const db = require('../../data/db-config')


const checkCarId = (req, res, next) => {
// DO YOUR MAGIC
console.log('verifying id')
const { id } = req.params;
Car.getById(id)
.then(foundCar => {
if (!foundCar) {
res.status(404).json({ message: `car with id ${id} is not found` })
}
else {
next()
}
})
}

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

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

const checkVinNumberUnique = (req, res, next) => {
// DO YOUR MAGIC
Car.checkVinUnique(req.body.vin)
.then(response => {
if (response) {
res.status(400).json({ message: `vin ${req.body.vin} already exists` })
}
else {
next()
}
})
.catch(err => {
res.status(500).json({ message: err.message })
})

}

module.exports = {
checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique
}
22 changes: 20 additions & 2 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
const { default: knex } = require('knex')
const db = require('../../data/db-config')

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

}

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

const create = () => {
const create = async ({ vin, make, model, mileage, title, transmission }) => {
// DO YOUR MAGIC
const [id] = await db('cars').insert({ vin, make, model, mileage, title, transmission })
return getById(id)
}

const checkVinUnique = (vinParam) => {
return db('cars').where({ vin: vinParam }).first()
}
module.exports = {
getAll,
getById,
create,
checkVinUnique
}
42 changes: 42 additions & 0 deletions api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
// DO YOUR MAGIC
const router = require('express').Router()
const Car = require('./cars-model')
const { checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique } = require('./cars-middleware')

router.get('/api/cars', (req, res) => {
Car.getAll()
.then(allCars => {
res.status(200).json(allCars)
})
.catch(err => {
res.status(500).json({ message: err.message })
})
console.log('getting all cars')
})

router.get('/api/cars/:id', checkCarId, (req, res) => {
const { id } = req.params;
Car.getById(id)
.then(car => {
res.status(200).json(car)
})
.catch(err => {
res.status(400).json({ message: err.message })
})
})

router.post('/api/cars', checkVinNumberUnique, checkVinNumberValid, checkCarPayload, (req, res) => {
Car.create(req.body)
.then(newCar => {
res.status(201).json(newCar)
})
.catch(err => {
res.status(500).json({ message: err.message })
})
})



module.exports = router;
4 changes: 4 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const express = require("express")
const carsRouter = require('./cars/cars-router')

const server = express()

server.use(express.json())
server.use('/', carsRouter)

// DO YOUR MAGIC

module.exports = server
Binary file added data/dealer.db3
Binary file not shown.
16 changes: 14 additions & 2 deletions data/migrations/01-make_cars_table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
exports.up = function (knex) {
exports.up = async function (knex) {
return knex.schema.createTable("cars", tbl => {
tbl.increments()
tbl.text("vin").unique().notNullable()
tbl.text("make").notNullable()
tbl.text("model").notNullable()
tbl.decimal("mileage").notNullable()
tbl.text("title")
tbl.text("transmission")
})
// DO YOUR MAGIC
};

exports.down = function (knex) {
exports.down = async function (knex) {
// DO YOUR MAGIC
return knex.schema.dropTableIfExists('cars')
};


Loading