From 85e1c4237932a830197398daa97cd20bdae32eba Mon Sep 17 00:00:00 2001 From: danriser Date: Thu, 20 Oct 2022 12:11:12 -0300 Subject: [PATCH 1/5] genre c created --- app/models/product.rb | 1 + app/models/user.rb | 2 ++ spec/factories/carts.rb | 4 ++-- spec/factories/users.rb | 2 +- spec/models/cart_spec.rb | 4 +++- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/product.rb b/app/models/product.rb index ebfa668..06a18d8 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,6 +1,7 @@ class Product < ApplicationRecord belongs_to :brand belongs_to :category + has_many :carts validates :price, presence: :true validates :brand_id, presence: :true validates :category_id, presence: :true diff --git a/app/models/user.rb b/app/models/user.rb index 8aab811..ebe8a48 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,7 @@ class User < ApplicationRecord + has_many :carts validates :name, presence: :true validates :email, presence: :true, uniqueness: :true validates :password, presence: :true + end diff --git a/spec/factories/carts.rb b/spec/factories/carts.rb index 3616aa1..2dbc829 100644 --- a/spec/factories/carts.rb +++ b/spec/factories/carts.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :cart do - product { nil } - user { nil } + product { create(:product) } + user { create(:user) } end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 24ab4fa..f60f614 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -4,6 +4,6 @@ email { "MyString" } password { "MyString" } wallet { 1 } - is_admin { false } + is_admin { false } end end diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb index 5985277..88a3a8f 100644 --- a/spec/models/cart_spec.rb +++ b/spec/models/cart_spec.rb @@ -1,5 +1,7 @@ require 'rails_helper' RSpec.describe Cart, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + context "Factory test" do + it {expect(build(:cart)).to be_valid} + end end From e86686562e14a46f376828807bbe3180410c06f6 Mon Sep 17 00:00:00 2001 From: danriser Date: Fri, 21 Oct 2022 11:56:45 -0300 Subject: [PATCH 2/5] create category done --- app/controllers/api/v1/category_controller.rb | 25 ++++++++++ config/routes.rb | 10 +++- db/seeds.rb | 28 ++++++++--- spec/requests/api/v1/category_spec.rb | 50 +++++++++++++++++++ 4 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 app/controllers/api/v1/category_controller.rb create mode 100644 spec/requests/api/v1/category_spec.rb diff --git a/app/controllers/api/v1/category_controller.rb b/app/controllers/api/v1/category_controller.rb new file mode 100644 index 0000000..60b8f39 --- /dev/null +++ b/app/controllers/api/v1/category_controller.rb @@ -0,0 +1,25 @@ +class Api::V1::CategoryController < ApplicationController + def index + category = Category.all + render json: category, status: :ok + end + + def show + category= Category.find(params[:id]) + render json: category, status: :ok + rescue StandardError => e + render json: e, status: :not_found + end + + def create + category = Category.new(category_params) + category.save! + render json: category, status: :created + rescue StandardError => e + render json: e, status: :bad_request + end + private + def category_params + params.require(:category).permit(:name) + end +end diff --git a/config/routes.rb b/config/routes.rb index c06383a..bea3f46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,11 @@ Rails.application.routes.draw do - # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + namespace'api' do + namespace 'v1' do + scope 'category' do + get 'index', to: 'category#index' + get 'show/:id', to: 'category#show' + post 'create', to: 'category#create' + end + end + end end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..04a78cd 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,21 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +Category.create!(name: "Gadgets") +Category.create!(name: "Clothes") +Category.create!(name: "Games") + +Brand.create!(name: "Macrohard") +Brand.create!(name: "Vanz") +Brand.create!(name: "Nontendi") + +Products.create!(id:1,name: "Nontendi Switch",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:2,name: "Mouse rgb 50000 dpi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:3,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:4,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:5,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:6,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:7,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:8,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:9,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Products.create!(id:10,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) + + + diff --git a/spec/requests/api/v1/category_spec.rb b/spec/requests/api/v1/category_spec.rb new file mode 100644 index 0000000..3c4d0a5 --- /dev/null +++ b/spec/requests/api/v1/category_spec.rb @@ -0,0 +1,50 @@ +require 'rails_helper' + +RSpec.describe "Api::V1::Categories", type: :request do + describe "GET /index" do + before do + create(:category, id:1, name: 'Beauty') + create(:category, id:2, name: 'Housing') + end + + + it 'return http status ok' do + get '/api/v1/category/index' + expect(response) .to have_http_status(:ok) + end + end + describe 'GET /show' do + let(:category) {create(:category)} + context 'id does exist' do + before do + get"/api/v1/category/show/#{genre.id}" + end + end + context'id not found' do + before do + get "/api/v1/category/show/-1" + end + it 'return http status not_found' do + expect(response).to have_http_status(:not_found) + end + end + end + describe 'POST /create' do + let(:category_params) do + attributes_for(:category) + end + context 'params check ok' do + it 'return http status created' do + p category_params + post "/api/v1/category/create", params:{category: category_params} + expect(response).to have_http_status(:created) + end + end + context 'bad params' do + it 'when param is nil' do + post "/api/v1/category/create", params:{category: category_params} + expect(response).to have_http_status(:bad_request) + end + end + end +end From f38f8c102a92b78a400aff8c0d3e19268b047345 Mon Sep 17 00:00:00 2001 From: danriser Date: Fri, 21 Oct 2022 14:06:58 -0300 Subject: [PATCH 3/5] category controller done --- app/controllers/api/v1/brand_controller.rb | 2 ++ app/controllers/api/v1/category_controller.rb | 22 +++++++++++- config/routes.rb | 2 ++ db/seeds.rb | 20 +++++------ spec/requests/api/v1/brand_spec.rb | 7 ++++ spec/requests/api/v1/category_spec.rb | 36 ++++++++++++++++++- 6 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 app/controllers/api/v1/brand_controller.rb create mode 100644 spec/requests/api/v1/brand_spec.rb diff --git a/app/controllers/api/v1/brand_controller.rb b/app/controllers/api/v1/brand_controller.rb new file mode 100644 index 0000000..16641c5 --- /dev/null +++ b/app/controllers/api/v1/brand_controller.rb @@ -0,0 +1,2 @@ +class Api::V1::BrandController < ApplicationController +end diff --git a/app/controllers/api/v1/category_controller.rb b/app/controllers/api/v1/category_controller.rb index 60b8f39..b3e491b 100644 --- a/app/controllers/api/v1/category_controller.rb +++ b/app/controllers/api/v1/category_controller.rb @@ -18,7 +18,27 @@ def create rescue StandardError => e render json: e, status: :bad_request end - private + + + def update + category = Category.find(params[:id]) + category.update!(category_params) + render json: category, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + def delete + category = Category.find(params[:id]) + category.destroy! + render json: category, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + + + private def category_params params.require(:category).permit(:name) end diff --git a/config/routes.rb b/config/routes.rb index bea3f46..cbb02ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,8 @@ get 'index', to: 'category#index' get 'show/:id', to: 'category#show' post 'create', to: 'category#create' + patch 'update/:id', to: 'category#update' + delete 'delete/:id', to: 'category#delete' end end end diff --git a/db/seeds.rb b/db/seeds.rb index 04a78cd..98e38a5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,16 +6,16 @@ Brand.create!(name: "Vanz") Brand.create!(name: "Nontendi") -Products.create!(id:1,name: "Nontendi Switch",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:2,name: "Mouse rgb 50000 dpi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:3,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:4,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:5,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:6,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:7,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:8,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:9,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) -Products.create!(id:10,name: "Nontendi",description: " " price: 200,stock: 45,brand_id:1 ,category_id: ) +Product.create!(id:1,name: "Nontendi Switch",description: " ", price: 200,stock: 45,brand_id:1 ,category_id:1 ) +Product.create!(id:2,name: "Mouse rgb 50000 dpi",description: " " ,price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:3,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:4,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:5,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:6,name: "Nontendi",description: " " ,price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:7,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:8,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:9,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:10,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) diff --git a/spec/requests/api/v1/brand_spec.rb b/spec/requests/api/v1/brand_spec.rb new file mode 100644 index 0000000..cea5bcb --- /dev/null +++ b/spec/requests/api/v1/brand_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe "Api::V1::Brands", type: :request do + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +end diff --git a/spec/requests/api/v1/category_spec.rb b/spec/requests/api/v1/category_spec.rb index 3c4d0a5..f3935be 100644 --- a/spec/requests/api/v1/category_spec.rb +++ b/spec/requests/api/v1/category_spec.rb @@ -42,9 +42,43 @@ end context 'bad params' do it 'when param is nil' do - post "/api/v1/category/create", params:{category: category_params} + post "/api/v1/category/create", params:{category: nil} expect(response).to have_http_status(:bad_request) end end + end + describe 'PATCH/update' do + let(:category) do + create(:category,name: "Kitchen") + end + context 'params ok' do + it 'return http status ok' do + patch "/api/v1/category/update/#{category.id}", params:{category: {name:"Utility"}} + expect(response).to have_http_status(:ok) + end + end + context 'bad params'do + it 'when params is nill' do + patch "/api/v1/category/update/#{category.id}", params:{category: {name: nil}} + expect(response).to have_http_status(:bad_request) + end + end + end + describe 'DELETE/delete_id' do + let(:category) do + create(:category) + end + context 'params ok' do + it 'return http status ok' do + delete "/api/v1/category/delete/#{category.id}" + expect(response).to have_http_status(:ok) + end + end + context 'bad params' do + it 'params is nill' do + delete "/api/v1/category/delete/-1" + expect(response).to have_http_status(:bad_request) + end + end end end From 2e7ec76fc35144810d4f0dfaef170dc73e963070 Mon Sep 17 00:00:00 2001 From: danriser Date: Mon, 24 Oct 2022 11:55:18 -0300 Subject: [PATCH 4/5] finished products controllers --- app/controllers/api/v1/brand_controller.rb | 44 ++++++++++ app/controllers/api/v1/product_controller.rb | 43 +++++++++ app/models/product.rb | 2 + config/routes.rb | 16 +++- db/seeds.rb | 20 ++--- spec/factories/categories.rb | 5 +- spec/models/product_spec.rb | 2 + spec/requests/api/v1/brand_spec.rb | 79 ++++++++++++++++- spec/requests/api/v1/product_spec.rb | 91 ++++++++++++++++++++ 9 files changed, 289 insertions(+), 13 deletions(-) create mode 100644 app/controllers/api/v1/product_controller.rb create mode 100644 spec/requests/api/v1/product_spec.rb diff --git a/app/controllers/api/v1/brand_controller.rb b/app/controllers/api/v1/brand_controller.rb index 16641c5..e0081e3 100644 --- a/app/controllers/api/v1/brand_controller.rb +++ b/app/controllers/api/v1/brand_controller.rb @@ -1,2 +1,46 @@ class Api::V1::BrandController < ApplicationController + def index + brand = Brand.all + render json: brand, status: :ok + end + + def show + brand= Brand.find(params[:id]) + render json: brand, status: :ok + rescue StandardError => e + render json: e, status: :not_found + end + + def create + brand = Brand.new(brand_params) + brand.save! + render json: brand, status: :created + rescue StandardError => e + render json: e, status: :bad_request + end + + + def update + brand = Brand.find(params[:id]) + brand.update!(brand_params) + render json: brand, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + def delete + brand = Brand.find(params[:id]) + brand.destroy! + render json: brand, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + + + private + def brand_params + params.require(:brand).permit(:name) + end + end diff --git a/app/controllers/api/v1/product_controller.rb b/app/controllers/api/v1/product_controller.rb new file mode 100644 index 0000000..3169cc1 --- /dev/null +++ b/app/controllers/api/v1/product_controller.rb @@ -0,0 +1,43 @@ +class Api::V1::ProductController < ApplicationController + def index + product = Product.all + render json: product, status: :ok + end + + def show + product= Product.find(params[:id]) + render json: product, status: :ok + rescue StandardError => e + render json: e, status: :not_found + end + + def create + product = Product.new(product_params) + product.save! + render json: product, status: :created + rescue StandardError => e + render json: e, status: :bad_request + end + + + def update + product = Product.find(params[:id]) + product.update!(product_params) + render json: product, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + def delete + product = Product.find(params[:id]) + product.destroy! + render json: product, status: :ok + rescue StandardError => e + render json: e, status: :bad_request + end + + private + def product_params + params.require(:product).permit(:name,:description,:price,:stock,:brand_id,:category_id) + end +end diff --git a/app/models/product.rb b/app/models/product.rb index 06a18d8..a119afa 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -2,6 +2,8 @@ class Product < ApplicationRecord belongs_to :brand belongs_to :category has_many :carts + validates :name, presence: :true + validates :description, presence: :true validates :price, presence: :true validates :brand_id, presence: :true validates :category_id, presence: :true diff --git a/config/routes.rb b/config/routes.rb index cbb02ef..8f8b1db 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,20 @@ patch 'update/:id', to: 'category#update' delete 'delete/:id', to: 'category#delete' end - end + scope 'brand' do + get 'index', to: 'brand#index' + get 'show/:id', to: 'brand#show' + post 'create', to: 'brand#create' + patch 'update/:id', to: 'brand#update' + delete 'delete/:id', to: 'brand#delete' + end + scope 'product' do + get 'index', to: 'product#index' + get 'show/:id', to: 'product#show' + post 'create', to: 'product#create' + patch 'update/:id', to: 'product#update' + delete 'delete/:id', to: 'product#delete' + end + end end end diff --git a/db/seeds.rb b/db/seeds.rb index 98e38a5..32cd0ba 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,16 +6,16 @@ Brand.create!(name: "Vanz") Brand.create!(name: "Nontendi") -Product.create!(id:1,name: "Nontendi Switch",description: " ", price: 200,stock: 45,brand_id:1 ,category_id:1 ) -Product.create!(id:2,name: "Mouse rgb 50000 dpi",description: " " ,price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:3,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:4,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:5,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:6,name: "Nontendi",description: " " ,price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:7,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:8,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:9,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:10,name: "Nontendi",description: " ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:1,name: "Nontendi Switch",description: "vg console ", price: 200,stock: 45,brand_id:1 ,category_id:1 ) +Product.create!(id:2,name: "Mouse rgb 50000 dpi",description: "shiny mouse" ,price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:3,name: "Drip shoes",description: "good quality shoes", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:4,name: "T-shirt",description: "cool shirt ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:5,name: "Nontendi",description: "oi ", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:6,name: "Nontendi",description: " oi" ,price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:7,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:8,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:9,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:10,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb index 2c4ede5..11cf385 100644 --- a/spec/factories/categories.rb +++ b/spec/factories/categories.rb @@ -1,5 +1,8 @@ FactoryBot.define do + sequence :categories do |e| + "category#{e}" + end factory :category do - name { "MyString" } + name {generate(:categories)} end end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 9eb7e51..a369c20 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -31,4 +31,6 @@ end end + + end diff --git a/spec/requests/api/v1/brand_spec.rb b/spec/requests/api/v1/brand_spec.rb index cea5bcb..9baff9c 100644 --- a/spec/requests/api/v1/brand_spec.rb +++ b/spec/requests/api/v1/brand_spec.rb @@ -2,6 +2,83 @@ RSpec.describe "Api::V1::Brands", type: :request do describe "GET /index" do - pending "add some examples (or delete) #{__FILE__}" + before do + create(:brand, id:1, name: 'Beauty') + create(:brand, id:2, name: 'Housing') + end + + + it 'return http status ok' do + get '/api/v1/brand/index' + expect(response) .to have_http_status(:ok) + end + end + describe 'GET /show' do + let(:brand) {create(:brand)} + context 'id does exist' do + before do + get"/api/v1/brand/show/#{brand.id}" + end + end + context'id not found' do + before do + get "/api/v1/brand/show/-1" + end + it 'return http status not_found' do + expect(response).to have_http_status(:not_found) + end + end + end + describe 'POST /create' do + let(:brand_params) do + attributes_for(:brand) + end + context 'params check ok' do + it 'return http status created' do + p brand_params + post "/api/v1/brand/create", params:{brand: brand_params} + expect(response).to have_http_status(:created) + end + end + context 'bad params' do + it 'when param is nil' do + post "/api/v1/brand/create", params:{brand: nil} + expect(response).to have_http_status(:bad_request) + end + end end + describe 'PATCH/update' do + let(:brand) do + create(:brand,name: "Kitchen") + end + context 'params ok' do + it 'return http status ok' do + patch "/api/v1/brand/update/#{brand.id}", params:{brand: {name:"Utility"}} + expect(response).to have_http_status(:ok) + end + end + context 'bad params'do + it 'when params is nill' do + patch "/api/v1/brand/update/#{brand.id}", params:{brand: {name: nil}} + expect(response).to have_http_status(:bad_request) + end + end + end + describe 'DELETE/delete_id' do + let(:brand) do + create(:brand) + end + context 'params ok' do + it 'return http status ok' do + delete "/api/v1/brand/delete/#{brand.id}" + expect(response).to have_http_status(:ok) + end + end + context 'bad params' do + it 'params is nill' do + delete "/api/v1/brand/delete/-1" + expect(response).to have_http_status(:bad_request) + end + end + end end diff --git a/spec/requests/api/v1/product_spec.rb b/spec/requests/api/v1/product_spec.rb new file mode 100644 index 0000000..58b7fa3 --- /dev/null +++ b/spec/requests/api/v1/product_spec.rb @@ -0,0 +1,91 @@ +require 'rails_helper' + +RSpec.describe "Api::V1::Products", type: :request do + describe "GET /index" do + it 'return http status ok' do + get '/api/v1/product/index' + expect(response) .to have_http_status(:ok) + end + end + describe 'GET /show' do + let(:product) {create(:product)} + context 'id does exist' do + before do + get"/api/v1/product/show/#{product.id}" + end + end + context'id not found' do + before do + get "/api/v1/product/show/-1" + end + it 'return http status not_found' do + expect(response).to have_http_status(:not_found) + end + end + end + describe 'POST /create' do + let(:category){create(:category)} + let(:brand){create(:brand)} + let(:product_params) do + attributes_for(:product).except(:category, :brand).merge({ category_id: category.id, brand_id: brand.id }) + end + context 'params check ok' do + it 'return http status created' do + p product_params + post "/api/v1/product/create", params:{product:product_params} + expect(response).to have_http_status(:created) + end + end + context 'bad params' do + it 'when param is nil' do + post "/api/v1/product/create", params:{product: nil} + expect(response).to have_http_status(:bad_request) + end + it 'when desc is nil' do + post "/api/v1/product/create", params:{product:{description:nil}} + expect(response).to have_http_status(:bad_request) + end + end + end + describe 'PATCH/update' do + let(:brand) {create(:brand)} + let(:category){create(:category)} + let(:product) do + create(:product) + end + context 'params ok' do + it 'return http status ok' do + patch "/api/v1/product/update/#{product.id}", params:{product:{name: "Lapis"}} + expect(response).to have_http_status(:ok) + end + end + context 'bad params'do + it 'when params is nill' do + patch "/api/v1/product/update/#{product.id}", params:{product: nil} + expect(response).to have_http_status(:bad_request) + end + it 'when params are the same' do + patch "/api/v1/product/update/#{product.id}", params:{product:{name: "Tapete",description: "This is an item",price: 1, stock:1, brand_id:brand.id, category_id:category.id}} + expect(response).to have_http_status(:bad_request) + + end + end + end + describe 'DELETE/delete_id' do + let(:product) do + create(:product) + end + context 'params ok' do + it 'return http status ok' do + delete "/api/v1/product/delete/#{product.id}" + expect(response).to have_http_status(:ok) + end + end + context 'bad params' do + it 'params is nill' do + delete "/api/v1/product/delete/-1" + expect(response).to have_http_status(:bad_request) + end + end + end +end From 125e42247fdf280a3d37e4b9e4db8b310ae41b55 Mon Sep 17 00:00:00 2001 From: danriser Date: Mon, 24 Oct 2022 21:26:19 -0300 Subject: [PATCH 5/5] finished products controllers definitive --- db/seeds.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 32cd0ba..34c0b72 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,12 +10,12 @@ Product.create!(id:2,name: "Mouse rgb 50000 dpi",description: "shiny mouse" ,price: 200,stock: 45,brand_id:1 ,category_id: 1) Product.create!(id:3,name: "Drip shoes",description: "good quality shoes", price: 200,stock: 45,brand_id:1 ,category_id: 1) Product.create!(id:4,name: "T-shirt",description: "cool shirt ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:5,name: "Nontendi",description: "oi ", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:6,name: "Nontendi",description: " oi" ,price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:7,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:8,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:9,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) -Product.create!(id:10,name: "Nontendi",description: " oi", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:5,name: "Sunglasses",description: "emoji ", price: 25,stock: 50,brand_id:1 ,category_id: 1) +Product.create!(id:6,name: "Mario Golf ",description: "Fun game" ,price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:7,name: "Galaxy M 44",description: "Powerfull Phone", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:8,name: "Ipear",description: "Stylish Phone", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:9,name: "Headphone",description: "Enhance your listening experience", price: 200,stock: 45,brand_id:1 ,category_id: 1) +Product.create!(id:10,name: "Pro Controller",description: "Made for pro gamers", price: 200,stock: 45,brand_id:1 ,category_id: 1)