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

Server-Side #20

Open
emilyparkes opened this issue Sep 26, 2022 · 0 comments
Open

Server-Side #20

emilyparkes opened this issue Sep 26, 2022 · 0 comments

Comments

@emilyparkes
Copy link

emilyparkes commented Sep 26, 2022

General

I see the word availability everywhere and I'm not sure what it means. Do you mean 'in season' or 'in stock' or something else? I have to look at many other things to grasp the context for reading this word.

db

  • Have consistent exports, export in all files at the end of the top, currently users.js is different from the rest and it should look like your code.
  • This function could be reduced as there is a lot of repetitive code, the only thing changing is the array. You could have variables that replace the numbers array based on the season rather than repeating the entire db function.
function readAvailabilityBySeason(season, db = connection) {
  switch (season) {
    case 'summer':
      return db('produce_available_months')
        .select()
        .whereIn('month', [12, 1, 2])
        .join('produce', 'produce.id', 'produce_available_months.produce_id')
        .select('produce.name', 'produce.display_name', 'produce.image_url')
    case 'autumn':
      return db('produce_available_months')
        .select()
        .whereIn('month', [3, 4, 5])
        .join('produce', 'produce.id', 'produce_available_months.produce_id')
        .select('produce.name', 'produce.display_name', 'produce.image_url')
    case 'winter':
      return db('produce_available_months')
        .select()
        .whereIn('month', [6, 7, 8])
        .join('produce', 'produce.id', 'produce_available_months.produce_id')
        .select('produce.name', 'produce.display_name', 'produce.image_url')
    case 'spring':
      return db('produce_available_months')
        .select()
        .whereIn('month', [9, 10, 11])
        .join('produce', 'produce.id', 'produce_available_months.produce_id')
        .select('produce.name', 'produce.display_name', 'produce.image_url')
    default:
      return db('produce_available_months')
        .select()
        .join('produce', 'produce.id', 'produce_available_months.produce_id')
        .select('produce.name', 'produce.display_name', 'produce.image_url')
  }
}

routes

  • You should make sure all ids are in fact numbers. Convert req.params.id into an integer.
router.get('/:id', async (req, res) => {
  try {
    const result = await db.readOneAvailability(req.params.id)
    res.json(result)
  } catch (err) {
    res.status(500).send(err.message)
  }
})
  • Remove unused variables.
router.patch('/:id', async (req, res) => {
  try {
    const response = await db.updateAvailability(req.body, req.params.id) <- unused 'response'
    const updatedResp = await db.readOneAvailability(req.params.id)
    res.json(updatedResp)
  } catch (err) {
    res.status(500).send(err.message)
  }
})
  • Remove comments, route should be clear on what is being requested. // Get all availability for a season
  • Routes should be in relevant collections. So I would expect :
// Get all availability with the same produce_id
router.get('/produce/:produce_id', async (req, res) => {
  try {
    const result = await db.readAvailabilityByProduceId(req.params.produce_id)
    res.json(result)
  } catch (err) {
    res.status(500).send(err.message)
  }
})

to be in the produce.js file instead.

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

No branches or pull requests

1 participant