Giter Club home page Giter Club logo

forfoodsake-backend's Introduction

PROJECT 4: GA-SEIR-129 BACKEND

For Food Sake: Eat Delicious Food, Save the Planet

Concept

Food waste in the US continues to be a crippling issue which effects everyone in our community. Here are a few stats to illustrate the issue.

40% of all food in the US goes to waste
20% of landfill weight is food waste
49 million Americans struggle to put food on their table
1 in 5 children are at risk of hunger

This app is a humble contribution to a solution that may bring about a much needed change in the dominant culture of waste. We can’t build electric cars or a rocket to mars, but we can build this app in the mean time.

Overview

The idea for the project came about through a mixture of activism and sustainability. Our main focus was to create an app that promotes waste reduction through food management by bringing businesses and the community together to tackle a common issue and provide a tool to effectively fight hunger.

✴️ Click here to view our app live


Table of Contents


API Overview

The API contains two models- Vendor and Listing. The Vendor model is made up of all the information for said Vendor- name, email, password, street address, type of vendor (restaurant, farm, or market), and all the adjoining listings for that vendor. The Listing model contains all the information for any given listing- item name, quantity, price, dietary classification (vegan? vegetarian?), and vendorId (the adjoining vendor information of the vendor that the listing belongs to). So, to speak in programming terms, the Vendor model has many listings, while the Listing model belongs to Vendor. Through our API, our users can easily traverse through different vendors and all of their listings to choose which food they would like to purchase.

Technologies used

Response Request Cycle Diagram

Database Model

Testing

Vendor Data Example

{
  id: 1,
  name: 'Dollys Donuts',
  type: 'Restaurant',
  phone: '1234567890',
  email: '[email protected]',
  password: '$2b$10$uHjWS3xW/R1D7llH43dt9OQNAkgWpFBwdkiMfl6/BMIaOEcBkjs2G',
  closing_time: '12:00 AM',
  street: '123',
  city: 'New York City',
  state: 'NY',
  zip_code: '33211',
  description: 'Local, family business that know how to fry & glaze puffy dough like no other.',
  image: 'https://www.bakingbusiness.com/ext/resources/2019/8/08192019/GlobalTrends.jpg?1566494557',
  createdAt: '2017-07-04T00:00:00.000Z',
  updatedAt: '2020-03-09T16:22:27.115Z',
  Listings:
    [
      {
        id: 1,
        name: 'Glazed Donut',
        price: 1.59,
        quantity: 20,
        vegan: false,
        vegetarian: true,
        description: "Who doesn't love a classic. Soft yeast dough covered in an all natural glaze makes these donuts to die for.",
        image: 'https://www.gosoftstuff.com/web/image/product.template/28091/image?unique=23e3a82',
        createdAt: '2017-07-04T00:00:00.000Z',
        updatedAt: '2020-03-09T16:22:27.215Z',
        VendorId: 1,
      },
    ],
}

Code Samples

Example of a GET request to view all vendors:

router.get('/', async (req, res) => {
  try {
    const vendors = await Vendor.findAll({
      include: [Listing]
    });
    res.json(vendors);
  } catch (err) {
    console.error(err);
  }
});

Example of a POST request to create a new vendor with password encryption:

router.post('/new', async (req, res) => {
  // new vendor info
  let vendorToCreate = req.body;
  try {
    // hashing password
    const hashedPassword = await bcrypt.hash(req.body.password, 10);
    // replacing password with hashed password
    vendorToCreate.password = hashedPassword;

    const newVendor = await Vendor.create(vendorToCreate);
    res.json(newVendor);
  } catch {
    res.status(500).send();
  }

  // req.body should be the vendor object
});

Example of a PUT request to update an existing vendor:

router.put('/:id/edit', async (req, res) => {
  // req.body should look like this: { field: field being edited e.g.'email', value: '[email protected]' }
  const vendorToUpdate = await Vendor.findByPk(req.params.id);
  vendorToUpdate[req.body.field] = req.body.value;
  vendorToUpdate.save();
  return res.json(vendorToUpdate);
});

Bugs & Fixes

  • Trouble connecting front and back ends - moved CORS up above Express in the index.js to fix
  • 'createdAt' & 'updatedAt' keys were rendering twice in the local host. The reason for this was because we were not seeding/migrating our data properly.

Future Additions

  • Image upload with Amazon Web Service(AWS)

Installation & Contributing Instructions

Click Here

Sources


Additional documentation can be found in the frontend repository Click here

forfoodsake-backend's People

Contributors

mmarmol88 avatar qusaifares avatar rixiobarrios avatar sage-kearney avatar

Stargazers

 avatar

Watchers

 avatar

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.