Giter VIP home page Giter VIP logo

expressoapi's Introduction

Expresso

Overview

In this project, I built all of the routing and database logic for a fictional business (restaurant or small cafe).

The Expresso internal tool allows users to:

  • Create, view, update, and delete menus
  • Create, view, update, and delete menu items
  • Create, view, update, and delete employees
  • Create, view, update, and delete employee's timesheets

Starting code can be found here.

How To Begin

To start, download the code from the repository. You need Node.js to run this. Run the command npm install from the root of this project to make sure you have everything you need to start the project. Run node server.js to begin the server code.

To view the site locally, open index.html in your web browser of choice. Try using Chrome if it doesn't appear to work correctly.

Implementation Details

To complete this project, I created database tables and API routes specified below:

Database Table Properties

  • Employee

    • id - Integer, primary key, required
    • name - Text, required
    • position - Text, required
    • wage - Integer, required
    • is_current_employee - Integer, defaults to 1
  • Timesheet

    • id - Integer, primary key, required
    • hours - Integer, required
    • rate - Integer, required
    • date - Integer, required
    • employee_id - Integer, foreign key, required
  • Menu

    • id - Integer, primary key, required
    • title - Text, required
  • MenuItem

    • id - Integer, primary key, required
    • name - Text, required
    • description - Text, optional
    • inventory - Integer, required
    • price - Integer, required
    • menu_id - Integer, foreign key, required

Route Paths and Functionality

/api/employees

  • GET
    • Returns a 200 response containing all saved currently-employed employees (is_current_employee is equal to 1) on the employees property of the response body
  • POST
    • Creates a new employee with the information from the employee property of the request body and saves it to the database. Returns a 201 response with the newly-created employee on the employee property of the response body
    • If any required fields are missing, returns a 400 response

/api/employees/:employeeId

  • GET
    • Returns a 200 response containing the employee with the supplied employee ID on the employee property of the response body
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response
  • PUT
    • Updates the employee with the specified employee ID using the information from the employee property of the request body and saves it to the database. Returns a 200 response with the updated employee on the employee property of the response body
    • If any required fields are missing, returns a 400 response
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response
  • DELETE
    • 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

/api/employees/:employeeId/timesheets

  • GET
    • Returns a 200 response containing all saved timesheets related to the employee with the supplied employee ID on the timesheets property of the response body
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response
  • POST
    • Creates a new timesheet, related to the employee with the supplied employee ID, with the information from the timesheet property of the request body and saves it to the database. Returns a 201 response with the newly-created timesheet on the timesheet property of the response body
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response

/api/employees/:employeeId/timesheets/:timesheetId

  • PUT
    • Updates the timesheet with the specified timesheet ID using the information from the timesheet property of the request body and saves it to the database. Returns a 200 response with the updated timesheet on the timesheet property of the response body
    • If any required fields are missing, returns a 400 response
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response
    • If an timesheet with the supplied timesheet ID doesn't exist, returns a 404 response
  • DELETE
    • Deletes the timesheet with the supplied timesheet ID from the database. Returns a 204 response.
    • If an employee with the supplied employee ID doesn't exist, returns a 404 response
    • If an timesheet with the supplied timesheet ID doesn't exist, returns a 404 response

/api/menus

  • GET
    • Returns a 200 response containing all saved menus on the menus property of the response body
  • POST
    • Creates a new menu with the information from the menu property of the request body and saves it to the database. Returns a 201 response with the newly-created menu on the menu property of the response body
    • If any required fields are missing, returns a 400 response

/api/menus/:menuId

  • GET
    • Returns a 200 response containing the menu with the supplied menu ID on the menu property of the response body
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response
  • PUT
    • Updates the menu with the specified menu ID using the information from the menu property of the request body and saves it to the database. Returns a 200 response with the updated menu on the menu property of the response body
    • If any required fields are missing, returns a 400 response
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response
  • DELETE
    • Deletes the menu with the supplied menu ID from the database if that menu has no related menu items. Returns a 204 response.
    • If the menu with the supplied menu ID has related menu items, returns a 400 response.
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response

