-
Notifications
You must be signed in to change notification settings - Fork 782
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
[Tommy Collison] RDBMS-API-Full #343
base: master
Are you sure you want to change the base?
Changes from all commits
71b60f6
190fbfe
9989bc1
eabfb1a
e1d14d6
38b5ab6
04c9115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Building an API using a Relational Database | ||
# Foo | ||
|
||
## Topics | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
exports.up = function(knex, Promise) { | ||
//changes to be made to the schema | ||
return knex.schema.createTable('cohorts', table => { | ||
table.increments(); | ||
table.string('name').notNullable(); | ||
}); | ||
}; | ||
|
||
exports.down = function(knex, Promise) { | ||
// CMD+Z | ||
return knex.schema.dropTableIfExists('cohorts'); | ||
|
||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
exports.up = function(knex, Promise) { | ||
return knex.schema.createTable('students', table => { | ||
table.increments(); | ||
table.string('name').notNullable(); | ||
table.integer('cohort_id').unsigned(); | ||
table.foreign('cohort_id').references('id').on('cohorts'); | ||
}) | ||
|
||
}; | ||
|
||
exports.down = function(knex, Promise) { | ||
return knex.schema.dropTableIfExists('students') | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const express = require('express'); | ||
const knex = require('knex'); | ||
|
||
const dbConfig = require('./knexfile'); | ||
|
||
const server = express(); | ||
const db = knex(dbConfig.development); | ||
const PORT = 8070; | ||
|
||
server.use(express.json()); | ||
|
||
server.get('/api/cohorts', (req, res) => { | ||
// I'm a get for cohorts | ||
db('cohorts').then(rows => { | ||
res.json(rows); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to find cohorts'}) | ||
}) | ||
}) | ||
|
||
server.get('/api/cohorts/:id', (req, res) => { | ||
// I'm a get by id | ||
const {id} = req.params; | ||
db('cohorts').where('id', id) | ||
.then(rows => { | ||
res.json(rows); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to find that cohort'}) | ||
}) | ||
}) | ||
|
||
server.get('/api/cohorts/:id/students', (req, res) => { | ||
// I'm a get-some-students-by-cohort-id | ||
const {id} = req.params; | ||
db('students').where('cohort_id', id) | ||
.then(rows => { | ||
res.json(rows); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to find students in that cohort'}) | ||
}) | ||
}) | ||
|
||
|
||
server.post('/api/cohorts', (req, res) => { | ||
// I'm a post request | ||
const cohort = req.body; | ||
db('cohorts').insert(cohort) | ||
.then(ids => { | ||
res.status(201).json(ids); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to insert this cohort'}) | ||
}) | ||
}) | ||
|
||
server.post('/api/students', (req, res) => { | ||
const student = req.body; | ||
console.log('yo') | ||
db('students').insert(student) | ||
.then(ids => { | ||
res.status(201).json(ids) | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to insert this student'}) | ||
}) | ||
}) | ||
|
||
server.put('/api/cohorts/:id', (req, res) => { | ||
// I'm a put request | ||
const {id} = req.params; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option for routing is using the server.route('/api/cohorts/:id')
.put((req, res) => { /* PUT logic here */ })
.delete((req, res) => { /* DELETE logic here */ }); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot of people will then break out the callbacks into helper functions (typically in another file): const putCohorts = (req, res) => { /* PUT logic for cohorts here */ };
const deleteCohorts = (req, res) => { /* DELETE logic for cohorts here */ };
server.route('/api/cohorts/:id')
.put(putCohorts)
.delete(deleteCohorts); This lends well for the organizational side of making your APIs modular. |
||
const cohort = req.body; | ||
|
||
db('cohorts').where('id', id).update(cohort) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can optionally pass in an object to db('cohorts').where({ id: id }).update(cohort) or, using the shorthand syntax for a key that has the same name as a variable being used for the value: db('cohorts').where({ id }).update(cohort) |
||
.then(rowCount => { | ||
res.status(201).json(rowCount); | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to update that cohort'}) | ||
}) | ||
}) | ||
|
||
server.delete('/api/cohorts/:id', (req, res) => { | ||
// I'm a delete request | ||
const {id} = req.params; | ||
db('cohorts').where('id', id).del() | ||
.then(rowCount => { | ||
res.status(201).json(rowCount) | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Failed to delete that cohort'}) | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
}) | ||
|
||
// student endpoints | ||
server.get('/api/students', (req, res) => { | ||
db('students').then(rows => { | ||
res.status(201).json(rows) | ||
}) | ||
.catch(err => { | ||
res.status(500).json({err: 'Could not find students'}) | ||
}) | ||
}) | ||
|
||
server.listen(PORT, () => { | ||
console.log(`I'm alive, awake, alert, and enthusiastic on port ${PORT}`) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Update with your config settings. | ||
|
||
module.exports = { | ||
|
||
development: { | ||
client: 'sqlite3', | ||
connection: { | ||
filename: './data/students.db' | ||
}, | ||
useNullAsDefault: true, | ||
migrations: { | ||
directory: './data/migrations' | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "Students", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"server": "nodemon" | ||
}, | ||
"repository": "https://github.com/tommycollison/RDBMS-API-Full.git", | ||
"author": "Tommmy Collison <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"express": "^4.16.4", | ||
"knex": "^0.16.3", | ||
"nodemon": "^1.18.9", | ||
"sqlite3": "^4.0.6" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could potentially reduce the repetition of having nearly identical post callbacks for students and cohorts here. For example, let's make one function for both:
and then, the usage, in an iterative way so as to avoid repetition: