From 26b11b97ecbadea1e9b56d43a5a5e9bcc3a0795b Mon Sep 17 00:00:00 2001 From: Mauriciofearauj Date: Mon, 20 May 2024 23:02:57 -0300 Subject: [PATCH] =?UTF-8?q?inser=C3=A7=C3=A3o=20do=20campo=20senha=20no=20?= =?UTF-8?q?banco=20de=20dados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/controllers/cargos_controller.rb | 2 +- backend/app/controllers/users_controller.rb | 4 +- backend/app/models/cargo.rb | 1 - backend/app/views/cargos/_cargo.html.erb | 7 ++++ backend/app/views/cargos/_cargo.json.jbuilder | 2 + backend/app/views/cargos/_form.html.erb | 22 ++++++++++ backend/app/views/cargos/edit.html.erb | 10 +++++ backend/app/views/cargos/index.html.erb | 14 +++++++ backend/app/views/cargos/index.json.jbuilder | 1 + backend/app/views/cargos/new.html.erb | 9 ++++ backend/app/views/cargos/show.html.erb | 10 +++++ backend/app/views/cargos/show.json.jbuilder | 1 + backend/app/views/users/_form.html.erb | 42 +++++++++++++++++++ backend/app/views/users/_user.html.erb | 27 ++++++++++++ backend/app/views/users/_user.json.jbuilder | 2 + backend/app/views/users/edit.html.erb | 10 +++++ backend/app/views/users/index.html.erb | 14 +++++++ backend/app/views/users/index.json.jbuilder | 1 + backend/app/views/users/new.html.erb | 9 ++++ backend/app/views/users/show.html.erb | 10 +++++ backend/app/views/users/show.json.jbuilder | 1 + ...gos.rb => 20240521013906_create_cargos.rb} | 0 ...sers.rb => 20240521013908_create_users.rb} | 1 + backend/db/schema.rb | 3 +- .../controllers/cargos_controller_test.rb | 4 +- .../test/controllers/users_controller_test.rb | 4 +- backend/test/fixtures/users.yml | 2 + backend/test/system/cargos_test.rb | 4 +- backend/test/system/users_test.rb | 4 ++ 29 files changed, 210 insertions(+), 11 deletions(-) create mode 100644 backend/app/views/cargos/_cargo.html.erb create mode 100644 backend/app/views/cargos/_cargo.json.jbuilder create mode 100644 backend/app/views/cargos/_form.html.erb create mode 100644 backend/app/views/cargos/edit.html.erb create mode 100644 backend/app/views/cargos/index.html.erb create mode 100644 backend/app/views/cargos/index.json.jbuilder create mode 100644 backend/app/views/cargos/new.html.erb create mode 100644 backend/app/views/cargos/show.html.erb create mode 100644 backend/app/views/cargos/show.json.jbuilder create mode 100644 backend/app/views/users/_form.html.erb create mode 100644 backend/app/views/users/_user.html.erb create mode 100644 backend/app/views/users/_user.json.jbuilder create mode 100644 backend/app/views/users/edit.html.erb create mode 100644 backend/app/views/users/index.html.erb create mode 100644 backend/app/views/users/index.json.jbuilder create mode 100644 backend/app/views/users/new.html.erb create mode 100644 backend/app/views/users/show.html.erb create mode 100644 backend/app/views/users/show.json.jbuilder rename backend/db/migrate/{20240520053452_create_cargos.rb => 20240521013906_create_cargos.rb} (100%) rename backend/db/migrate/{20240520053455_create_users.rb => 20240521013908_create_users.rb} (92%) diff --git a/backend/app/controllers/cargos_controller.rb b/backend/app/controllers/cargos_controller.rb index 202514f..3466816 100644 --- a/backend/app/controllers/cargos_controller.rb +++ b/backend/app/controllers/cargos_controller.rb @@ -65,6 +65,6 @@ def set_cargo # Only allow a list of trusted parameters through. def cargo_params - params.require(:cargo).permit(:cargos) + params.require(:cargo).permit(:nome) end end diff --git a/backend/app/controllers/users_controller.rb b/backend/app/controllers/users_controller.rb index ed44168..dc2059b 100644 --- a/backend/app/controllers/users_controller.rb +++ b/backend/app/controllers/users_controller.rb @@ -1,3 +1,4 @@ +require "bcrypt" class UsersController < ApplicationController before_action :set_user, only: %i[ show edit update destroy ] @@ -5,7 +6,6 @@ class UsersController < ApplicationController def index @users = User.all end - # GET /users/1 or /users/1.json def show end @@ -65,6 +65,6 @@ def set_user # Only allow a list of trusted parameters through. def user_params - params.require(:user).permit(:matricula, :nome, :email) + params.require(:user).permit(:nome, :matricula, :email, :senha, :cargo_id) end end diff --git a/backend/app/models/cargo.rb b/backend/app/models/cargo.rb index a78b63e..6c564c3 100644 --- a/backend/app/models/cargo.rb +++ b/backend/app/models/cargo.rb @@ -1,3 +1,2 @@ class Cargo < ApplicationRecord - has_many :users end diff --git a/backend/app/views/cargos/_cargo.html.erb b/backend/app/views/cargos/_cargo.html.erb new file mode 100644 index 0000000..c42bed7 --- /dev/null +++ b/backend/app/views/cargos/_cargo.html.erb @@ -0,0 +1,7 @@ +
+

