From 05b71d9b1b4b4cfd5f669c44024c1fada497db32 Mon Sep 17 00:00:00 2001 From: minccia Date: Wed, 26 Oct 2022 17:28:40 -0300 Subject: [PATCH 1/5] Removidos arquivos que vieram com o projeto --- Gemfile.lock | 4 ++ .../product_categories_controller.rb | 21 ------- app/controllers/products_controller.rb | 61 ------------------- app/controllers/welcome_controller.rb | 9 --- app/views/layouts/application.html.erb | 43 +------------ .../_product_category.html.erb | 1 - app/views/products/_form.html.erb | 27 -------- app/views/products/_product.html.erb | 12 ---- app/views/products/edit.html.erb | 10 --- app/views/products/index.html.erb | 50 --------------- app/views/products/new.html.erb | 9 --- app/views/products/show.html.erb | 12 ---- app/views/welcome/index.html.erb | 42 ------------- config/routes.rb | 6 -- 14 files changed, 6 insertions(+), 301 deletions(-) delete mode 100644 app/controllers/product_categories_controller.rb delete mode 100644 app/controllers/products_controller.rb delete mode 100644 app/controllers/welcome_controller.rb delete mode 100644 app/views/product_categories/_product_category.html.erb delete mode 100644 app/views/products/_form.html.erb delete mode 100644 app/views/products/_product.html.erb delete mode 100644 app/views/products/edit.html.erb delete mode 100644 app/views/products/index.html.erb delete mode 100644 app/views/products/new.html.erb delete mode 100644 app/views/products/show.html.erb delete mode 100644 app/views/welcome/index.html.erb diff --git a/Gemfile.lock b/Gemfile.lock index 72989ad..d5f5e1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -131,6 +131,8 @@ GEM nio4r (2.5.8) nokogiri (1.13.8-arm64-darwin) racc (~> 1.4) + nokogiri (1.13.8-x86_64-linux) + racc (~> 1.4) orm_adapter (0.5.0) popper_js (2.11.6) public_suffix (5.0.0) @@ -206,6 +208,7 @@ GEM activesupport (>= 5.2) sprockets (>= 3.0.0) sqlite3 (1.5.2-arm64-darwin) + sqlite3 (1.5.2-x86_64-linux) thor (1.2.1) tilt (2.0.11) timeout (0.3.0) @@ -222,6 +225,7 @@ GEM PLATFORMS arm64-darwin-21 + x86_64-linux DEPENDENCIES bootstrap (~> 5.2.1) diff --git a/app/controllers/product_categories_controller.rb b/app/controllers/product_categories_controller.rb deleted file mode 100644 index 180bc41..0000000 --- a/app/controllers/product_categories_controller.rb +++ /dev/null @@ -1,21 +0,0 @@ -class ProductCategoriesController < ApplicationController - def create - ProductCategory.create!(name: params.require(:product_category).permit(:name)) - redirect_to products_path, notice: 'Categoria cadastrada' - end - - def search - nome_a_buscar = params[:busca] - ProductCategory.where("name LIKE :param_busca OR outro_campo LIKE :param_busca OR mais_um_campo LIKE :param_busca ", - { param_busca: "%#{nome_a_buscar}%" } ) - - @transports = find_available_transports - - end - - private - - def find_available_transports() - FreightageFinder.new(transports, order).find() - end -end \ No newline at end of file diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb deleted file mode 100644 index 039a826..0000000 --- a/app/controllers/products_controller.rb +++ /dev/null @@ -1,61 +0,0 @@ -class ProductsController < ApplicationController - before_action :set_product, only: %i[ show edit update destroy ] - - # GET /products - def index - @products = Product.all - @product = Product.new - @product_category = ProductCategory.new - @product_categories = ProductCategory.all - end - - # GET /products/1 - def show - end - - # GET /products/new - def new - @product = Product.new - end - - # GET /products/1/edit - def edit - end - - # POST /products - def create - @product = Product.new(product_params) - - if @product.save - redirect_to @product, notice: "Product was successfully created." - else - render :new, status: :unprocessable_entity - end - end - - # PATCH/PUT /products/1 - def update - if @product.update(product_params) - redirect_to @product, notice: "Product was successfully updated." - else - render :edit, status: :unprocessable_entity - end - end - - # DELETE /products/1 - def destroy - @product.destroy - redirect_to products_url, notice: "Product was successfully destroyed." - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_product - @product = Product.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def product_params - params.require(:product).permit(:name, :price, :product_category_id) - end -end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb deleted file mode 100644 index 73ff2dc..0000000 --- a/app/controllers/welcome_controller.rb +++ /dev/null @@ -1,9 +0,0 @@ -class WelcomeController < ApplicationController - - - def index - - end - - -end \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c557d62..129ffe1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,46 +11,7 @@ - - -
- <%= notice %> - <%= yield %> -
+ <%= notice %> + <%= yield %> diff --git a/app/views/product_categories/_product_category.html.erb b/app/views/product_categories/_product_category.html.erb deleted file mode 100644 index e216369..0000000 --- a/app/views/product_categories/_product_category.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= product_category.name %> \ No newline at end of file diff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb deleted file mode 100644 index a2a1d8e..0000000 --- a/app/views/products/_form.html.erb +++ /dev/null @@ -1,27 +0,0 @@ -<%= form_with(model: product) do |form| %> - <% if product.errors.any? %> -
-

