diff --git a/backend/Gemfile b/backend/Gemfile index 37ba2c4..bf89f9a 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -26,6 +26,9 @@ gem "stimulus-rails" # Build JSON APIs with ease [https://github.com/rails/jbuilder] gem "jbuilder" +# Gem para .env +gem 'dotenv', groups: [:development, :test] + # Use Redis adapter to run Action Cable in production # gem "redis", ">= 4.0.1" diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index 548406c..a338987 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -99,6 +99,7 @@ GEM debug (1.9.2) irb (~> 1.10) reline (>= 0.3.8) + dotenv (3.1.2) drb (2.2.1) erubi (1.12.0) globalid (1.2.1) @@ -255,6 +256,7 @@ DEPENDENCIES bootsnap capybara debug + dotenv importmap-rails jbuilder pg (~> 1.1) diff --git a/backend/app/controllers/cargos_controller.rb b/backend/app/controllers/cargos_controller.rb new file mode 100644 index 0000000..202514f --- /dev/null +++ b/backend/app/controllers/cargos_controller.rb @@ -0,0 +1,70 @@ +class CargosController < ApplicationController + before_action :set_cargo, only: %i[ show edit update destroy ] + + # GET /cargos or /cargos.json + def index + @cargos = Cargo.all + end + + # GET /cargos/1 or /cargos/1.json + def show + end + + # GET /cargos/new + def new + @cargo = Cargo.new + end + + # GET /cargos/1/edit + def edit + end + + # POST /cargos or /cargos.json + def create + @cargo = Cargo.new(cargo_params) + + respond_to do |format| + if @cargo.save + format.html { redirect_to cargo_url(@cargo), notice: "Cargo was successfully created." } + format.json { render :show, status: :created, location: @cargo } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @cargo.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /cargos/1 or /cargos/1.json + def update + respond_to do |format| + if @cargo.update(cargo_params) + format.html { redirect_to cargo_url(@cargo), notice: "Cargo was successfully updated." } + format.json { render :show, status: :ok, location: @cargo } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @cargo.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /cargos/1 or /cargos/1.json + def destroy + @cargo.destroy! + + respond_to do |format| + format.html { redirect_to cargos_url, notice: "Cargo was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_cargo + @cargo = Cargo.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def cargo_params + params.require(:cargo).permit(:cargos) + end +end diff --git a/backend/app/helpers/cargos_helper.rb b/backend/app/helpers/cargos_helper.rb new file mode 100644 index 0000000..20b961d --- /dev/null +++ b/backend/app/helpers/cargos_helper.rb @@ -0,0 +1,2 @@ +module CargosHelper +end diff --git a/backend/app/models/cargo.rb b/backend/app/models/cargo.rb new file mode 100644 index 0000000..6c564c3 --- /dev/null +++ b/backend/app/models/cargo.rb @@ -0,0 +1,2 @@ +class Cargo < ApplicationRecord +end diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 379658a..c87ee1e 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -1,2 +1,3 @@ class User < ApplicationRecord + belongs_to :cargo end diff --git a/backend/app/views/users/_form.html.erb b/backend/app/views/users/_form.html.erb deleted file mode 100644 index 453f44b..0000000 --- a/backend/app/views/users/_form.html.erb +++ /dev/null @@ -1,32 +0,0 @@ -<%= form_with(model: user) do |form| %> - <% if user.errors.any? %> -
-

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

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

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

- -

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

- -

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

- -
diff --git a/backend/app/views/users/_user.json.jbuilder b/backend/app/views/users/_user.json.jbuilder deleted file mode 100644 index 423af30..0000000 --- a/backend/app/views/users/_user.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! user, :id, :matricula, :nome, :email, :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 deleted file mode 100644 index 9dda632..0000000 --- a/backend/app/views/users/edit.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

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 deleted file mode 100644 index fe4dd5c..0000000 --- a/backend/app/views/users/index.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -

<%= 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 deleted file mode 100644 index 98788da..0000000 --- a/backend/app/views/users/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index eedbd83..0000000 --- a/backend/app/views/users/new.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -

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 deleted file mode 100644 index 673fae2..0000000 --- a/backend/app/views/users/show.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -

