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

Michael newell #247

Open
wants to merge 6 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
90 changes: 90 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Mac cruft
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# 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

# 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/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# VSCode debugging
.vscode/launch.json

# databases
*.db3

22 changes: 22 additions & 0 deletions api/capitals/capitals-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const db = require('../../data/db-config.js')

module.exports = {
addCapital,
getAll,
getById,

}

async function getAll() {
return db('capitals')
}

async function getById(id) {
return db('capitals').where('capital_id', id).first()
}

async function addCapital(capital) {
const [id] = await db('capitals').insert(capital)
return db('capitals').where('capital_id', id).first()
}

39 changes: 39 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const express = require('express');

const Capitals = require('./capitals/capitals-model.js');

const server = express();

server.use(express.json());

server.get('/', (req, res) => {
res.status(200).json({ api: 'up' })
})

server.get('/capitals', (req, res) => {
Capitals.getAll()
.then(capitals => {
res.status(200).json(capitals)
})
.catch(err => {
res.status(500).json(err)
})
})

server.get('/capitals/:id', (req, res, next) => {
Capitals.getById(req.params.id)
.then(resource => {
res.status(200).json(resource)
})
.catch(next)
})

server.post('/capitals', async (req, res) => {
console.log(req.body.city)
res.status(201).json(await Capitals.addCapital(req.body))
})




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


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

beforeEach(async () => {
await db.seed.run()
})

describe('correct env var', () => {
test('correct env var', () => {
expect(process.env.NODE_ENV).toBe('testing')
})
})

describe('[GET] /capitals', () => {
test('responds with 200 ok', async () => {
const res = await request(server).get('/capitals')
expect(res.status).toBe(200)
})
test('responds with all capitals', async () => {
const res = await request(server).get('/capitals')
expect(res.body).toHaveLength(5)
})
})

describe('[GET] /capitals/:id', () => {
test('get capital with id of 1', async () => {
const res = await request(server).get('/capitals/1')
expect(res.body).toEqual({ "city": 'Paris', "country": 'France', "capital_id": 1})
})
test('get capital with id of 2', async () => {
const res = await request(server).get('/capitals/2')
expect(res.body).toEqual({"city": 'Madrid', "country": 'Spain', "capital_id": 2})
})
test('get capital with id of 3', async () => {
const res = await request(server).get('/capitals/3')
expect(res.body).toEqual({"city": 'Ottawa', "country": 'Canada', "capital_id": 3})
})
test('get capital with id of 4', async () => {
const res = await request(server).get('/capitals/4')
expect(res.body).toEqual({"city": 'Washington DC', "country": 'USA', "capital_id": 4})
})
test('get capital with id of 5', async () => {
const res = await request(server).get('/capitals/5')
expect(res.body).toEqual({"city": "Dublin", "country": "Ireland", "capital_id": 5})
})
})

describe('[POST] /capitals', () => {
const newOne = { city: 'London', country: 'UK' }
test('add new capital', async () => {
await request(server).post('/capitals').send(newOne)
expect(await db('capitals')).toHaveLength(6)
})
test('add another capital', async () => {
const newTwo = {city: 'Cairo', country: 'Egypt'}
await request(server).post('/capitals').send(newTwo)
expect(await db('capitals')).toHaveLength(6)
})
})
7 changes: 7 additions & 0 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const knex = require('knex');

const config = require('../knexfile.js');

const environment = process.env.NODE_ENV || 'development';

module.exports = knex(config[environment]);
11 changes: 11 additions & 0 deletions data/migrations/1691448929843-capitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.up = function (knex) {
return knex.schema.createTable('capitals', tbl => {
tbl.increments('capital_id');
tbl.string('city', 30).unique().notNullable();
tbl.string('country', 30).unique().notNullable();
});
}

exports.down = function (knex) {
return knex.schema.dropTableIfExists('capitals');
}
13 changes: 13 additions & 0 deletions data/seeds/001-capitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.seed = function(knex, Promise) {
return knex('capitals')
.truncate()
.then(function() {
return knex('capitals').insert([
{city: 'Paris', country: 'France'},
{city: 'Madrid', country: 'Spain'},
{city: 'Ottawa', country: 'Canada'},
{city: 'Washington DC', country: 'USA'},
{city: 'Dublin', country: 'Ireland'}
])
})
}
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').config()

const server = require('./api/server.js');

const port = process.env.PORT || 9000;
server.listen(port, () => console.log(`\n** server up on port ${port} **\n`))
24 changes: 24 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const common = {
client: 'sqlite3',
useNullAsDefault: true,
migrations: { directory: './data/migrations' },
seeds: { directory: './data/seeds' },
}

module.exports = {
development: {
...common,
connection: {
filename: './data/capitals.db3',
},
},
testing: {
...common,
connection: {
filename: './data/test.db3'
},
},
production: {

}
}
Loading