Giter Site home page Giter Site logo

rails-mini-course-sprint2-mod1-student--starter's Introduction

Ruby on Rails - Intermediate Ruby on Rails - Migrations, Routing, Controllers - Project

Setup

Clone the application and then bundle install. Then, create, migrate, and seed the database.

Intro

This app is a partially completed API that will power a warehouse fulfillment client application. We have already built the customer and products portions but need to add orders and a way to associate orders with products.

Here is a breakdown of the existing data models for the application:

Customer

attribute type
id integer
email string
created_at datetime
updated_at datetime

Product

attribute type
id integer
name string
cost_cents integer
inventory integer
created_at datetime
updated_at datetime

Here is a breakdown of the existing resource API for the application:

Resources

verb resource route controller#action note
GET customer /api/v1/customers api/v1/customers#index list all customers
POST customer /api/v1/customers api/v1/customers#create create a customer
GET customer /api/v1/customers/:id api/v1/customers#show get a customer
PATCH customer /api/v1/customers/:id api/v1/customers#update update a customer
PUT customer /api/v1/customers/:id api/v1/customers#update update a customer
DELETE customer /api/v1/customers/:id api/v1/customers#destroy delete a customer
GET product /api/v1/products api/v1/products#index list all products
GET product /api/v1/products/:id api/v1/products#show get a specific product

Step One - Add Order and OrderProduct Models

Our application has customers and products, but now it needs to manage orders and order_products. For now we just need to set up the model files and the database to match our specifications below. Our application will be able to create and ship orders and add products to orders via order_products.

Order

attribute type
id integer
status string
customer_id integer
created_at datetime
updated_at datetime

OrderProduct

attribute type
id integer
order_id integer
product_id integer
created_at datetime
updated_at datetime
  1. Create and run a migration for the orders table with the above columns and types
  2. Create an Order ActiveRecord model file
  3. Create and run a migration for the order_products table with the above columns and types
  4. Create an OrderProduct ActiveRecord model file

Step Two - Add Order Routes

We need to add new routes for our orders resource.

verb resource route controller#action note
GET order /api/v1/orders api/v1/orders#index list all orders
GET order /api/v1/customers/:customer_id/orders api/v1/orders#index list all orders for a customer
POST order /api/v1/customers/:customer_id/orders api/v1/orders#create create an order for a customer
GET order /api/v1/orders/:id api/v1/orders#show get a specific order
POST order /api/v1/orders/:id/ship api/v1/orders#ship ship a specific order
GET product /api/v1/orders/:order_id/products api/v1/products#index list all products for an order
POST product /api/v1/orders/:order_id/products api/v1/products#create add a product to an order
  1. Add the order route to list all orders
  2. Add the order route to show a specific order
  3. Add a custom POST member route to the orders resource to ship an order
  4. Nested within the order resource, add the products routes to list and create products
  5. Nested within the customer resource, add the order routes to list and create orders
  6. Limit new routes to only the ones expected above

Step Three - Add Orders Controller

Create an orders controller and implement the index, create, show and ship actions to respond to the routes you created. All the actions should return an appropriate JSON response.

  1. The orders index should work for showing all orders and orders scoped to a specific customer

    • If a customer_id param is present, set @orders = Order.where(customer_id: params[:customer_id])
    • If customer_id is not present, set @orders = Order.all
    • Render @orders as json
  2. The orders create action should set the customer_id attribute and default the status to "pending"

  3. The orders show action should get a specific order from the database

  4. The custom ship action should update the order status to "shipped" and render the order as JSON

  5. There should be no update or destroy actions

rails-mini-course-sprint2-mod1-student--starter's People

Contributors

mattmccarley avatar bnbry avatar

Watchers

James Cloos avatar Soumya Ghosh avatar  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.