<%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:

- - -
- <% end %> - -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> -
- -
- <%= form.label :price, style: "display: block" %> - <%= form.text_field :price %> -
- -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/products/_product.html.erb b/app/views/products/_product.html.erb deleted file mode 100644 index 19ca353..0000000 --- a/app/views/products/_product.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -
-

- Name: - <%= product.name %> -

- -

- Price: - <%= product.price %> -

- -
diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb deleted file mode 100644 index 90cb860..0000000 --- a/app/views/products/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

Editing product

- -<%= render "form", product: @product %> - -
- -
- <%= link_to "Show this product", @product %> | - <%= link_to "Back to products", products_path %> -
diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb deleted file mode 100644 index a84a004..0000000 --- a/app/views/products/index.html.erb +++ /dev/null @@ -1,50 +0,0 @@ -

<%= notice %>

- -

Products

-
- <% @products.each do |product| %> - <%= render product %> -

- <%= link_to "Show this product", product %> -

- <% end %> -
-
-

Nova Categoria de Produto

-<%= form_with(model: @product_category) do |f| %> - <%= f.label :name %> - <%= f.text_field :name %> - <%= f.submit %> -<% end %> -
- - - -

Novo Produto

-<%= form_with(model: @product) do |form| %> -
- <%= form.label :name, style: "display: block" %> - <%= form.text_field :name %> -
- -
- <%= form.label :price, style: "display: block" %> - <%= form.text_field :price %> -
- -
- <%= form.label :condition_new, 'Produto Novo' %> - <%= form.radio_button :condition, :new %> - - <%= form.label :condition_used, 'Produto Usado' %> - <%= form.radio_button :condition, :used %> -
- -
- <%= form.submit %> -
-<% end %> - - -choose 'Produto Usado' - diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb deleted file mode 100644 index d438e41..0000000 --- a/app/views/products/new.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -

New product

- -<%= render "form", product: @product %> - -
- -
- <%= link_to "Back to products", products_path %> -
diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb deleted file mode 100644 index 7e30dc8..0000000 --- a/app/views/products/show.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -

<%= notice %>

- -<%= render @product %> - -<%= render @product.product_category %> - -
- <%= link_to "Edit this product", edit_product_path(@product) %> | - <%= link_to "Back to products", products_path %> - - <%= button_to "Destroy this product", @product, method: :delete %> -
diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb deleted file mode 100644 index 65777a5..0000000 --- a/app/views/welcome/index.html.erb +++ /dev/null @@ -1,42 +0,0 @@ -

