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 72989ad..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) @@ -131,6 +135,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) @@ -190,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) @@ -206,6 +213,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,12 +230,14 @@ GEM PLATFORMS arm64-darwin-21 + x86_64-linux DEPENDENCIES bootstrap (~> 5.2.1) 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 new file mode 100644 index 0000000..d044da0 --- /dev/null +++ b/app/controllers/api/v1/products_controller.rb @@ -0,0 +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/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 index 039a826..8051780 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,61 +1,27 @@ class ProductsController < ApplicationController - before_action :set_product, only: %i[ show edit update destroy ] - - # GET /products - def index + 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 + @product_categories = ProductCategory.all 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 + 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 - # 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 + private - # Only allow a list of trusted parameters through. - def product_params - params.require(:product).permit(:name, :price, :product_category_id) + def new_product_params + params.require(:product).permit( + :name, :price, :product_category_id + ) end -end +end \ No newline at end of file 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/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 c557d62..48665ca 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,46 +11,8 @@
- - -