+ Nome: + <%= cargo.nome %> +

+ +
diff --git a/backend/app/views/cargos/_cargo.json.jbuilder b/backend/app/views/cargos/_cargo.json.jbuilder new file mode 100644 index 0000000..f0216dc --- /dev/null +++ b/backend/app/views/cargos/_cargo.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! cargo, :id, :nome, :created_at, :updated_at +json.url cargo_url(cargo, format: :json) diff --git a/backend/app/views/cargos/_form.html.erb b/backend/app/views/cargos/_form.html.erb new file mode 100644 index 0000000..f61efb1 --- /dev/null +++ b/backend/app/views/cargos/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: cargo) do |form| %> + <% if cargo.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.label :nome, style: "display: block" %> + <%= form.text_field :nome %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/backend/app/views/cargos/edit.html.erb b/backend/app/views/cargos/edit.html.erb new file mode 100644 index 0000000..4486858 --- /dev/null +++ b/backend/app/views/cargos/edit.html.erb @@ -0,0 +1,10 @@ +

Editing cargo

+ +<%= render "form", cargo: @cargo %> + +
+ +
+ <%= link_to "Show this cargo", @cargo %> | + <%= link_to "Back to cargos", cargos_path %> +
diff --git a/backend/app/views/cargos/index.html.erb b/backend/app/views/cargos/index.html.erb new file mode 100644 index 0000000..fd3efed --- /dev/null +++ b/backend/app/views/cargos/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Cargos

+ +
+ <% @cargos.each do |cargo| %> + <%= render cargo %> +

+ <%= link_to "Show this cargo", cargo %> +

+ <% end %> +
+ +<%= link_to "New cargo", new_cargo_path %> diff --git a/backend/app/views/cargos/index.json.jbuilder b/backend/app/views/cargos/index.json.jbuilder new file mode 100644 index 0000000..812da20 --- /dev/null +++ b/backend/app/views/cargos/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @cargos, partial: "cargos/cargo", as: :cargo diff --git a/backend/app/views/cargos/new.html.erb b/backend/app/views/cargos/new.html.erb new file mode 100644 index 0000000..43d32f2 --- /dev/null +++ b/backend/app/views/cargos/new.html.erb @@ -0,0 +1,9 @@ +

New cargo

+ +<%= render "form", cargo: @cargo %> + +
+ +
+ <%= link_to "Back to cargos", cargos_path %> +
diff --git a/backend/app/views/cargos/show.html.erb b/backend/app/views/cargos/show.html.erb new file mode 100644 index 0000000..6568fbd --- /dev/null +++ b/backend/app/views/cargos/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @cargo %> + +
+ <%= link_to "Edit this cargo", edit_cargo_path(@cargo) %> | + <%= link_to "Back to cargos", cargos_path %> + + <%= button_to "Destroy this cargo", @cargo, method: :delete %> +
diff --git a/backend/app/views/cargos/show.json.jbuilder b/backend/app/views/cargos/show.json.jbuilder new file mode 100644 index 0000000..fe05d38 --- /dev/null +++ b/backend/app/views/cargos/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "cargos/cargo", cargo: @cargo diff --git a/backend/app/views/users/_form.html.erb b/backend/app/views/users/_form.html.erb new file mode 100644 index 0000000..50fd2c8 --- /dev/null +++ b/backend/app/views/users/_form.html.erb @@ -0,0 +1,42 @@ +<%= form_with(model: user) do |form| %> + <% if user.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.label :nome, style: "display: block" %> + <%= form.text_field :nome %> +
+ +
+ <%= form.label :matricula, style: "display: block" %> + <%= form.text_field :matricula %> +
+ +
+ <%= form.label :email, style: "display: block" %> + <%= form.text_field :email %> +
+ +
+ <%= form.label :senha, style: "display: block" %> + <%= form.text_field :senha %> +
+ +
+ <%= form.label :cargo_id, style: "display: block" %> + <%= form.text_field :cargo_id %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/backend/app/views/users/_user.html.erb b/backend/app/views/users/_user.html.erb new file mode 100644 index 0000000..5997ebc --- /dev/null +++ b/backend/app/views/users/_user.html.erb @@ -0,0 +1,27 @@ +
+

