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

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

const checkCarPayload = (req, res, next) => {
// DO YOUR MAGIC
const chechCarPayloadSchema = 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.number().required('mileage is missing'),
title: yup.string(),
transmission: yup.string()
})

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

const checkVinNumberValid = (req, res, next) => {
// DO YOUR MAGIC
next()
}

const checkVinNumberUnique = async (req, res, next) => {
try {
const exisiting = await db('cars')
.where('vin', req.body.vin)
.first()
if (!exisiting) {
next()
} else {
res.status(400).json({
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,
}
24 changes: 19 additions & 5 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@

const db = require('../../data/db-config')

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

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

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

module.exports = {
getAll,
getById,
create,
}
36 changes: 35 additions & 1 deletion api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
// DO YOUR MAGIC
const router = require('express').Router()
const Cars = require('./cars-model')
const { checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique,
} = 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', checkCarId, async (req, res) => {
res.status(200).json(req.car)
})

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

module.exports = router
13 changes: 10 additions & 3 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
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);

server.get('/', (req, res) => {
res.send(`<h1>CARS</h1>`)
});



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

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