Giter Site home page Giter Site logo

scaling-adventure's Introduction

Rails Code Challenge - Sweets Vendors

For this assessment, you'll be working with a vendors and sweets domain.

In this repo, there is a Flask application with some features built out. There is also a fully built React frontend application, so you can test if your API is working.

Your job is to build out the Flask API to add the functionality described in the deliverables below.

Setup

To download the dependencies for the frontend and backend, run:

pipenv install
npm install --prefix client

There is some starter code in the app/seed.py file so that once you've generated the models, you'll be able to create data to test your application.

You can run your Flask API on localhost:5555 by running:

python app.py

You can run your React app on localhost:4000 by running:

npm start --prefix client

You are not being assessed on React, and you don't have to update any of the React code; the frontend code is available just so that you can test out the behavior of your API in a realistic setting.

Depending on your preference, you can either check your progress by:

  • Running the React application in the browser and interacting with the API via the frontend
  • Running the Flask server and using Postman to make requests

Models

You need to create the following relationships:

  • A Vendor has many Sweets through VendorSweet
  • A Sweet has many Vendors through VendorSweet
  • A VendorSweet belongs to a Vendor and belongs to a Sweet

Start by creating the models and migrations for the following database tables:

domain diagram

Add any code needed in the model files to establish the relationships.

Then, run the migrations and seed file:

flask db revision --autogenerate -m'message'
flask db upgrade
python seed.py

If you aren't able to get the provided seed file working, you are welcome to generate your own seed data to test the application.

Routes

Set up the following routes. Make sure to return JSON data in the format specified along with the appropriate HTTP verb.

GET /vendors

Return JSON data in the format below:

[
  { "id": 1, "name": "Insomnia Cookies" },
  { "id": 2, "name": "Cookies Cream" }
]

GET /vendors/:id

If the Vendor exists, return JSON data in the format below:

{
  "id": 1,
  "name": "Insomnia Cookies",
  "vendor_sweets": [
    {
      "id": 5,
      "name": "Chocolate Chip Cookies",
      "price": 200
    },
    {
      "id": 6,
      "name": "Brownies",
      "price": 300
    }
  ]
}

Note: The name attribute in the vendor_sweets array represents the name of the Sweet that the VendorSweet belongs to.

If the Vendor does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Vendor not found"
}

GET /sweets

Return JSON data in the format below:

[
  {
    "id": 1,
    "name": "Chocolate Chip Cookie"
  },
  {
    "id": 2,
    "name": "Brownie"
  }
]

GET /sweets/:id

If the Sweet exists, return JSON data in the format below:

{
  "id": 1,
  "name": "Chocolate Chip Cookie"
}

If the Sweet does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Sweet not found"
}

POST /vendor_sweets

This route should create a new VendorSweet that is associated with an existing Vendor and Sweet. It should accept an object with the following properties in the body of the request:

{
  "price": 300,
  "vendor_id": 1,
  "sweet_id": 3
}

If the VendorSweet is created successfully, send back a response with the following data:

{
  "id": 5,
  "name": "Chocolate Chip Cookies",
  "price": 300
}

Note: The name attribute in the response represents the name of the Sweet that the VendorSweet belongs to.

STRETCH GOAL: If the VendorSweet is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

DELETE /vendor_sweets/:id

This route should delete an existing VendorSweet. If the VendorSweet exists and is deleted successfully, return an empty object as a response:

{}

If the VendorSweet does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "VendorSweet not found"
}

Validations (Stretch Goals)

Add validations to the VendorSweet model:

  • price cannot be blank
  • price cannot be a negative number

scaling-adventure's People

Contributors

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