Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joseph watkins #237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
36 changes: 0 additions & 36 deletions .github/lockdown.yml

This file was deleted.

60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
17 changes: 17 additions & 0 deletions api/cars/cars-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const db = require('../../data/db-config');

async function createCar(car) {
const [id] = await db("cars").insert(car)
return db("cars").where("id", id).first()
}

async function deleteCar(id) {
const car = await db("cars").where("id", id).first()
await db("cars").delete().where("id", id)
return car
}

module.exports = {
createCar,
deleteCar,
}
11 changes: 11 additions & 0 deletions api/cars/cars-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');
const Car = require('./cars-model');
const router = express.Router();

router.delete("/:id", async (req, res) => {
const id = req.params.id
const deleteCar = await Car.createCar(id)
res.status(200).json(deleteCar)
})

module.exports = router
60 changes: 60 additions & 0 deletions api/cars/cars.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const request = require('supertest')
const db = require("../../data/db-config")
const server = require('../server')
const Car = require("./cars-model")

const car1 = { make: "KIA", model: "Forte", transmission: "Automatic" }
const car2 = { make: "Chevrolet", model: "Silverado", transmission: "Manual" }

beforeAll(async () => {
await db.migrate.rollback()
await db.migrate.latest()
})

beforeEach(async () => {
await db("cars").truncate()
})

afterAll(async () => {
await db.destroy()
})

it("correct env var", () => {
expect(process.env.DB_ENV).toBe("testing")
})

describe("Cars model functions", () => {
describe("create car", () => {
it("adds a car to the db", async () => {
let cars
await Car.createCar(car1)
cars = await db("cars")
expect(cars).toHaveLength(1)

await Car.createCar(car2)
cars = await db("cars")
expect(cars).toHaveLength(2)
})

it("inserted make and model", async () => {
const car = await Car.createCar(car1)
expect(car).toMatchObject({ id: 1, ...car })
})
})

describe("[DELETE] / - delete car", () => {
it("removes a car from the db", async () => {
const [id] = await db("cars").insert(car1)
let car = await db("cars").where({ id }).first()
expect(car).toBeTruthy()
await request(server).delete("/cars/" + id)
car = await db("cars").where({ id }).first()
expect(car).toBeFalsy()
})
it("responds with the deleted car", async () => {
await db("cars").insert(car1)
let car = await request(server).delete("/cars/1")
expect(car.body).toMatchObject(car1)
})
})
})
9 changes: 9 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");
const server = express();
const carsRouter = require('./cars/cars-router');

server.use('/api/cars', carsRouter);
server.use(express.json());


module.exports = server;
5 changes: 5 additions & 0 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const knex = require('knex')
const configs = require('../knexfile')
const env = process.env.DB_ENV || 'development'

module.exports = knex(configs[env])
Binary file added data/dealer.db3
Binary file not shown.
14 changes: 14 additions & 0 deletions data/migrations/20220728205723_01-make_cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

exports.up = function(knex) {
return knex.schema.createTable('cars', tbl => {
tbl.increments('id');
tbl.string('make', 128).notNullable();
tbl.string('model', 128).notNullable();
tbl.string('transmission', 128);
});
};


exports.down = function(knex) {
return knex.schema.dropTableIfExists('cars');
};
18 changes: 18 additions & 0 deletions data/seeds/01-cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const cars = [
{
make: "Ford",
model: "F150",
transmission: "automatic",
},
{
make: "Ford",
model: "F150",
transmission: "automatic",
},
]


exports.seed = async function(knex) {
await knex('cars').truncate()
await knex('cars').insert(cars)
};
Binary file added data/test.db3
Binary file not shown.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const server = require('./')

const port = process.env.PORT || 9000;

server.listen(port, () => console.log(`\n** Running on port ${port} **\n`))

//COMPLETE
26 changes: 26 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const sharedConfig = {
client: 'sqlite3',
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
},
seeds: {
directory: './data/seeds',
},
pool: {
afterCreate: (conn, done) => {
conn.run('PRAGMA foreign_keys = ON', done)
},
},
}

module.exports = {
development: {
...sharedConfig,
connection: { filename: './data/dealer.db3' },
},
testing: {
...sharedConfig,
connection: { filename: './data/test.db3' },
},
}
Loading