From 770dbcb4b47fb9635c105bdddde4323bee487bd4 Mon Sep 17 00:00:00 2001 From: Gabe Chilson Date: Wed, 20 Oct 2021 12:12:23 -0600 Subject: [PATCH] finished MVP tasks --- api/cars/cars-middleware.js | 64 ++++++++++++++++++++++++--- api/cars/cars-model.js | 25 ++++++++--- api/cars/cars-router.js | 40 ++++++++++++++++- data/migrations/01-make_cars_table.js | 19 +++++++- package-lock.json | 1 - 5 files changed, 134 insertions(+), 15 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 7e738d6062..e52a3bd5d9 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,15 +1,67 @@ -const checkCarId = (req, res, next) => { - // DO YOUR MAGIC +const Car = require('./cars-model') +const vin = require('vin-validator') + +const checkCarId = async (req, res, next) => { + 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 = async(req, res, next) => { + 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) + } } -const checkVinNumberUnique = (req, res, next) => { - // DO YOUR MAGIC +module.exports = { + checkCarId, + checkCarPayload, + checkVinNumberValid, + checkVinNumberUnique } diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index 39b2c50a8d..83138e601e 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -1,11 +1,26 @@ +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 getByVin = (vin) => { + return db('cars').where('vin',vin).first() } -const create = () => { - // DO YOUR MAGIC +const create = (car) => { + return db('cars').insert(car) + .then(([id]) => + getById(id)) } + +module.exports = { + getAll, + getById, + create, + getByVin, + } diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 3fca0e7d7d..de450ec32d 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -1 +1,39 @@ -// DO YOUR MAGIC +const express = require('express') +const Car = require('./cars-model') +const { + checkCarId, + checkCarPayload, + checkVinNumberValid, + checkVinNumberUnique +} = require('./cars-middleware') + +const router = express.Router() + +router.get('/', async (req,res,next) => { + try{ + const cars= await Car.getAll() + res.json(cars) + } catch (err){ + next(err) + } +}) + +router.get('/:id', checkCarId, (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 diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index 9723c175be..73e409d1b2 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -1,7 +1,22 @@ 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) + + tbl.string('model', 256).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') }; diff --git a/package-lock.json b/package-lock.json index bd99a1ccee..cf8657c1b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,6 @@ "requires": true, "packages": { "": { - "name": "node-db2-project", "version": "1.0.0", "dependencies": { "express": "^4.17.1",