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
17 changes: 14 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": "latest"
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 13
},
"plugins": [
"react"
],
"rules": {
"semi": 0
}
}
65 changes: 63 additions & 2 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) => {
const Car = require('./cars-model')
const vin = require("vin-validator")

const checkCarId = async (req, res, next) => {
// DO YOUR MAGIC
try {
const car = await Car.getById(req.params.id)
if(!car){
next({status: 404, message: '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) => {
const checkVinNumberUnique = async (req, res, next) => {
// DO YOUR MAGIC
try {
const existing = await Car.getByVin(req.body.vin)
if (!existing) {
next()
}else{
next({
status: 400,
message: `vin ${req.body.vin} already exists`
})
}
}catch(err){
next(err)
}

}

module.exports = {
checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique,
}
26 changes: 23 additions & 3 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
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 = () => {
const getByVin = (vin) => {
return db('cars').where('vin', vin).first()
}


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

module.exports = {
getAll,
getById,
create,
getByVin,
}
44 changes: 44 additions & 0 deletions api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
// DO YOUR MAGIC
const express = require('express')
const Car = require('./cars-model')
const {
checkCarId,
checkCarPayload,
checkVinNumberValid,
checkVinNumberUnique
} = require('./cars-middleware')
// Not sure what this does so i took it out for now
//const res = require('express/lib/response')

const router = express.Router()

// end points


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

router.get('/:id', checkCarId, async(req, res, next) => {
res.json(req.car)
})

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

module.exports = router
20 changes: 20 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ const express = require("express")

const server = express()

const carsRouter = require("./cars/cars-router")

// DO YOUR MAGIC
server.use(express.json())

server.use('/api/cars', carsRouter)

server.get("/", (_, res) => {
res.status(200).json({ message: "server running" });
});


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.
11 changes: 11 additions & 0 deletions data/migrations/01-make_cars_table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
exports.up = function (knex) {
// DO YOUR MAGIC
return knex.schema.createTable('cars', tbl => {
tbl.increments()

tbl.string('vin', 17).notNullable().unique()
tbl.string('make', 128).notNullable()
tbl.string('model',128).notNullable()
tbl.integer('mileage').unsigned().notNullable()
tbl.string('title', 128)
tbl.string('transmission', 128)
})
};

exports.down = function (knex) {
// DO YOUR MAGIC
return knex.schema.dropTableIfExists('cars')
};
39 changes: 38 additions & 1 deletion data/seeds/01-cars.js
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
// STRETCH
const cars = [
{
vin: '1D4HR38N13F581006',
make: 'toyota',
model: 'corolla',
mileage: 432000,
title: 'clean',
transmission: 'manual'
},
{
vin: 'ZCFJS7458M1953433',
make: 'toyota',
model: 'prius',
mileage: 32000,
title: 'salvage',

},
{
vin: 'SCBBR53W36C034889',
make: 'ford',
model: 'mustang',
mileage: 2000

},
]

exports.seed = function(knex){
return knex('cars')
.truncate()
.then(()=> {
return knex('cars').insert(cars)
})
}

exports.seed = async function(knex){
await knex('cars').truncate()
await knex('cars').insert(cars)
}
Loading