Giter Club home page Giter Club logo

codecademy_final-capstone-project_expresso-web_api's Introduction

Hi, I'm a full stack dev and music producer. Find more on my website.

codecademy_final-capstone-project_expresso-web_api's People

Contributors

aletna avatar

Watchers

 avatar

codecademy_final-capstone-project_expresso-web_api's Issues

SQL Errors in DELETE /api/employees/:id

This one was a bit of a mystery to me. I finally did console.log with the sql variable and found the sql was missing something. Looks like the variable name didn't quite match

//DELETE /employees/:employeeId - Updates the employee with the specified employee ID to be unemployed (is_current_employee equal to 0). Returns a 200 response.
//If an employee with the supplied employee ID doesn't exist, returns a 404 response
employeesRouter.delete('/:employeeId', (req, res, next) => {
const sql = 'UPDATE Employee SET is_current_employee = $num WHERE id = $EmployeeId';
const values = {
$num: 0,
$employeeId: req.params.employeeId
};
db.run(sql, values, (error) => {
if (error) {
next(error);
} else {
db.get(`SELECT * FROM Employee WHERE id = ${req.params.employeeId}`,
(error, employee) => {
res.status(200).json({employee: employee});
});
}
});
});

the variable is employeeId but the sql query uses EmployeeId. Oops! Happens to me all the time.

Once you change it to

  const sql = 'UPDATE Employee SET is_current_employee = $num WHERE id = $employeeId';

It works

Menu test: should not delete menus with existing related menu items

I noticed this test was failing should not delete menus with existing related menu items

To pass this you should make sure your function checks to make sure there is no related menu items before it deletes it:

// DELETE
menusRouter.delete('/:menuId', (req, res, next) => {
  db.get(`SELECT * FROM MenuItem WHERE MenuItem.menu_id = ${req.params.menuId}`,
    (error, menuItem) => {
      if (menuItem) {
        res.sendStatus(400);
      } else {
        const sql = 'DELETE FROM Menu WHERE Menu.id = $menuId';
        const values = { $menuId: req.params.menuId };
        db.run(sql, values, (err) => {
          if (err) {
            next(err);
          }

          res.sendStatus(204);
        });
      }
    });
});

Summary: Needs Improvement

Hi I'm reviewing for codecademy. Great job getting this far in the course! And you've got an amazing start here, it will be easy to get it to 100%.

I found where some of the automated tests failed and I will open some issues with some tips and tricks to fix them.

I hope you find them helpful and will go on to build some nice APIs in the future!

PUT /api/employees/:employeeId/timesheets/:timesheetId tests

I noticed these tests were failing

Error: SQLITE_ERROR: no such column: hours
    1) should update the timesheet with the given ID
Error: SQLITE_ERROR: no such column: hours
    2) should return a 200 status code after timesheet update
Error: SQLITE_ERROR: no such column: hours
    3) should return the updated timesheet after timesheet update
    ✓ should return a 404 status code for invalid timesheet IDs
    ✓ should return a 400 status code for invalid timesheet updates
    ✓ should return a 404 status code if an employee with the updated employee ID doesn't exist

https://github.com/aletna/Codecademy_Final-Capstone-Project_Expresso-Web_API/blob/master/api/employees.js#L213-L245

This one can be fixed by writing the code to

  • Get the timesheet
  • Update the timesheet

Your code is acting on the employee table rather than the timesheet table: UPDATE Employee

Here's an example of how you could do it and have it work

employeesRouter.put('/:employeeId/timesheets/:timesheetId', (req, res, next) => {
  const hours = req.body.timesheet.hours,
        rate = req.body.timesheet.rate,
        date = req.body.timesheet.date,
        employeeId = req.params.employeeId,
        timesheetId = req.params.timesheetId;

    const getTimesheet = 'SELECT * FROM Timesheet WHERE Timesheet.id = $timesheetId';
      const getValues = {$timesheetId: timesheetId};
      db.get(getTimesheet, getValues, (error, timesheet) => {
        if (timesheet === undefined){
          res.sendStatus(404);
        }
      });


   if (!employeeId) {
     return res.sendStatus(404);
   }

  if (!hours || !rate || !date) {
    return res.sendStatus(400);
  }

  const sql = 'UPDATE Timesheet SET hours = $hours, rate = $rate, date = $date, employee_id = $employeeId ' +
      'WHERE Timesheet.id = $timesheetId';
  const values = {
    $hours: hours,
    $rate: rate,
    $date: date,
    $employeeId: employeeId,
    $timesheetId: timesheetId
  };

  db.run(sql, values, (error) => {
    if (error) {
      next(error);
    } else {
      db.get(`SELECT * FROM Timesheet WHERE Timesheet.id = ${req.params.timesheetId}`,
        (error, timesheet) => {
          res.status(200).json({timesheet: timesheet});
        });
    }
  });
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.