+ Nome: + <%= user.nome %> +

+ +

+ Matricula: + <%= user.matricula %> +

+ +

+ Email: + <%= user.email %> +

+ +

+ Senha: + <%= user.senha %> +

+ +

+ Cargo: + <%= user.cargo_id %> +

+ +
diff --git a/backend/app/views/users/_user.json.jbuilder b/backend/app/views/users/_user.json.jbuilder new file mode 100644 index 0000000..f38bf53 --- /dev/null +++ b/backend/app/views/users/_user.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! user, :id, :nome, :matricula, :email, :senha, :cargo_id, :created_at, :updated_at +json.url user_url(user, format: :json) diff --git a/backend/app/views/users/edit.html.erb b/backend/app/views/users/edit.html.erb new file mode 100644 index 0000000..9dda632 --- /dev/null +++ b/backend/app/views/users/edit.html.erb @@ -0,0 +1,10 @@ +

Editing user

+ +<%= render "form", user: @user %> + +
+ +
+ <%= link_to "Show this user", @user %> | + <%= link_to "Back to users", users_path %> +
diff --git a/backend/app/views/users/index.html.erb b/backend/app/views/users/index.html.erb new file mode 100644 index 0000000..fe4dd5c --- /dev/null +++ b/backend/app/views/users/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Users

+ +
+ <% @users.each do |user| %> + <%= render user %> +

+ <%= link_to "Show this user", user %> +

+ <% end %> +
+ +<%= link_to "New user", new_user_path %> diff --git a/backend/app/views/users/index.json.jbuilder b/backend/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..98788da --- /dev/null +++ b/backend/app/views/users/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @users, partial: "users/user", as: :user diff --git a/backend/app/views/users/new.html.erb b/backend/app/views/users/new.html.erb new file mode 100644 index 0000000..eedbd83 --- /dev/null +++ b/backend/app/views/users/new.html.erb @@ -0,0 +1,9 @@ +

New user