/api/menus/:menuId/menu-items

  • GET
    • Returns a 200 response containing all saved menu items related to the menu with the supplied menu ID on the menu items property of the response body
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response
  • POST
    • Creates a new menu item, related to the menu with the supplied menu ID, with the information from the menuItem property of the request body and saves it to the database. Returns a 201 response with the newly-created menu item on the menuItem property of the response body
    • If any required fields are missing, returns a 400 response
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response

/api/menus/:menuId/menu-items/:menuItemId

  • PUT
    • Updates the menu item with the specified menu item ID using the information from the menuItem property of the request body and saves it to the database. Returns a 200 response with the updated menu item on the menuItem property of the response body
    • If any required fields are missing, returns a 400 response
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response
    • If a menu item with the supplied menu item ID doesn't exist, returns a 404 response
  • DELETE
    • Deletes the menu item with the supplied menu item ID from the database. Returns a 204 response.
    • If a menu with the supplied menu ID doesn't exist, returns a 404 response
    • If a menu item with the supplied menu item ID doesn't exist, returns a 404 response

Testing

A testing suite has is provided that checks for all essential functionality and edge cases.

To run these tests, first, open the root project directory in your terminal. Then run npm install to install all necessary testing dependencies (if you haven't already). Finally, run npm test. You will see a list of tests that ran with information about whether or not each test passed. All should pass currently.

expressoapi's People

Contributors

mrandrew avatar

Watchers

James Cloos avatar

expressoapi's Issues

Summary

Overall good job Andrew, I saw no major issues with your code and all your tests pass. So I do indeed like these apples. For the one issue you did have, if you are still running into it take a look at the code I provided in that issue. Also if you have any questions feel free to ask the advisors.

Let me know as well if you have any questions. Again great job.

Add, Delete, Save, Edit etc.

After testing your repository I was able to successfully add, edit, save and delete all for both menu/item and the employee/timesheet. I saw in one of your comments this:

//checks if timesheet is associated with a valid employee
//CURRENT TEST WILL SOMETIMES THROW AN ERROR HERE BECAUSE IT SENDS
//AN INVALID TIMESHEET OBJECT AT THE SAME TIME AS AN INVALID EMPLOYEE ID
//(test.js: line 634), BUT THIS CODE IS STILL FUNCTIONAL AND CATCHES
//WRONG INPUT, TEST IS JUST WRITTEN WEIRD, I SEE NO NOTICEABLE CHANGE
// IN APP FUNCTION, TEST ONLY SOMETIMES RETURNS THIS ERROR

I was unable to reproduce your error, do you still sometimes run into this?

Even though what you wrote works I was unable to see why you would sometimes get the error you got, if you would like to compare and perhaps see how you can get rid of a few lines of code you have take a look here, this is the solution code:

timeSheetRouter.param('timesheetId', (req, res, next, id) => {
    db.get('SELECT * FROM Timesheet WHERE id = $id',
        {
            $id: id
        }, 
        (err, result) => {
            if (err) {
                next(err);
            } else if (result) {
                req.timesheet = result;
                next();
            } else {
                res.sendStatus(404);
            }
        })
});

/////

timeSheetRouter.post('/', (req, res, next) => {

    const hours = req.body.timesheet.hours;
    const rate = req.body.timesheet.rate;
    const date = req.body.timesheet.date;

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

    db.run('INSERT INTO Timesheet (hours, rate, date ,employee_id) VALUES ($hours, $rate, $date, $employee_id)',
        {
            $hours: hours,
            $rate: rate,
            $date: date,
            $employee_id: req.employee.id
        }, 
        (err) => {
            if (err) {
                throw (err);
            } else {
                db.get('SELECT * FROM Timesheet WHERE id = last_insert_rowid()',
                    (err, result) => {
                        res.status(201).json({ timesheet: result });
                    })
            }
        })
});

NPM Test

89 passing that is all the tests, good job Mr. Andrew on getting all your tests to pass.

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.