From 65f2b648291e823b7ebb9f568b1fe72c474c7e9c Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 11:38:47 -0800 Subject: [PATCH 1/9] cars table completed --- data/migrations/01-make_cars_table.js | 10 ++++++++-- package-lock.json | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index 9723c175be..7542ec8a2b 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -1,7 +1,13 @@ exports.up = function (knex) { - // DO YOUR MAGIC + table.increments() + table.string('vin', 16).notNullable().unique() + table.string('make', 128).notNullable() + table.string('model', 128).notNullable() + table.integer('mileage', 128).notNullable().unsigned() + table.string('title', 128) + table.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 d1f3f92341..1257c42909 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 6a68ace7ff0272c7299e993cb8fe1cbcca43d05e Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 11:50:46 -0800 Subject: [PATCH 2/9] created cars in seeds folder. db created --- data/dealer.db3 | Bin 0 -> 24576 bytes data/migrations/01-make_cars_table.js | 16 +++++++------ data/seeds/01-cars.js | 32 +++++++++++++++++++++++++- package-lock.json | 15 ++++++------ package.json | 9 ++++++-- 5 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 data/dealer.db3 diff --git a/data/dealer.db3 b/data/dealer.db3 new file mode 100644 index 0000000000000000000000000000000000000000..aa143dbaa9006efeead50cb1700a1eea6e92b733 GIT binary patch literal 24576 zcmeI(&u-H&90zc_=^uvJiY8UPTAUhn3^r8Yz<~li45o>}hTxQVsp(7;hb8W`2??p= z1$YHcJP>!ToDdJN3wBCdHB6N_aOm{4nl}FTtNn27_#?? zjCgX&`r?%FfsUmL5)lcd*I4!)?3gFPAx|0`cil-ZQy1~R@TZlV_h)P4t_bH;%TVy1 z>7O5t39O|KbCGRn!c`i{G?lTMjq46?IF)*Bw zQ4{N&E!p1#o$Dq?-zuiw?(>4V0-E$qwod6$X!gEdD$Me$K;*K{%)oM$4aFm|W_r5A z2ee{Cp54k~=Ek4P7G`4lx!P==)4@)bdF~|bK&C6P*=o6FnGU!P4kudnG8r#~r=@M* zyBnUtN0uX=z1Rwwb2tWV=5P$##7E*xG61lfjy1jAJ-Y4?1J6YLn ziuHFX{rELl{bKk3h3Yp_{bAmaAOHafKmY;|fB*y_009U<00IzL6oJ)((_G3uT2QA> zD_@p9k}>-I|D06M7iD@FCj=k>0SG_<0uX=z1Rwwb2tWV={~+K{hg`AyfAgn*BnUtN z0uX=z1Rwwb2tWV=5P$##F0sH>J2&kAFYyXvY7l?`1Rwwb2tWV=5P$##AOHbEegn|g BKqdeH literal 0 HcmV?d00001 diff --git a/data/migrations/01-make_cars_table.js b/data/migrations/01-make_cars_table.js index 7542ec8a2b..b10625aabb 100644 --- a/data/migrations/01-make_cars_table.js +++ b/data/migrations/01-make_cars_table.js @@ -1,11 +1,13 @@ exports.up = function (knex) { - table.increments() - table.string('vin', 16).notNullable().unique() - table.string('make', 128).notNullable() - table.string('model', 128).notNullable() - table.integer('mileage', 128).notNullable().unsigned() - table.string('title', 128) - table.string('transmission', 128) + return knex.schema.createTable('cars', (table) => { + table.increments() + table.string('vin', 16).notNullable().unique() + table.string('make', 128).notNullable() + table.string('model', 128).notNullable() + table.integer('mileage', 128).notNullable().unsigned() + table.string('title', 128) + table.string('transmission', 128) + }) }; exports.down = function (knex) { diff --git a/data/seeds/01-cars.js b/data/seeds/01-cars.js index 3f7acdde23..238bc91672 100644 --- a/data/seeds/01-cars.js +++ b/data/seeds/01-cars.js @@ -1 +1,31 @@ -// STRETCH +const cars = [ + { + vin: 'JTEBU11F670058710', + make: 'toyota', + model: 'yaris', + mileage: 235000, + title: 'clean', + transmision: 'manual' + }, + { + vin: '1D7HA18287S191814', + make: 'jeep', + model: 'grand cherokee', + mileage: 115000, + title: 'clean', + transmision: 'automatic' + }, + { + vin: '5UXXW3C58F0M65560', + make: 'ford', + model: 'bronco', + mileage: 250000, + title: 'clean', + transmision: 'automatic' + } + ] + + exports.seed = async function(knex){ + await knex('cars').truncate() + await knex('cars').insert(cars) + } diff --git a/package-lock.json b/package-lock.json index 1257c42909..b5833301f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "version": "1.0.0", "dependencies": { "express": "^4.17.1", - "knex": "^0.95.14", + "knex": "^0.95.15", "sqlite3": "^5.0.2" }, "devDependencies": { @@ -4613,9 +4613,10 @@ } }, "node_modules/knex": { - "version": "0.95.14", - "resolved": "https://registry.npmjs.org/knex/-/knex-0.95.14.tgz", - "integrity": "sha512-j4qLjWySrC/JRRVtOpoR2LcS1yBOsd7Krc6mEukPvmTDX/w11pD52Pq9FYR56/kLXGeAV8jFdWBjsZFi1mscWg==", + "version": "0.95.15", + "resolved": "https://registry.npmjs.org/knex/-/knex-0.95.15.tgz", + "integrity": "sha512-Loq6WgHaWlmL2bfZGWPsy4l8xw4pOE+tmLGkPG0auBppxpI0UcK+GYCycJcqz9W54f2LiGewkCVLBm3Wq4ur/w==", + "license": "MIT", "dependencies": { "colorette": "2.0.16", "commander": "^7.1.0", @@ -10784,9 +10785,9 @@ "dev": true }, "knex": { - "version": "0.95.14", - "resolved": "https://registry.npmjs.org/knex/-/knex-0.95.14.tgz", - "integrity": "sha512-j4qLjWySrC/JRRVtOpoR2LcS1yBOsd7Krc6mEukPvmTDX/w11pD52Pq9FYR56/kLXGeAV8jFdWBjsZFi1mscWg==", + "version": "0.95.15", + "resolved": "https://registry.npmjs.org/knex/-/knex-0.95.15.tgz", + "integrity": "sha512-Loq6WgHaWlmL2bfZGWPsy4l8xw4pOE+tmLGkPG0auBppxpI0UcK+GYCycJcqz9W54f2LiGewkCVLBm3Wq4ur/w==", "requires": { "colorette": "2.0.16", "commander": "^7.1.0", diff --git a/package.json b/package.json index cdbfc72774..08805c86f5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,12 @@ "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", + "rollback": "knex migrate:rollback", + "migup": "knex migrate:up", + "migdown": "knex migrate:down", + "seed": "knex seed:run" }, "devDependencies": { "@types/jest": "^27.0.3", @@ -16,7 +21,7 @@ }, "dependencies": { "express": "^4.17.1", - "knex": "^0.95.14", + "knex": "^0.95.15", "sqlite3": "^5.0.2" }, "repository": { From 0a1377c4677d6d4ffe67595a717bd5bd6c787b50 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 12:51:54 -0800 Subject: [PATCH 3/9] wip --- data/dealer.db3 | Bin 24576 -> 28672 bytes data/seeds/01-cars.js | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data/dealer.db3 b/data/dealer.db3 index aa143dbaa9006efeead50cb1700a1eea6e92b733..0faf5c5e06e32bcd39c1aad924024750d203257e 100644 GIT binary patch literal 28672 zcmeI)O>Y}T7zgkjPhBTz+C^%OE}%?hX4d1009U<00Izz00jOm0rFn%_HFX~YGmva}K_HEVZQ>nuE&TB$I{m~F9@MzbW&p4c91*4y#9 zYxMZ&!ne3Hy0RT^?DCVS$c~&*)hIB$(6z(R_6IkcD+)bpAxnJQv-mU92*S?Bh4Z;& zj`-?M-?I<;Tvj+~d#kas*{X{#x?gXzIC4~vtvBKbX2v|W| zE)IiAEH)aIEmaeRvDsgi9vhCgq{IfCi8zsYS~XEtJbH&_a=K2AcH^G+JpQcX+Pi@f zi4h1pj&Js+<(*5&bf;D6vO+f=v+fYg^CRZ<9f$1)wrd23tj7lo;>@vVH1-?Sx#6wr7gxTt1q^OPXldSYg34TwW59#iKhUBX9Y9aay;> zx|2%WbG);0KSzyauhmJ}kS;qOio%kv7e&u^jL6&@_F7~N2ci?%vc6?RT%Hx>+?=RC zE8-6a1Rwwb2tWV=5P$##AOHafKmY>&NdcBhE0x)FS-U2eh&u7gRlH1b{c%V>``T45 z$oW52zf;s-)E~tM4hTR10uX=z1Rwwb2tWV=5P$##&al7?rCB29_w;g#E|Brx9pnhr zXU8(*pJWX7|6eHTi!r@5&s>F zu03lf@IBL~KYVG5S6F!R#RcN^d$xHeC8LTWO1oeAu%=a)s+C9DEv>5Ee9Za2c$tD{ zF>{XxevfnVZ8aIaL_~BxM|1gy&HCMzraf4$l*@XxqLm~6(2tBmBd|mI)gN(~Yj}Ob zIW;;N^*1Gb>KdmIfB*y_009U<00Izz00bZa0SKH8fh=7l6AKJf=1wdiq^{7#*@=aL zInfWQexsHGBk|EY8Of3hg>CtmvQ8~^|S delta 424 zcmZp8z}Rqrae}lU3j+fK8xX?)%S0VxaTW$W?+Ls>Ay%HZ4E&CK@Ar=VlfcXXYauwb_Mlwjd+p*Isau~h#lvb z6k=l5=FHAZt%%RfOfO0-0ctK5XJj_zoIHU~WRn4l02712W Date: Sat, 29 Jan 2022 12:58:39 -0800 Subject: [PATCH 4/9] cars model completed --- api/cars/cars-model.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index 39b2c50a8d..bdbc484a0c 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -1,11 +1,20 @@ +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 create = (cars) => { + const [ id ] = db('cars').insert(cars) + return getById(id) } -const create = () => { - // DO YOUR MAGIC +module.exports = { + getAll, + getById, + create, } From e4ada41973b283efbb92c5dcf42f2f61d9a9f497 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 13:03:05 -0800 Subject: [PATCH 5/9] server updated --- api/server.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/api/server.js b/api/server.js index c5a0be1b41..071b45d969 100644 --- a/api/server.js +++ b/api/server.js @@ -1,7 +1,19 @@ 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) +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 From c962d3c81bbac3bc8bce9c5348f6284f53e046d9 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 13:18:58 -0800 Subject: [PATCH 6/9] middleware completed and cars model updated --- api/cars/cars-middleware.js | 73 ++++++++++++++++++++++++++++++++++--- api/cars/cars-model.js | 6 +-- package-lock.json | 13 ++++++- package.json | 4 +- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 7e738d6062..9750450ab3 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -1,15 +1,76 @@ -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: `car with id ${car} is 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.vin) + return next({ + status: 400, + message: 'vin 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) => { - // DO YOUR MAGIC +const checkVinNumberUnique = async (req, res, next) => { + try{ + const existingVin = await Car.getByVin(req.body.vin) + if(!existingVin){ + next() + }else{ + next({ + status: 400, + message: `vin ${req.body.vin} already exists` + }) + } + }catch(err){ + next(err) + } } +module.exports = { + checkCarId, + checkCarPayload, + checkVinNumberUnique, + checkVinNumberValid, +} \ No newline at end of file diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index bdbc484a0c..1175e7135f 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -4,8 +4,8 @@ const getAll = () => { return db('cars') } -const getById = (id) => { - return db('cars').where('id', id).first() +const getByVin = (vin) => { + return db('cars').where('vin', vin).first() } const create = (cars) => { @@ -15,6 +15,6 @@ const create = (cars) => { module.exports = { getAll, - getById, + getByVin, create, } diff --git a/package-lock.json b/package-lock.json index b5833301f4..3f43fe209f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "dependencies": { "express": "^4.17.1", "knex": "^0.95.15", - "sqlite3": "^5.0.2" + "sqlite3": "^5.0.2", + "vin-validator": "^1.0.0" }, "devDependencies": { "@types/jest": "^27.0.3", @@ -6882,6 +6883,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", @@ -12518,6 +12524,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 08805c86f5..f71feffcb4 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "start": "node index.js", "server": "nodemon index.js", "test": "cross-env NODE_ENV=testing jest --verbose --runInBand", "migrate": "knex migrate:latest", @@ -22,7 +23,8 @@ "dependencies": { "express": "^4.17.1", "knex": "^0.95.15", - "sqlite3": "^5.0.2" + "sqlite3": "^5.0.2", + "vin-validator": "^1.0.0" }, "repository": { "type": "git", From 6511c88f433deaf5c6bef88962d3cc26ef63df81 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 13:24:53 -0800 Subject: [PATCH 7/9] router.get working --- api/cars/cars-router.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 3fca0e7d7d..30100b634c 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -1 +1,18 @@ -// DO YOUR MAGIC +const router = require('express').Router() +const Car = require('./cars-model') +const { + checkCarId, + checkVinNumberUnique, + checkCarPayload, + checkVinNumberValid, +} = require('./cars-middleware') + +router.get('/', async (req, res, next) => { + try{ + const cars = await Car.getAll() + res.json(cars) + }catch(err){ + next(err) + } +}) +module.exports = router \ No newline at end of file From 3af76b90f584574281fd074e349f0aa074f128d0 Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 13:33:33 -0800 Subject: [PATCH 8/9] added getById back, didnt mean to delete --- api/cars/cars-model.js | 9 +++++++-- api/cars/cars-router.js | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index 1175e7135f..6a491aff29 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -4,17 +4,22 @@ const getAll = () => { return db('cars') } +const getById = (id) => { + return db('cars').where('id', id).first() +} + const getByVin = (vin) => { return db('cars').where('vin', vin).first() } -const create = (cars) => { - const [ id ] = db('cars').insert(cars) +const create = () => { + const [ id ] = db('cars').insert(car) return getById(id) } module.exports = { getAll, getByVin, + getById, create, } diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 30100b634c..4989450c7c 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -15,4 +15,8 @@ router.get('/', async (req, res, next) => { next(err) } }) + +router.get('/:id', checkCarId, (req, res, next) => { + res.json(req.car) +}) module.exports = router \ No newline at end of file From c87064667742ef0f0aef4bc5a98d6235ef346dcf Mon Sep 17 00:00:00 2001 From: Cynthia Coronado Date: Sat, 29 Jan 2022 14:24:14 -0800 Subject: [PATCH 9/9] tests passing, put is not working but delete is working --- api/cars/cars-middleware.js | 4 ++-- api/cars/cars-model.js | 16 ++++++++++++-- api/cars/cars-router.js | 42 ++++++++++++++++++++++++++++++++++++ data/dealer.db3 | Bin 28672 -> 28672 bytes 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/api/cars/cars-middleware.js b/api/cars/cars-middleware.js index 9750450ab3..0bf61d4b94 100644 --- a/api/cars/cars-middleware.js +++ b/api/cars/cars-middleware.js @@ -34,10 +34,10 @@ const checkCarPayload = (req, res, next) => { status: 400, message: 'model is missing' }) - if(!req.body.vin) + if(!req.body.mileage) return next({ status: 400, - message: 'vin is missing' + message: 'mileage is missing' }) next() } diff --git a/api/cars/cars-model.js b/api/cars/cars-model.js index 6a491aff29..3126bb3570 100644 --- a/api/cars/cars-model.js +++ b/api/cars/cars-model.js @@ -12,14 +12,26 @@ const getByVin = (vin) => { return db('cars').where('vin', vin).first() } -const create = () => { - const [ id ] = db('cars').insert(car) +const create = (car) => { + return db('cars').insert(car).then(([id]) => { + return getById(id) + }) +} + +const updateById = (id, car) => { + db('cars').where('id', id).update(car) return getById(id) } +const deleteById = (id) => { + return db('cars').where('id', id).del() +} + module.exports = { getAll, getByVin, getById, create, + updateById, + deleteById } diff --git a/api/cars/cars-router.js b/api/cars/cars-router.js index 4989450c7c..eb2ef74861 100644 --- a/api/cars/cars-router.js +++ b/api/cars/cars-router.js @@ -19,4 +19,46 @@ router.get('/', async (req, res, next) => { router.get('/:id', checkCarId, (req, res, next) => { res.json(req.car) }) + +router.post('/', +checkCarPayload, +checkVinNumberUnique, +checkVinNumberValid, +async (req, res, next) => { + try{ + const newCar = await Car.create(req.body) + res.status(201).json(newCar) + }catch(err){ + next(err) + } +}) + +router.put('/:id', +checkCarId, +checkCarPayload, +async (req, res, next) => { + try{ + const updatedCar = await Car.updateById(req.params.id, req.body) + res.status(200).json(updatedCar) + }catch(err){ + next(err) + } +}) + +router.delete('/:id', checkCarId, (req, res, next) => { + Car.deleteById(req.params.id) + .then(deletedCar => { + res.status(200).json(deletedCar) + }) + .catch(err => { + next(err) + }) +}) + +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 index 0faf5c5e06e32bcd39c1aad924024750d203257e..8d1d5c79aae67efa8d1fd1392c452be711fb0d41 100644 GIT binary patch delta 282 zcmZp8z}WDBae}moXd