From ed63ec954045cbc394157cb0ae823f57f92bc0be Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sun, 14 Nov 2021 12:34:36 -0800 Subject: [PATCH 01/11] migrations and server set up --- api/server.js | 4 +++- data/migrations/01-make_cars_table.js | 10 ++++++++++ data/seeds/01-cars.js | 14 +++++++++++++- package-lock.json | 1 - 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/api/server.js b/api/server.js index c5a0be1b41..a36d675f36 100644 --- a/api/server.js +++ b/api/server.js @@ -1,7 +1,9 @@ const express = require("express") +const carsRouter = require('./cars/cars-router') const server = express() - // DO YOUR MAGIC +server.use(express.json()) +server.use('/api/cars', carsRouter) module.exports = server diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index 9723c175be..c525df545f 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -1,7 +1,17 @@ exports.up = function (knex) { // DO YOUR MAGIC + return knex.schema.createTable('cars', (table) => { + table.increments() + table.text('vin').notNullable().unique() + table.text('make').notNullable() + table.text('model').notNullable() + table.integer('mileage').notNullable() + table.text('title') + table.text('transmision') + }) }; exports.down = function (knex) { // DO YOUR MAGIC + return knex.schema.dropTableIfExists('cars') }; diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index 3f7acdde23..a730e7dcc2 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -1 +1,13 @@ -// STRETCH + +exports.seed = function(knex) { + // Deletes ALL existing entries + return knex('cars').truncate() + .then(function () { + // Inserts seed entries + return knex('cars').insert([ + {id: 1, colName: 'rowValue1'}, + {id: 2, colName: 'rowValue2'}, + {id: 3, colName: 'rowValue3'} + ]); + }); +}; \ No newline at end of file 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", From cb373e9f1c68ba01acbf7c3a12fc9b80087eb088 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sun, 14 Nov 2021 12:50:03 -0800 Subject: [PATCH 02/11] middleware created --- api/cars/cars-middleware.js | 70 +++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 7e738d6062..239163eaad 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,15 +1,81 @@ -const checkCarId = (req, res, next) => { +const db = require('../../data/db-config') +const Cars = require('./cars-model') +const vinValidator = require('vin-validator') + +const checkCarId = async (req, res, next) => { // DO YOUR MAGIC + try{ + const car = await Cars.getById(req.params.id) + if (car) { + req.car = car + next() + } else { + next({ + status: 404, + message: `The car with the specified id of ${req.params.id} is not found`, + }) + } + } catch (error) { + next(error) + } } const checkCarPayload = (req, res, next) => { // DO YOUR MAGIC + const { vin, make, model, mileage } = req.body + if (!vin) { + next({ + status: 404, + message: 'The VIN is missing' + }) + } else if (!make) { + next({ + status: 404, + message: 'The make of the vehicle is missing' + }) + } else if (!model) { + next({ + status: 404, + message: 'The model of the vehicle is missing' + }) + } else if (!mileage) { + next({ + status: 404, + message: 'The mileage of the is missing' + }) + } else { + next() + } } const checkVinNumberValid = (req, res, next) => { // DO YOUR MAGIC + const isVinValid = vinValidator(req.body.vin) + if (isVinValid) { + next() + } else { + next({ + status: 400, + message: `The VIN ${req.body.vin} is not a valid VIN` + }) + } } -const checkVinNumberUnique = (req, res, next) => { +const checkVinNumberUnique = async (req, res, next) => { // DO YOUR MAGIC + const { vin } = req.body + const vinMatch = await db('cars').where('name', vin.trim()).first() + + if (vinMatch) { + next({ + status: 400, + message: `The VIN ${vin} already exists` + }) + } } +module.exports = { + checkCarId, + checkCarPayload, + checkVinNumberUnique, + checkVinNumberValid, +} \ No newline at end of file From d9a0e5d53ca0d8a66dc6e1a7c3268127a1c5029b Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sun, 14 Nov 2021 12:56:09 -0800 Subject: [PATCH 03/11] cars-model complete --- api/cars/cars-model.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index 39b2c50a8d..afca656da8 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -1,11 +1,25 @@ -const getAll = () => { +const db = require('../../data/db-config') + +const getAll = async () => { // DO YOUR MAGIC + const result = await db('cars') + return result } -const getById = () => { +const getById = async () => { // DO YOUR MAGIC + const result = await db('cars').where('id', id).first() + return result } -const create = () => { +const create = async () => { // DO YOUR MAGIC + const [ id ] = await db('cars').insert(car) + const post = await getById(id) + return post } +module.exports = { + getAll, + getById, + create +} \ No newline at end of file From 19feed28ab5eb792fb49f0af7dee0f715b0405fe Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 16:03:06 -0800 Subject: [PATCH 04/11] starting over. created seeds , installed vin validator --- .eslintrc.json | 7 ++-- api/cars/cars-middleware.js | 64 ++++--------------------------------- api/cars/cars-router.js | 38 ++++++++++++++++++++++ data/dealer.db3 | 0 data/seeds/01-cars.js | 41 +++++++++++++++++------- package-lock.json | 13 +++++++- package.json | 7 ++-- 7 files changed, 93 insertions(+), 77 deletions(-) create mode 100644 data/dealer.db3 diff --git a/.eslintrc.json b/.eslintrc.json index a0cbc54ccc..aac5302e02 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,13 +1,12 @@ { "env": { + "browser": true, "commonjs": true, - "es2021": true, - "node": true, - "jest": true + "es2021": true }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": "latest" + "ecmaVersion": 13 }, "rules": { } diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 239163eaad..c2b828daa1 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,77 +1,25 @@ const db = require('../../data/db-config') const Cars = require('./cars-model') -const vinValidator = require('vin-validator') +// const vinValidator = require('vin-validator') const checkCarId = async (req, res, next) => { // DO YOUR MAGIC - try{ - const car = await Cars.getById(req.params.id) - if (car) { - req.car = car - next() - } else { - next({ - status: 404, - message: `The car with the specified id of ${req.params.id} is not found`, - }) - } - } catch (error) { - next(error) - } + } const checkCarPayload = (req, res, next) => { // DO YOUR MAGIC - const { vin, make, model, mileage } = req.body - if (!vin) { - next({ - status: 404, - message: 'The VIN is missing' - }) - } else if (!make) { - next({ - status: 404, - message: 'The make of the vehicle is missing' - }) - } else if (!model) { - next({ - status: 404, - message: 'The model of the vehicle is missing' - }) - } else if (!mileage) { - next({ - status: 404, - message: 'The mileage of the is missing' - }) - } else { - next() - } + } -const checkVinNumberValid = (req, res, next) => { +const checkVinNumberValid = async (req, res, next) => { // DO YOUR MAGIC - const isVinValid = vinValidator(req.body.vin) - if (isVinValid) { - next() - } else { - next({ - status: 400, - message: `The VIN ${req.body.vin} is not a valid VIN` - }) - } + } const checkVinNumberUnique = async (req, res, next) => { // DO YOUR MAGIC - const { vin } = req.body - const vinMatch = await db('cars').where('name', vin.trim()).first() - - if (vinMatch) { - next({ - status: 400, - message: `The VIN ${vin} already exists` - }) - } + } module.exports = { checkCarId, diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 3fca0e7d7d..3d358e26fe 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -1 +1,39 @@ // DO YOUR MAGIC +const router = require('express').Router() +const { + checkCarId, + checkCarPayload, + checkVinNumberUnique, + checkVinNumberValid, +} = require('./cars-middleware') +const Cars = require('./cars-model') + +router.get('/', async (req, res, next) => { + try{ + const data = await Cars.getAll() + res.json(data) + } catch (error) { + next(error) + } +}) + +router.get('/:id', checkCarId, (req, res, next) => { + res.json(req.car) +}) + +router.post('/', checkCarPayload, checkVinNumberUnique, checkVinNumberValid, async (req, res, next) => { + try{ + const newCar = await Cars.create(req.body) + res.status(201).json(newCar) + }catch (error) { + next(error) + } +}) + +router.use((err, req, res, next) => { + res.status(err.status || 500).json({ + message: err.message, + stack: err.stack, + }); + }); + module.exports = router \ No newline at end of file diff --git a/data/dealer.db3 b/data/dealer.db3 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index a730e7dcc2..10a7f80809 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -1,13 +1,30 @@ +const { default: knex } = require("knex") -exports.seed = function(knex) { - // Deletes ALL existing entries - return knex('cars').truncate() - .then(function () { - // Inserts seed entries - return knex('cars').insert([ - {id: 1, colName: 'rowValue1'}, - {id: 2, colName: 'rowValue2'}, - {id: 3, colName: 'rowValue3'} - ]); - }); -}; \ No newline at end of file +const cars = [ + { + vin: '1111111111111111', + make: 'toyota', + model: 'yaris', + mileage: 246000, + title: 'clean', + transmision: 'manual' + }, + { + vin: '1111111111111111', + make: 'jeep', + model: 'grand cherokee', + mileage: 115000, + title: 'clean', + }, + { + vin: '1111111111111111', + make: 'ford', + model: 'bronco', + mileage: 250000, + } +] + +exports.seed = async function(knex){ + await knex('cars').truncate() + await knex('cars').insert(cars) +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cf8657c1b2..80757a50f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "dependencies": { "express": "^4.17.1", "knex": "^0.95.11", - "sqlite3": "^5.0.2" + "sqlite3": "^5.0.2", + "vin-validator": "^1.0.0" }, "devDependencies": { "@types/jest": "^27.0.2", @@ -6911,6 +6912,11 @@ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", "optional": true }, + "node_modules/vin-validator": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/vin-validator/-/vin-validator-1.0.0.tgz", + "integrity": "sha1-F2Via4VYskAvd7bCXHYX9zzsrmc=" + }, "node_modules/w3c-hr-time": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz", @@ -12587,6 +12593,11 @@ } } }, + "vin-validator": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/vin-validator/-/vin-validator-1.0.0.tgz", + "integrity": "sha1-F2Via4VYskAvd7bCXHYX9zzsrmc=" + }, "w3c-hr-time": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz", diff --git a/package.json b/package.json index e5d2cd615a..874d1980d4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "main": "index.js", "scripts": { "server": "nodemon index.js", - "test": "cross-env NODE_ENV=testing jest --verbose --runInBand" + "test": "cross-env NODE_ENV=testing jest --verbose --runInBand", + "migrate": "knex migrate:latest", + "seed": "knex seed:run" }, "devDependencies": { "@types/jest": "^27.0.2", @@ -17,7 +19,8 @@ "dependencies": { "express": "^4.17.1", "knex": "^0.95.11", - "sqlite3": "^5.0.2" + "sqlite3": "^5.0.2", + "vin-validator": "^1.0.0" }, "repository": { "type": "git", From d96cd4e69cb0cd62c2fa37bb1b04ea40dafbf516 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 16:10:53 -0800 Subject: [PATCH 05/11] exports.up created --- data/migrations/01-make_cars_table.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index c525df545f..2dfa10ef6e 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -2,12 +2,12 @@ exports.up = function (knex) { // DO YOUR MAGIC return knex.schema.createTable('cars', (table) => { table.increments() - table.text('vin').notNullable().unique() - table.text('make').notNullable() - table.text('model').notNullable() - table.integer('mileage').notNullable() - table.text('title') - table.text('transmision') + table.string('vin', 16).notNullable().unique() + table.string('make', 128).notNullable() + table.string('model', 128).notNullable() + table.numeric('mileage').unsigned.notNullable() + table.string('title', 128) + table.string('transmision', 128) }) }; From f2493fe47e8166f1670846090501df2b4c7da1a5 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 16:25:17 -0800 Subject: [PATCH 06/11] added migrate commands and updated database --- data/dealer.db3 | Bin 0 -> 24576 bytes data/migrations/01-make_cars_table.js | 4 ++-- data/seeds/01-cars.js | 4 ++-- package.json | 3 +++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/data/dealer.db3 b/data/dealer.db3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..41322559c4a1149d3c964be484ba7a0f62a55e14 100644 GIT binary patch literal 24576 zcmeI)O-~a+7zgl~4&}uNYrv-AHZd__165m6<={conxKS;g3(huY{#;6cNTWHgvNLP zzW`Uyei2_TUQCP^Z(h9+_0GlFtx!862NDxZ{*yL4J1@`9{AMpZZT8{fyc2LYJWk<=Sd%MHBHhcCPv1UiUkK7%;qq9g5 zfB*y_009U<00Izz00jQAz@uz`elka&xvDO=91%v12;00V+g!Bbz*SBteW~Ipo?e4*iqKgV;Y9JOeb`Ia`JyRn z-OAETZ*5SQUbnhbHafeW6b;?q9F7hgyRm0PPD(?eBHxYNKy||w^;PZd3v3`?F4OHs zYS2`2tLnRrP{g`zRP_RQgfFLbPkZ}19Y{j%Ts~^r#&j}0F>6L`I?wK44_EovNhSm7 zu0*L^w)HBlh}c;@)JpD>gN3NZZqmLkVwvRZz{}fB*y_009U<00Izz z00bcLUkY%RCD(?sSBiyXo2Z&@WTo2_m)0Ws+}V5gDtZ2A)_Y=owLa+s5(FRs0SG_< z0uX=z1Rwwb2tWV=r&wT+u^dgF@7Y*}O;Y2Z1uRcldB`YC|B})9{ePcW`=@wv7#aj1 z009U<00Izz00bZa0SG_<0w)z6h@;SHeI!0sFXH$|TKfmM0}6tx37AU#p<%+*y@j(3FzC znQ`=s98{6&KI3_oA1@jy4%$I1+9Gr#_WDQK*cYlLyyT^R%z8_#J?pbRAVB~E5P$## pAOHafKmY;|fB*y_a4H4PvjR0X5~f*FXlxslGGnYTWb6wRe*$}{d8+^b literal 0 HcmV?d00001 diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index 2dfa10ef6e..1bc4260371 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -4,8 +4,8 @@ exports.up = function (knex) { table.increments() table.string('vin', 16).notNullable().unique() table.string('make', 128).notNullable() - table.string('model', 128).notNullable() - table.numeric('mileage').unsigned.notNullable() + table.string('model', 256).notNullable() + table.integer('mileage').unsigned().notNullable() table.string('title', 128) table.string('transmision', 128) }) diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index 10a7f80809..bbc80ca47d 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -10,14 +10,14 @@ const cars = [ transmision: 'manual' }, { - vin: '1111111111111111', + vin: '2222222222222222', make: 'jeep', model: 'grand cherokee', mileage: 115000, title: 'clean', }, { - vin: '1111111111111111', + vin: '3333333333333333', make: 'ford', model: 'bronco', mileage: 250000, diff --git a/package.json b/package.json index 874d1980d4..023b6f17ea 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "server": "nodemon index.js", "test": "cross-env NODE_ENV=testing jest --verbose --runInBand", "migrate": "knex migrate:latest", + "rollback": "knex migrate:rollback", + "migup": "knex migrate:up", + "migdown": "knex migrate:down", "seed": "knex seed:run" }, "devDependencies": { From fa90056152fe39290157fd3eed33164e8ad8885c Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 16:47:28 -0800 Subject: [PATCH 07/11] checkCarId and getById completed --- api/cars/cars-middleware.js | 13 +++++++++++-- api/cars/cars-model.js | 11 ++++------- api/cars/cars-router.js | 6 +++--- api/server.js | 11 +++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index c2b828daa1..91f2b1558a 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -3,8 +3,17 @@ const Cars = require('./cars-model') // const vinValidator = require('vin-validator') const checkCarId = async (req, res, next) => { - // DO YOUR MAGIC - + try{ + const cars = await Cars.getById(req.params.id) + if(!car) { + next({status: 404, message: 'not found'}) + }else { + req.car = car + next() + } +} catch (error) { + next(error) +} } const checkCarPayload = (req, res, next) => { diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index afca656da8..f32700183e 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -1,15 +1,12 @@ const db = require('../../data/db-config') -const getAll = async () => { - // DO YOUR MAGIC - const result = await db('cars') - return result +const getAll = () => { + return db('cars') } -const getById = async () => { +const getById = (id) => { // DO YOUR MAGIC - const result = await db('cars').where('id', id).first() - return result + return db('cars').where('id', id).first() } const create = async () => { diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 3d358e26fe..fa5cfdc038 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -10,14 +10,14 @@ const Cars = require('./cars-model') router.get('/', async (req, res, next) => { try{ - const data = await Cars.getAll() - res.json(data) + const cars = await Cars.getAll() + res.json(cars) } catch (error) { next(error) } }) -router.get('/:id', checkCarId, (req, res, next) => { +router.get('/:id', checkCarId, async (req, res, next) => { res.json(req.car) }) diff --git a/api/server.js b/api/server.js index a36d675f36..baac808363 100644 --- a/api/server.js +++ b/api/server.js @@ -6,4 +6,15 @@ const server = express() server.use(express.json()) server.use('/api/cars', carsRouter) +server.use('*', (req, res, next) => { + next({ + status: 404, + message: 'not found' + }) +}) +server.use((err, req, res, next) => { // eslint-diable-line + res.status(err.status || 500).json({ + message: err.message + }) +}) module.exports = server From caecd82ebecb6a8ebe76f4fe49ae82ed4033782e Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 16:56:42 -0800 Subject: [PATCH 08/11] checkCarPayload completed --- api/cars/cars-middleware.js | 20 ++++++++++++++++++-- api/cars/cars-router.js | 6 +++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 91f2b1558a..181e9fc064 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -17,8 +17,24 @@ const checkCarId = async (req, res, next) => { } const checkCarPayload = (req, res, next) => { - // DO YOUR MAGIC - + const error = { status: 400 } + 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 = async (req, res, next) => { diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index fa5cfdc038..e0a2208d37 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -21,7 +21,11 @@ router.get('/:id', checkCarId, async (req, res, next) => { res.json(req.car) }) -router.post('/', checkCarPayload, checkVinNumberUnique, checkVinNumberValid, async (req, res, next) => { +router.post('/', +checkCarPayload, +checkVinNumberUnique, +checkVinNumberValid, +async (req, res, next) => { try{ const newCar = await Cars.create(req.body) res.status(201).json(newCar) From 67f09c14e34a733dfe00743f28ad292e600ba0cc Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 17:08:25 -0800 Subject: [PATCH 09/11] checkVinNumberValid complete, changed vin numbers in seed file --- api/cars/cars-middleware.js | 11 +++++++++-- data/dealer.db3 | Bin 24576 -> 24576 bytes data/seeds/01-cars.js | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 181e9fc064..b42f17204f 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,6 +1,6 @@ const db = require('../../data/db-config') const Cars = require('./cars-model') -// const vinValidator = require('vin-validator') +const vin = require('vin-validator') const checkCarId = async (req, res, next) => { try{ @@ -38,7 +38,14 @@ const checkCarPayload = (req, res, next) => { } const checkVinNumberValid = async (req, res, next) => { - // DO YOUR MAGIC + if(vin.validate(req.body.vin)){ + next() + }else{ + next({ + status: 400, + message: `vin ${req.body.vin} is invalid` + }) + } } diff --git a/data/dealer.db3 b/data/dealer.db3 index 41322559c4a1149d3c964be484ba7a0f62a55e14..0ee10bfe73e1b8cd8ab2680d5127bd4cfa366fce 100644 GIT binary patch delta 291 zcmZoTz}Rqrae}m-FsBF|-~3}+b`X5edPYHDVX zmS2>TRFt2WoX>n=f-w_FohGw5gQ1JLhohl|k%f7%p{1dPp-EP1YC(EYVqS_uaz<)V zes*dq<3Wq$oYcfTBSxSmNpWUzNv{xBr%*#fH#2hs15*ogLxYn1%KVbV%E|BiB{=po j@L%QM&;J_eqK*8HqRje?7;a$(iDKByB*LuEiD5JVUgAff delta 288 zcmZoTz}Rqrae}m<5Ca1P8xX?)*F+s-Mxl)f_416&lOM>-Fekq`y!pTUbAC2vaYklS z&dC$_L^c_)32+oK@So+M$zQZtP#}fBUX_`HL043gnSsF=4W#84r6d*Q=OyPeUzlLT z1X8BSEY4tr2C`C93(|`c^HLO&Gg6E4vr|(U4_YMWq$cJWG6MBTiZhE#8lr)c{L1{2 y#LC2?%*h}9ML7;J@W0`|&VLB#qAmP(BFwssXihO^1_`2BY{VqYtjmdJvLOHy?L=z; diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index bbc80ca47d..f6016377aa 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -2,7 +2,7 @@ const { default: knex } = require("knex") const cars = [ { - vin: '1111111111111111', + vin: 'JTEBU11F670058710', make: 'toyota', model: 'yaris', mileage: 246000, @@ -10,14 +10,14 @@ const cars = [ transmision: 'manual' }, { - vin: '2222222222222222', + vin: '1D7HA18287S191814', make: 'jeep', model: 'grand cherokee', mileage: 115000, title: 'clean', }, { - vin: '3333333333333333', + vin: '5UXXW3C58F0M65560', make: 'ford', model: 'bronco', mileage: 250000, From dbf9c49e556ad576b1023d6ad9af9cb24b78682e Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 17:19:36 -0800 Subject: [PATCH 10/11] middleware complete --- api/cars/cars-middleware.js | 16 +++++++++++++--- api/cars/cars-model.js | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index b42f17204f..ab594e3fb3 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -17,7 +17,6 @@ const checkCarId = async (req, res, next) => { } const checkCarPayload = (req, res, next) => { - const error = { status: 400 } if(!req.body.vin) return next({ status:400, message: 'vin is missing' @@ -50,8 +49,19 @@ const checkVinNumberValid = async (req, res, next) => { } const checkVinNumberUnique = async (req, res, next) => { - // DO YOUR MAGIC - + try{ + const existing = await Cars.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, diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index f32700183e..eca3b14bb5 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -9,6 +9,10 @@ const getById = (id) => { return db('cars').where('id', id).first() } +const getByVin = (vin) => { + return db('cars').where('vin', vin).first() +} + const create = async () => { // DO YOUR MAGIC const [ id ] = await db('cars').insert(car) @@ -18,5 +22,6 @@ const create = async () => { module.exports = { getAll, getById, - create + create, + getByVin, } \ No newline at end of file From 966d31d4cbfac701d679564d43a52fac9400c537 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 27 Nov 2021 17:37:57 -0800 Subject: [PATCH 11/11] endpoints are working in the httpie but my tests are not passing --- api/cars/cars-middleware.js | 5 ++--- api/cars/cars-model.js | 10 ++++------ api/cars/cars-router.js | 7 ++++--- data/seeds/01-cars.js | 2 -- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index ab594e3fb3..97285d8059 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,10 +1,9 @@ -const db = require('../../data/db-config') -const Cars = require('./cars-model') +const Car = require('./cars-model') const vin = require('vin-validator') const checkCarId = async (req, res, next) => { try{ - const cars = await Cars.getById(req.params.id) + const car = await Car.getById(req.params.id) if(!car) { next({status: 404, message: 'not found'}) }else { diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index eca3b14bb5..8591706345 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -5,7 +5,6 @@ const getAll = () => { } const getById = (id) => { - // DO YOUR MAGIC return db('cars').where('id', id).first() } @@ -13,11 +12,10 @@ const getByVin = (vin) => { return db('cars').where('vin', vin).first() } -const create = async () => { - // DO YOUR MAGIC - const [ id ] = await db('cars').insert(car) - const post = await getById(id) - return post +const create = () => { + return db('cars').insert(car).then(([id]) => { + return getById(id) + }) } module.exports = { getAll, diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index e0a2208d37..af5fa38c6f 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -1,16 +1,17 @@ // DO YOUR MAGIC const router = require('express').Router() +const Car = require('./cars-model') const { checkCarId, checkCarPayload, checkVinNumberUnique, checkVinNumberValid, } = require('./cars-middleware') -const Cars = require('./cars-model') + router.get('/', async (req, res, next) => { try{ - const cars = await Cars.getAll() + const cars = await Car.getAll() res.json(cars) } catch (error) { next(error) @@ -27,7 +28,7 @@ checkVinNumberUnique, checkVinNumberValid, async (req, res, next) => { try{ - const newCar = await Cars.create(req.body) + const newCar = await Car.create(req.body) res.status(201).json(newCar) }catch (error) { next(error) diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index f6016377aa..26694c9bf1 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -1,5 +1,3 @@ -const { default: knex } = require("knex") - const cars = [ { vin: 'JTEBU11F670058710',