Skip to content
Open

mvp #600

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
40 changes: 40 additions & 0 deletions api/cars/cars-middleware.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
const Car = require('./cars-model')


const checkCarId = (req, res, next) => {
// DO YOUR MAGIC
try {
const car = await Car.getById(req.param)
if(car) {
req.car = car
next()
}
} catch(err) {
res.json({message: err.message})
}
}

const checkCarPayload = (req, res, next) => {
// DO YOUR MAGIC
const { vin, make, model, mileage, title, transmission } = req.body

if (vin === undefined || make === undefined || model === undefined || mileage === undefined || title === undefined || transmission === undefined ) {
res.status(400).json({message: `${req.body} is required`})
}else {
next()
}
}

const checkVinNumberValid = (req, res, next) => {
Expand All @@ -12,4 +31,25 @@ const checkVinNumberValid = (req, res, next) => {

const checkVinNumberUnique = (req, res, next) => {
// DO YOUR MAGIC
try {
const existing = await db('cars')
.where('vin', req.body.name.trim())
.first()

if (existing) {
next({ status: 400, messae: 'that vin is taken'})
} else {
next()
}
} catch (err) {
next(err)
}
}


module.exports = {
checkCarId,
checkCarPayload,
checkVinNumberUnique,
checkVinNumberValid
}
8 changes: 7 additions & 1 deletion api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const db = require('../../data/db-config')

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

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

const create = () => {
const create = ({ vin, make, model, mileage, title, transmission }) => {
// DO YOUR MAGIC
const [id] = await db('accounts').insert({vin, make, model, mileage, title, transmission})
return getById(id)
}
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() // primary key
tbl.text('vin', 128).unique().notNullable()
tbl.string('make', 128).notNullable()
tbl.string('model', 128).notNullable()
tbl.string('mileage', 128).notNullable()
tbl.string('title', 128)
tbl.string('transmission', 128)
})
};

exports.down = function (knex) {
// DO YOUR MAGIC
return knex.schema.dropTableIfExists("fruits")
};