<%= translate('.title') %>

- -
-
- <%= image_tag 'delivery.png', class: 'img-fluid' %> -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#FirstLastHandle
1MarkOtto@mdo
2JacobThornton@fat
3Larry the Bird@twitter
-
-
- - -<%= link_to 'Início', root_path, class: 'btn btn-primary btn-lg' %> diff --git a/config/routes.rb b/config/routes.rb index e23c4d5..1daf9a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,2 @@ Rails.application.routes.draw do - resources :products - resources :product_categories, only: [:create, :show] do - resources :products, only: [:create] - end - devise_for :users - root 'welcome#index' end From a5c3e1959cf873b39e52914ab403071b63693613 Mon Sep 17 00:00:00 2001 From: minccia Date: Wed, 26 Oct 2022 18:26:23 -0300 Subject: [PATCH 2/5] User create new products Co-authored-by: brunabarbozavieira --- app/controllers/products_controller.rb | 27 +++++++++++++++++ app/models/product_category.rb | 1 + app/views/layouts/application.html.erb | 1 + app/views/products/index.html.erb | 14 +++++++++ app/views/products/new.html.erb | 11 +++++++ config/routes.rb | 3 ++ db/seeds.rb | 18 ----------- .../products/user_create_new_product_spec.rb | 30 +++++++++++++++++++ spec/system/user_view_home_page_spec.rb | 10 +++++++ 9 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 app/controllers/products_controller.rb create mode 100644 app/views/products/index.html.erb create mode 100644 app/views/products/new.html.erb create mode 100644 spec/system/products/user_create_new_product_spec.rb create mode 100644 spec/system/user_view_home_page_spec.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 0000000..8051780 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,27 @@ +class ProductsController < ApplicationController + def index + @products = Product.all + end + + def new + @product = Product.new + @product_categories = ProductCategory.all + end + + def create + @product = Product.new new_product_params + if @product.save + return redirect_to root_url, notice: 'Produto cadastrado com sucesso.' + end + flash.notice = 'Produto não cadastrado' + render :new + end + + private + + def new_product_params + params.require(:product).permit( + :name, :price, :product_category_id + ) + end +end \ No newline at end of file diff --git a/app/models/product_category.rb b/app/models/product_category.rb index 282738c..d95f936 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -1,3 +1,4 @@ class ProductCategory < ApplicationRecord validates :name, presence: true + has_many :products end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 129ffe1..48665ca 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,7 @@ +

Estoque de Produto

<%= notice %> <%= yield %> diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 0000000..86843aa --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,14 @@ +
+
+ <%= link_to 'Cadastrar Novo Produto', new_product_path %> +
+
+ <% if @products.any? %> + <% @products.each do |product| %> + Nome: <%= product.name %> + Preço: <%= product.price %> + Categoria de Produto: <%= product.product_category.name %> + <% end %> + <% end %> +
+
\ No newline at end of file diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000..953d92a --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,11 @@ +

Cadastrando Novo Produto

