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

[Tommy Collison] RDBMS-API-Full #343

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

tommycollison
Copy link

@johnoro ✏️ 📅 🦁

@tommycollison
Copy link
Author

tommycollison commented Jan 16, 2019

next steps:

  • flesh out other endpoints
  • make sure cohorts and students are successfully talking to one another

})
.catch(err => {
res.status(500).json({err: 'Failed to insert this student'})
})
Copy link

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:

const post = table => ({ body }, res) => {
  db(table).insert(body)
    .then(ids => res.status(201).json(ids))
    .catch(() => {
      res.status(500).json({
        err: `Failed to insert into ${table}`
      });
    });
};

and then, the usage, in an iterative way so as to avoid repetition:

[ 'cohorts', 'students' ].forEach(table => {
  server.post(`api/${table}`, post(table));
});

Copy link

@johnoro johnoro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far!

const {id} = req.params;
const cohort = req.body;

db('cohorts').where('id', id).update(cohort)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can optionally pass in an object to where:

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)

})
.catch(err => {
res.status(500).json({err: 'Failed to delete that cohort'})
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delete looks very similar to your put. I'm wondering if we could make this do the same thing with a helper function for both. I think with just these two alone, it wouldn't quite be worth it, but when considering the other endpoints that we have (and possible future ones), it'd probably be beneficial to build out something to make them all more modular.

@@ -53,10 +53,28 @@ server.post('/api/students', (req, res) => {

server.put('/api/cohorts/:id', (req, res) => {
// I'm a put request
const {id} = req.params;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option for routing is using the route method and then chaining the specific CRUD actions on it, like this:

server.route('/api/cohorts/:id')
  .put((req, res) => { /* PUT logic here */ })
  .delete((req, res) => { /* DELETE logic here */ });

Copy link

Choose a reason for hiding this comment

The 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.

@tommycollison
Copy link
Author

tommycollison commented Jan 19, 2019

MVP

  • [POST] /api/cohorts This route should save a new cohort to the database.
  • [GET] /api/cohorts This route will return an array of all cohorts.
  • [GET] /api/cohorts/:id This route will return the cohort with the matching id.
  • [GET] /api/cohorts/:id/students returns all students for the cohort with the specified id.
  • [PUT] /api/cohorts/:id This route will update the cohort with the matching id using information sent in the body of the request.
  • [DELETE] /api/cohorts/:id This route should delete the specified cohort.

Stretch

  • [POST] /students This route should save a new student to the database.
  • [GET] /students This route will return an array of all students.
  • [GET] /students/:id This route will return the student with the matching id.
  • [PUT] /students/:id This route will update the student with the matching id using information sent in the body of the request.
  • [DELETE] /students/:id This route should delete the specified student.

@johnoro
Copy link

johnoro commented Jan 19, 2019

Since you're already a bit ahead, here's one more thing that you could do as stretch:

  • [GET] /api/cohorts/:id This route will return the cohort with the matching id, AND will contain its corresponding students in this format:
{
  id: 4,
  name: 'WebPT2',
  students: [
    {
      id: 12,
      name: 'Tommy'
    },
    {
      id: 13,
      name: 'John' // not exactly a student
    }
  ]
}

@tommycollison
Copy link
Author

I was going to refactor based on your feedback above, but I'm all ears!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants