Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User can add to order #10

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/controllers/api/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Api::OrdersController < ApplicationController
def create
order = Order.create
order.order_items.create(product_id: params[:product_id])
render json: {message: 'The product has been added to your order', order_id: order.id }
end

def update
order = Order.find(params[:id])
product = Product.find(params[:product_id])
order.order_items.create(product: product)
render json: { message: 'The product has been added to your order', order_id: order.id }
end

end
6 changes: 6 additions & 0 deletions app/controllers/api/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Api::ProductsController < ApplicationController
def index
product = Product.all
render json: { products: product }
end
end
6 changes: 0 additions & 6 deletions app/controllers/api/v0/pings_controller.rb

This file was deleted.

6 changes: 0 additions & 6 deletions app/controllers/api/v0/product_controller.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Order < ApplicationRecord
has_many :order_items
end
4 changes: 4 additions & 0 deletions app/models/order_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class OrderItem < ApplicationRecord
belongs_to :order
belongs_to :product
end
3 changes: 1 addition & 2 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'rails_helper'
class Product < ApplicationRecord
validates_presence_of :name, :description, :price, :allergens
validates_presence_of :name, :description, :price
end
3 changes: 0 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

class User < ActiveRecord::Base
extend Devise::Models
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
include DeviseTokenAuth::Concerns::User
Expand Down
7 changes: 3 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'api/v1/auth', skip: [:omniauth_callbacks]
mount_devise_token_auth_for 'User', at: 'api/auth', skip: [:omniauth_callbacks]
namespace :api do
resources :products, only: [:index], constraints: { format: 'json' }
namespace :v1, defaults: { format: :json } do
end
resources :products, only: [:index]
resources :orders, only: [:create, :update]
end
end
12 changes: 0 additions & 12 deletions db/migrate/20200303141509_create_products.rb

This file was deleted.

11 changes: 11 additions & 0 deletions db/migrate/20200308114522_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateItems < ActiveRecord::Migration[6.0]
def change
create_table :items do |t|
t.string :name
t.string :description
t.decimal :price

t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20200308152106_create_orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateOrders < ActiveRecord::Migration[6.0]
def change
create_table :orders do |t|

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20200308152215_create_order_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateOrderItems < ActiveRecord::Migration[6.0]
def change
create_table :order_items do |t|
t.references :order, null: false, foreign_key: true
t.references :product, null: false, foreign_key: true

t.timestamps
end
end
end
26 changes: 25 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,33 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_03_05_152407) do
ActiveRecord::Schema.define(version: 2020_03_08_152215) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "items", force: :cascade do |t|
t.string "name"
t.string "description"
t.decimal "price"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "order_items", force: :cascade do |t|
t.bigint "order_id", null: false
t.bigint "product_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["order_id"], name: "index_order_items_on_order_id"
t.index ["product_id"], name: "index_order_items_on_product_id"
end

create_table "orders", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "products", force: :cascade do |t|
t.string "name"
t.string "description"
Expand Down Expand Up @@ -51,4 +73,6 @@
t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true
end

add_foreign_key "order_items", "orders"
add_foreign_key "order_items", "products"
end
12 changes: 12 additions & 0 deletions spec/controllers/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

RSpec.describe Api::OrdersController, type: :controller do

describe "GET #create" do
it "returns http success" do
get :create
expect(response).to have_http_status(:success)
end
end

end
7 changes: 7 additions & 0 deletions spec/factories/items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :item do
name { "MyString" }
description { "MyString" }
price { "9.99" }
end
end
6 changes: 6 additions & 0 deletions spec/factories/order_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :order_item do
order { nil }
product { nil }
end
end
5 changes: 5 additions & 0 deletions spec/factories/orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :order do

end
end
1 change: 0 additions & 1 deletion spec/factories/products.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
name { "Noodles" }
description { "Italian linguine" }
price { 1.5 }
allergens { "gluten" }
end
end
6 changes: 6 additions & 0 deletions spec/models/order_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rails_helper'

RSpec.describe OrderItem, type: :model do
it {is_expected.to belong_to :order}
it {is_expected.to belong_to :product}
end
5 changes: 5 additions & 0 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Order, type: :model do
it {is_expected.to have_many :order_items}
end
31 changes: 13 additions & 18 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe Product, type: :model do

it 'should have valid Factory' do
expect(create(:product)).to be_valid
describe "Database table" do
it { is_expected.to have_db_column :id }
it { is_expected.to have_db_column :name }
it { is_expected.to have_db_column :description }
it { is_expected.to have_db_column :price }
end

describe 'Database table' do
it { is_expected.to have_db_column :name}
it { is_expected.to have_db_column :description}
it { is_expected.to have_db_column :price}
it { is_expected.to have_db_column :allergens}
describe "Validations" do
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :description }
it { is_expected.to validate_presence_of :price }
end

describe 'GET product' do
let!(:products) do
3.times do
FactoryBot.create(:product)
end
end

it 'returns a collection of products' do
expect(products).to eq 3
describe "Factory" do
it "should have valid Factory" do
expect(create(:product)).to be_valid
end
end
end
10 changes: 5 additions & 5 deletions spec/requests/api/v1/registration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
RSpec.describe "POST /api/v1/auth", type: :request do
RSpec.describe "POST /api/auth", type: :request do
let(:headers) { { HTTP_ACCEPT: 'application/json' } }
describe 'with valid credentials' do
before do
post '/api/v1/auth',
post '/api/auth',
params: {
email: '[email protected]',
password: 'password',
Expand All @@ -20,7 +20,7 @@
context 'when a user submits' do
describe 'a non-matching password confirmation' do
before do
post '/api/v1/auth',
post '/api/auth',
params: {
email: '[email protected]',
password: 'password',
Expand All @@ -37,7 +37,7 @@
end
describe 'an invalid email address' do
before do
post '/api/v1/auth',
post '/api/auth',
params: {
email: 'example@craft',
password: 'password',
Expand All @@ -55,7 +55,7 @@
describe 'an already registered email' do
let!(:registered_user) { create(:user, email: '[email protected]') }
before do
post '/api/v1/auth',
post '/api/auth',
params: {
email: '[email protected]',
password: 'password',
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/api/v1/session_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe 'POST /api/vi/auth/sign_in', type: :request do
RSpec.describe 'POST /api/auth/sign_in', type: :request do
let(:headers) { { HTTP_ACCEPT: 'application/json' } }
let(:user) { create(:user) }
let(:expected_response) do
Expand All @@ -11,7 +11,7 @@
end
describe 'with valid credentials' do
before do
post '/api/v1/auth/sign_in',
post '/api/auth/sign_in',
params: {
email: user.email,
password: user.password
Expand All @@ -30,7 +30,7 @@

describe 'with invalid password' do
before do
post '/api/v1/auth/sign_in',
post '/api/auth/sign_in',
params: {
email: user.email,
password: 'wrong_password'
Expand All @@ -49,7 +49,7 @@

describe 'with invalid email' do
before do
post '/api/v1/auth/sign_in',
post '/api/auth/sign_in',
params: {
email: '[email protected]',
password: user.password
Expand Down
35 changes: 35 additions & 0 deletions spec/requests/api/v1/user_can_create_new_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
RSpec.describe Api::OrdersController, type: :request do
let!(:product_1) { create(:product, name: "Fish") }
let!(:product_2) { create(:product, name: "Meatball") }

before do
post "/api/orders", params: { product_id: product_1.id }
@order_id = JSON.parse(response.body)['order_id']
end

describe 'POST /api/orders' do
it 'responds with success message' do
expect(JSON.parse(response.body)['message']).to eq 'The product has been added to your order'
end

it 'responds with order id' do
order = Order.find(@order_id)
expect(JSON.parse(response.body)['order_id']).to eq order.id
end
end

describe 'PUT /api/orders/:id' do
before do
put "/api/orders/#{@order_id}", params: { product_id: product_2.id }
@order = Order.find(@order_id)
end

it 'adds another product to order if request is a PUT and param id of the order is present' do
expect(@order.order_items.count).to eq 2
end

it 'responds with order id' do
expect(JSON.parse(response.body)['order_id']).to eq @order.id
end
end
end
17 changes: 17 additions & 0 deletions spec/requests/products_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.describe Api::ProductsController, type: :request do
let!(:products) { 3.times { create(:product) } }

describe "GET /product" do
before do
get "/api/products"
end

it "should return a 200 response" do
expect(response).to have_http_status 200
end

it "should return three products" do
expect(JSON.parse(response.body)["products"].count).to eq 3
end
end
end