-
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
Open
tommycollison
wants to merge
7
commits into
bloominstituteoftechnology:master
Choose a base branch
from
tommycollison:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71b60f6
minor edit for initial commit
tommycollison 190fbfe
skeleton
tommycollison 9989bc1
some more posts
tommycollison eabfb1a
delicious, delicious migrations
tommycollison e1d14d6
GET and POST endpoints tested w postman
tommycollison 38b5ab6
put and delete endpoints
tommycollison 04c9115
bunch more endpoints, get-students-by-cohort-id working
tommycollison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Building an API using a Relational Database | ||
# Foo | ||
|
||
## Topics | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 | ||
}) | ||
|
||
server.get('/api/cohorts/:id/students', (req, res) => { | ||
// I'm a get-some-students-by-cohort-id | ||
}) | ||
|
||
|
||
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; | ||
db('students').insert(student) | ||
.then(id => { | ||
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 | ||
}) | ||
|
||
server.delete('/api/cohorts/:id', (req, res) => { | ||
// I'm a delete request | ||
}) | ||
|
||
server.listen(PORT, () => { | ||
console.log(`I'm alive, awake, alert, and enthusiastic on port ${PORT}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: