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
63 changes: 55 additions & 8 deletions api/cars/cars-middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
const checkCarId = (req, res, next) => {
// DO YOUR MAGIC
const Cars = require('./cars-model')
const vinValidator = require('vin-validator')
const yup = require('yup')
const db = require('../../data/db-config')

const checkCarId = async (req, res, next) => {
try{
const car = await Cars.getById(req.params.id)
if(!car){
next({message: `Car with id ${id} is not found`})
}else{
req.car = car
next()
}
}catch(err){
next(err)
}
}

const checkCarPayload = (req, res, next) => {
// DO YOUR MAGIC
const checkCarPayloadSchema = yup.object().shape({
vin: yup.string().required('vin is missing'),
make: yup.string().required('make is missing'),
model: yup.string().required('model is missing'),
mileage: yup.string().required('mileage is missing'),
title: yup.string(),
transmission: yup.string()
})

const checkCarPayload = async (req, res, next) => {
try{
const payValidation = await checkCarPayloadSchema.validate(req.body, {strict: false, stripUnkown: true})
req.body = payValidation
next()
}catch(err){
res.status(400).json({message: err.message})
}
}

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

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

module.exports = {checkCarId, checkCarPayload, checkVinNumberUnique, checkVinNumberValid}
21 changes: 16 additions & 5 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
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 create = async (car) => {
const [id] = await db('cars').insert(car)
return getById(id)
}

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

module.exports = {getAll, getById, create, getByVin}
33 changes: 32 additions & 1 deletion api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
// DO YOUR MAGIC
const router = require('express').Router()
const Cars = require('./cars-model')
const mw = require('./cars-middleware')

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

router.get('/:id', mw.checkCarId, async (req, res) => {
const car = await Cars.getById(req.params.id)
res.json(car)
})

router.post('/', mw.checkCarPayload, mw.checkVinNumberUnique, mw.checkVinNumberValid, async (req, res, next) => {
try{
const newCar = await Cars.create(req.body)
res.status(201).json(newCar)
}catch(err){
next(err)
}
})

router.use((err, req, res, next) => {
res.status(err.status || 404).json({message: err.message})
})

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

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

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

module.exports = server
module.exports = server
16 changes: 12 additions & 4 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
exports.up = async function (knex) {
await knex.schema.createTable('cars', table => {
table.increments()
table.text('vin', 17).unique().notNullable()
table.text('make', 128).notNullable()
table.text('model', 128).notNullable()
table.integer('mileage').notNullable()
table.text('title', 128)
table.text('transmission', 128)
})
};

exports.down = function (knex) {
// DO YOUR MAGIC
exports.down = async function (knex) {
await knex.schema.dropTableIfExists('cars')
};
1 change: 0 additions & 1 deletion data/seeds/01-cars.js
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
// STRETCH
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const server = require('./api/server')

const port = process.env.PORT || 9000;
const port = process.env.PORT || 5000;

server.listen(port, () => console.log(`\n** Running on port ${port} **\n`))
Loading