Skip to content
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 0 additions & 21 deletions app/controllers/product_categories_controller.rb

This file was deleted.

62 changes: 14 additions & 48 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions app/controllers/welcome_controller.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/models/product_category.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class ProductCategory < ApplicationRecord
validates :name, presence: true
has_many :products
end
44 changes: 3 additions & 41 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,8 @@
</head>

<body>
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<%= form_with(url: root_path, class: 'd-flex') do |f| %>
<%= f.text_field :busca, class: 'form-control me-2' %>
<%= f.submit 'Buscar', class: 'btn btn-outline-success' %>
<% end %>
</div>
</div>
</nav>

<div class="container">
<%= notice %>
<%= yield %>
</div>
<h1> Estoque de Produto </h1>
<%= notice %>
<%= yield %>
</body>
</html>
1 change: 0 additions & 1 deletion app/views/product_categories/_product_category.html.erb

This file was deleted.

27 changes: 0 additions & 27 deletions app/views/products/_form.html.erb

This file was deleted.

12 changes: 0 additions & 12 deletions app/views/products/_product.html.erb

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/products/edit.html.erb

This file was deleted.

64 changes: 14 additions & 50 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Products</h1>
<div id="products">
<% @products.each do |product| %>
<%= render product %>
<p>
<%= link_to "Show this product", product %>
</p>
<% end %>
</div>
<hr/>
<h2>Nova Categoria de Produto</h2>
<%= form_with(model: @product_category) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
<hr/>



<h2>Novo Produto</h2>
<%= form_with(model: @product) do |form| %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :price, style: "display: block" %>
<%= form.text_field :price %>
</div>

<div>
<%= form.label :condition_new, 'Produto Novo' %>
<%= form.radio_button :condition, :new %>

<%= form.label :condition_used, 'Produto Usado' %>
<%= form.radio_button :condition, :used %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>


choose 'Produto Usado'

<article>
<header>
<%= link_to 'Cadastrar Novo Produto', new_product_path %>
</header>
<main>
<% if @products.any? %>
<% @products.each do |product| %>
Nome: <%= product.name %>
Preço: <%= product.price %>
Categoria de Produto: <%= product.product_category.name %>
<% end %>
<% end %>
</main>
</article>
18 changes: 10 additions & 8 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<h1>New product</h1>
<h1>Cadastrando Novo Produto</h1>

<%= render "form", product: @product %>

<br>

<div>
<%= link_to "Back to products", products_path %>
</div>
<%= 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 %>
12 changes: 0 additions & 12 deletions app/views/products/show.html.erb

This file was deleted.

Loading