<%= 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 deleted file mode 100644 index ff40bb9..0000000 --- a/backend/app/views/users/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "users/user", user: @user diff --git a/backend/config/application.rb b/backend/config/application.rb index 64d8c82..cbcf7f1 100644 --- a/backend/config/application.rb +++ b/backend/config/application.rb @@ -1,6 +1,7 @@ require_relative "boot" require "rails/all" +require "dotenv" # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -23,5 +24,8 @@ class Application < Rails::Application # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") + + #dotenv config + Dotenv.load end end diff --git a/backend/config/database.yml b/backend/config/database.yml index ee5d450..faee578 100644 --- a/backend/config/database.yml +++ b/backend/config/database.yml @@ -21,25 +21,24 @@ default: &default development: <<: *default - database: backend_development - + database: <%= ENV['POSTGRES_DB'] %> # The specified database role being used to connect to PostgreSQL. # To create additional roles in PostgreSQL see `$ createuser --help`. # When left blank, PostgreSQL will use the default role. This is # the same name as the operating system user running Rails. - #username: backend + username: <%= ENV['POSTGRES_USER'] %> # The password associated with the PostgreSQL role (username). - #password: + password: <%= ENV['POSTGRES_PASSWORD'] %> # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have # domain sockets, so uncomment these lines. - #host: localhost + host: localhost # The TCP port the server listens on. Defaults to 5432. # If your server runs on a different port number, change accordingly. - #port: 5432 + port: 5432 # Schema search path. The server defaults to $user,public #schema_search_path: myapp,sharedapp,public diff --git a/backend/config/routes.rb b/backend/config/routes.rb index 4067cd3..621d76e 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :cargos resources :users # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html diff --git a/backend/db/migrate/20240518231634_create_cargos.rb b/backend/db/migrate/20240518231634_create_cargos.rb new file mode 100644 index 0000000..e295c9b --- /dev/null +++ b/backend/db/migrate/20240518231634_create_cargos.rb @@ -0,0 +1,9 @@ +class CreateCargos < ActiveRecord::Migration[7.1] + def change + create_table :cargos do |t| + t.string :type + + t.timestamps + end + end +end diff --git a/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb b/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb new file mode 100644 index 0000000..7e3083e --- /dev/null +++ b/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb @@ -0,0 +1,5 @@ +class CreateUserJoinTableCargos < ActiveRecord::Migration[7.1] + def change + add_reference :users, :cargos, foreign_key: true + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb new file mode 100644 index 0000000..1ea5aa0 --- /dev/null +++ b/backend/db/schema.rb @@ -0,0 +1,34 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2024_05_18_234753) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "cargos", force: :cascade do |t| + t.string "type" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "users", force: :cascade do |t| + t.string "matricula" + t.string "nome" + t.string "email" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "cargos_id" + t.index ["cargos_id"], name: "index_users_on_cargos_id" + end + + add_foreign_key "users", "cargos", column: "cargos_id" +end diff --git a/backend/test/controllers/cargos_controller_test.rb b/backend/test/controllers/cargos_controller_test.rb new file mode 100644 index 0000000..e039002 --- /dev/null +++ b/backend/test/controllers/cargos_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class CargosControllerTest < ActionDispatch::IntegrationTest + setup do + @cargo = cargos(:one) + end + + test "should get index" do + get cargos_url + assert_response :success + end + + test "should get new" do + get new_cargo_url + assert_response :success + end + + test "should create cargo" do + assert_difference("Cargo.count") do + post cargos_url, params: { cargo: { cargos: @cargo.cargos } } + end + + assert_redirected_to cargo_url(Cargo.last) + end + + test "should show cargo" do + get cargo_url(@cargo) + assert_response :success + end + + test "should get edit" do + get edit_cargo_url(@cargo) + assert_response :success + end + + test "should update cargo" do + patch cargo_url(@cargo), params: { cargo: { cargos: @cargo.cargos } } + assert_redirected_to cargo_url(@cargo) + end + + test "should destroy cargo" do + assert_difference("Cargo.count", -1) do + delete cargo_url(@cargo) + end + + assert_redirected_to cargos_url + end +end diff --git a/backend/test/fixtures/cargos.yml b/backend/test/fixtures/cargos.yml new file mode 100644 index 0000000..af4cbf6 --- /dev/null +++ b/backend/test/fixtures/cargos.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + cargos: MyString + +two: + cargos: MyString diff --git a/backend/test/models/cargo_test.rb b/backend/test/models/cargo_test.rb new file mode 100644 index 0000000..8cd33d0 --- /dev/null +++ b/backend/test/models/cargo_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CargoTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/backend/test/system/cargos_test.rb b/backend/test/system/cargos_test.rb new file mode 100644 index 0000000..077b70f --- /dev/null +++ b/backend/test/system/cargos_test.rb @@ -0,0 +1,41 @@ +require "application_system_test_case" + +class CargosTest < ApplicationSystemTestCase + setup do + @cargo = cargos(:one) + end + + test "visiting the index" do + visit cargos_url + assert_selector "h1", text: "Cargos" + end + + test "should create cargo" do + visit cargos_url + click_on "New cargo" + + fill_in "Cargos", with: @cargo.cargos + click_on "Create Cargo" + + assert_text "Cargo was successfully created" + click_on "Back" + end + + test "should update Cargo" do + visit cargo_url(@cargo) + click_on "Edit this cargo", match: :first + + fill_in "Cargos", with: @cargo.cargos + click_on "Update Cargo" + + assert_text "Cargo was successfully updated" + click_on "Back" + end + + test "should destroy Cargo" do + visit cargo_url(@cargo) + click_on "Destroy this cargo", match: :first + + assert_text "Cargo was successfully destroyed" + end +end diff --git a/docker-compose.yml b/docker-compose.yml index 715ecc7..3ceefe6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,16 +1,13 @@ -version: "2.1" - services: postgres: # nome do serviço a ser criado image: postgres # nome da imagem existente no docker hub ports: ["5432:5432"] # porta a ser exposta para fora do container networks: [backing-services] # definição de rede virtual para outros possíveis serviços a serem disponibilizados na mesma rede possam acessar - volumes: ["./data"] # definição de local onde estará exposto o volume persistente + volumes: ["/apt/data/EdraPGDB"] # definição de local onde estará exposto o volume persistente environment: # Variáveis de ambiente , no caso o nome do banco o usuário e senha de acesso que é criada dinamicamente quando o container é criado - POSTGRES_DB: postgres - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} networks: # configuação de rede virtual backing-services: driver: bridge