+ +<%= form_with model: @product do |f|%> + <%= f.label :name, 'Nome' %> + <%= f.text_field :name %> + <%= f.label :price, 'Preço' %> + <%= f.number_field :price %> + <%= f.label :product_category_id, 'Categoria de Produto' %> + <%= f.collection_select :product_category_id, @product_categories, :id, :name %> + <%= f.submit 'Salvar' %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1daf9a4..d2a0a2c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,2 +1,5 @@ Rails.application.routes.draw do + root 'products#index' + + resources :products, only: %i[new create] end diff --git a/db/seeds.rb b/db/seeds.rb index fbaf120..e69de29 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,18 +0,0 @@ -# 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 bin/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) - -User.create(email: user@email.com, password: ) -Company.create(name: , cnpj: ) -Product.create(name: , price: , company: ) - - - - - - -User.create(email: user@email.com, password: ) \ No newline at end of file diff --git a/spec/system/products/user_create_new_product_spec.rb b/spec/system/products/user_create_new_product_spec.rb new file mode 100644 index 0000000..37aefa3 --- /dev/null +++ b/spec/system/products/user_create_new_product_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +describe 'Usuário cria um novo produto' do + it 'a partir de um formulário' do + + visit root_path + click_on 'Cadastrar Novo Produto' + + expect(page).to have_content 'Cadastrando Novo Produto' + expect(page).to have_field 'Nome' + expect(page).to have_field 'Preço' + expect(page).to have_field 'Categoria de Produto' + expect(page).to have_button 'Salvar' + end + + it 'com sucesso' do + product_category = ProductCategory.create!(name: 'Eletrônicos') + + visit new_product_path + fill_in 'Nome', with: 'TV' + fill_in 'Preço', with: '15000' + select 'Eletrônicos', from: 'Categoria de Produto' + click_on 'Salvar' + + expect(page).to have_content 'Produto cadastrado com sucesso.' + expect(page).to have_content 'Nome: TV' + expect(page).to have_content 'Preço: 15000' + expect(page).to have_content 'Categoria de Produto: Eletrônicos' + end +end \ No newline at end of file diff --git a/spec/system/user_view_home_page_spec.rb b/spec/system/user_view_home_page_spec.rb new file mode 100644 index 0000000..8ba94fb --- /dev/null +++ b/spec/system/user_view_home_page_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +describe 'Usuário vê a home page' do + it 'com sucesso' do + visit root_path + + expect(page).to have_content 'Estoque de Produto' + expect(page).to have_link 'Cadastrar Novo Produto' + end +end \ No newline at end of file From e3935440215fd5ad1deeab6b80931843d48f12c5 Mon Sep 17 00:00:00 2001 From: minccia Date: Fri, 28 Oct 2022 18:37:09 -0300 Subject: [PATCH 3/5] User view products through application API --- app/controllers/api/v1/products_controller.rb | 6 ++++ config/routes.rb | 6 ++++ .../products/user_view_products_api_spec.rb | 29 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 app/controllers/api/v1/products_controller.rb create mode 100644 spec/requests/api/products/user_view_products_api_spec.rb diff --git a/app/controllers/api/v1/products_controller.rb b/app/controllers/api/v1/products_controller.rb new file mode 100644 index 0000000..65be03d --- /dev/null +++ b/app/controllers/api/v1/products_controller.rb @@ -0,0 +1,6 @@ +class Api::V1::ProductsController < ActionController::API + def index + products = Product.all + render status: 200, json: products.as_json + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d2a0a2c..94b3775 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,10 @@ root 'products#index' resources :products, only: %i[new create] + + namespace :api do + namespace :v1 do + resources :products, only: [:index] + end + end end diff --git a/spec/requests/api/products/user_view_products_api_spec.rb b/spec/requests/api/products/user_view_products_api_spec.rb new file mode 100644 index 0000000..20fd369 --- /dev/null +++ b/spec/requests/api/products/user_view_products_api_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +describe 'Usuário vê produtos a partir da API' do + context 'GET /api/v1/products/' do + it 'with success' do + + categoria = ProductCategory.create!( + name: 'Sobremesas' + + ) + Product.create!( + name: 'Bombom de chocolate', + price: 5, + product_category: categoria + ) + + get '/api/v1/products/' + + expect(response).to have_http_status 200 + expect(response.content_type).to include 'application/json' + + json_response = JSON.parse(response.body) + + expect(json_response.first["name"]).to eq 'Bombom de chocolate' + expect(json_response.first["price"]).to eq "5.0" + expect(json_response.first["product_category_id"]).to eq 1 + end + end +end \ No newline at end of file From 8a4585c2d32f166a731a15f5efe967424ecf78ab Mon Sep 17 00:00:00 2001 From: minccia Date: Fri, 28 Oct 2022 19:00:08 -0300 Subject: [PATCH 4/5] Added more tests for code coverage --- Gemfile | 1 + Gemfile.lock | 6 ++++ app/controllers/api/v1/products_controller.rb | 8 +++++ .../products/user_view_products_api_spec.rb | 30 ++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 2831a34..fdebc48 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem "sprockets-rails" gem "sqlite3", "~> 1.4" gem "puma", "~> 5.0" gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +gem "faraday" group :development, :test do gem "capybara" diff --git a/Gemfile.lock b/Gemfile.lock index d5f5e1b..5068c48 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -99,6 +99,10 @@ GEM diff-lcs (1.5.0) erubi (1.11.0) execjs (2.8.1) + faraday (2.6.0) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.1) ffi (1.15.5) globalid (1.0.0) activesupport (>= 5.0) @@ -192,6 +196,7 @@ GEM rspec-mocks (~> 3.11) rspec-support (~> 3.11) rspec-support (3.11.1) + ruby2_keywords (0.0.5) sassc (2.4.0) ffi (~> 1.9) sassc-rails (2.1.2) @@ -232,6 +237,7 @@ DEPENDENCIES capybara debug devise + faraday importmap-rails puma (~> 5.0) rails (~> 7.0.4) diff --git a/app/controllers/api/v1/products_controller.rb b/app/controllers/api/v1/products_controller.rb index 65be03d..d044da0 100644 --- a/app/controllers/api/v1/products_controller.rb +++ b/app/controllers/api/v1/products_controller.rb @@ -1,6 +1,14 @@ class Api::V1::ProductsController < ActionController::API + rescue_from ActiveRecord::QueryCanceled, with: :return_500 + def index products = Product.all render status: 200, json: products.as_json end + + private + + def return_500 + render status: 500 + end end \ No newline at end of file diff --git a/spec/requests/api/products/user_view_products_api_spec.rb b/spec/requests/api/products/user_view_products_api_spec.rb index 20fd369..c377dee 100644 --- a/spec/requests/api/products/user_view_products_api_spec.rb +++ b/spec/requests/api/products/user_view_products_api_spec.rb @@ -2,7 +2,7 @@ describe 'Usuário vê produtos a partir da API' do context 'GET /api/v1/products/' do - it 'with success' do + it 'com sucesso' do categoria = ProductCategory.create!( name: 'Sobremesas' @@ -25,5 +25,33 @@ expect(json_response.first["price"]).to eq "5.0" expect(json_response.first["product_category_id"]).to eq 1 end + + it 'e não há produtos cadastrados' do + #arrange + #act + get '/api/v1/products/' + #assert + expect(response).to have_http_status 200 + expect(response.content_type).to include 'application/json' + expect(response.body).to eq "[]" + end + + it 'e ocorre um erro interno' do + categoria = ProductCategory.create!( + name: 'Sobremesas' + + ) + Product.create!( + name: 'Bombom de chocolate', + price: 5, + product_category: categoria + ) + + allow(Product).to receive(:all).and_raise(ActiveRecord::QueryCanceled) + + get '/api/v1/products/' + + expect(response).to have_http_status 500 + end end end \ No newline at end of file From be99325030110ce3bb771dcd873a499f13ad4ab8 Mon Sep 17 00:00:00 2001 From: minccia Date: Fri, 28 Oct 2022 19:02:49 -0300 Subject: [PATCH 5/5] Added Ana Borba as coauthor Co-authored-by: ana-borowsky --- spec/requests/api/products/user_view_products_api_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/requests/api/products/user_view_products_api_spec.rb b/spec/requests/api/products/user_view_products_api_spec.rb index c377dee..b289f18 100644 --- a/spec/requests/api/products/user_view_products_api_spec.rb +++ b/spec/requests/api/products/user_view_products_api_spec.rb @@ -48,9 +48,10 @@ ) allow(Product).to receive(:all).and_raise(ActiveRecord::QueryCanceled) - + get '/api/v1/products/' + expect(response).to have_http_status 500 end end