+ +<%= render "form", user: @user %> + +
+ +
+ <%= link_to "Back to users", users_path %> +
diff --git a/backend/app/views/users/show.html.erb b/backend/app/views/users/show.html.erb new file mode 100644 index 0000000..673fae2 --- /dev/null +++ b/backend/app/views/users/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @user %> + +
+ <%= link_to "Edit this user", edit_user_path(@user) %> | + <%= link_to "Back to users", users_path %> + + <%= button_to "Destroy this user", @user, method: :delete %> +
diff --git a/backend/app/views/users/show.json.jbuilder b/backend/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..ff40bb9 --- /dev/null +++ b/backend/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "users/user", user: @user diff --git a/backend/db/migrate/20240520053452_create_cargos.rb b/backend/db/migrate/20240521013906_create_cargos.rb similarity index 100% rename from backend/db/migrate/20240520053452_create_cargos.rb rename to backend/db/migrate/20240521013906_create_cargos.rb diff --git a/backend/db/migrate/20240520053455_create_users.rb b/backend/db/migrate/20240521013908_create_users.rb similarity index 92% rename from backend/db/migrate/20240520053455_create_users.rb rename to backend/db/migrate/20240521013908_create_users.rb index 29c5716..1de544d 100644 --- a/backend/db/migrate/20240520053455_create_users.rb +++ b/backend/db/migrate/20240521013908_create_users.rb @@ -4,6 +4,7 @@ def change t.string :nome t.string :matricula t.string :email + t.string :senha t.references :cargo, null: false, foreign_key: true t.timestamps diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 2a718ed..fd261d0 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_05_20_053455) do +ActiveRecord::Schema[7.1].define(version: 2024_05_21_013908) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -24,6 +24,7 @@ t.string "nome" t.string "matricula" t.string "email" + t.string "senha" t.bigint "cargo_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false diff --git a/backend/test/controllers/cargos_controller_test.rb b/backend/test/controllers/cargos_controller_test.rb index e039002..70f2ea2 100644 --- a/backend/test/controllers/cargos_controller_test.rb +++ b/backend/test/controllers/cargos_controller_test.rb @@ -17,7 +17,7 @@ class CargosControllerTest < ActionDispatch::IntegrationTest test "should create cargo" do assert_difference("Cargo.count") do - post cargos_url, params: { cargo: { cargos: @cargo.cargos } } + post cargos_url, params: { cargo: { nome: @cargo.nome } } end assert_redirected_to cargo_url(Cargo.last) @@ -34,7 +34,7 @@ class CargosControllerTest < ActionDispatch::IntegrationTest end test "should update cargo" do - patch cargo_url(@cargo), params: { cargo: { cargos: @cargo.cargos } } + patch cargo_url(@cargo), params: { cargo: { nome: @cargo.nome } } assert_redirected_to cargo_url(@cargo) end diff --git a/backend/test/controllers/users_controller_test.rb b/backend/test/controllers/users_controller_test.rb index 95643f4..399ad00 100644 --- a/backend/test/controllers/users_controller_test.rb +++ b/backend/test/controllers/users_controller_test.rb @@ -17,7 +17,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest test "should create user" do assert_difference("User.count") do - post users_url, params: { user: { email: @user.email, matricula: @user.matricula, nome: @user.nome } } + post users_url, params: { user: { cargo_id: @user.cargo_id, email: @user.email, matricula: @user.matricula, nome: @user.nome, senha: @user.senha } } end assert_redirected_to user_url(User.last) @@ -34,7 +34,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end test "should update user" do - patch user_url(@user), params: { user: { email: @user.email, matricula: @user.matricula, nome: @user.nome } } + patch user_url(@user), params: { user: { cargo_id: @user.cargo_id, email: @user.email, matricula: @user.matricula, nome: @user.nome, senha: @user.senha } } assert_redirected_to user_url(@user) end diff --git a/backend/test/fixtures/users.yml b/backend/test/fixtures/users.yml index b9db350..7cf94fb 100644 --- a/backend/test/fixtures/users.yml +++ b/backend/test/fixtures/users.yml @@ -4,10 +4,12 @@ one: nome: MyString matricula: MyString email: MyString + senha: MyString cargo: one two: nome: MyString matricula: MyString email: MyString + senha: MyString cargo: two diff --git a/backend/test/system/cargos_test.rb b/backend/test/system/cargos_test.rb index 077b70f..3bd9437 100644 --- a/backend/test/system/cargos_test.rb +++ b/backend/test/system/cargos_test.rb @@ -14,7 +14,7 @@ class CargosTest < ApplicationSystemTestCase visit cargos_url click_on "New cargo" - fill_in "Cargos", with: @cargo.cargos + fill_in "Nome", with: @cargo.nome click_on "Create Cargo" assert_text "Cargo was successfully created" @@ -25,7 +25,7 @@ class CargosTest < ApplicationSystemTestCase visit cargo_url(@cargo) click_on "Edit this cargo", match: :first - fill_in "Cargos", with: @cargo.cargos + fill_in "Nome", with: @cargo.nome click_on "Update Cargo" assert_text "Cargo was successfully updated" diff --git a/backend/test/system/users_test.rb b/backend/test/system/users_test.rb index 6d29c18..5de1214 100644 --- a/backend/test/system/users_test.rb +++ b/backend/test/system/users_test.rb @@ -14,9 +14,11 @@ class UsersTest < ApplicationSystemTestCase visit users_url click_on "New user" + fill_in "Cargo", with: @user.cargo_id fill_in "Email", with: @user.email fill_in "Matricula", with: @user.matricula fill_in "Nome", with: @user.nome + fill_in "Senha", with: @user.senha click_on "Create User" assert_text "User was successfully created" @@ -27,9 +29,11 @@ class UsersTest < ApplicationSystemTestCase visit user_url(@user) click_on "Edit this user", match: :first + fill_in "Cargo", with: @user.cargo_id fill_in "Email", with: @user.email fill_in "Matricula", with: @user.matricula fill_in "Nome", with: @user.nome + fill_in "Senha", with: @user.senha click_on "Update User" assert_text "User was successfully updated"