From d92d1410ecaa8e5a944f569d967358f4782d45e3 Mon Sep 17 00:00:00 2001 From: AlansidneyJunior Date: Thu, 17 Oct 2024 00:32:42 -0300 Subject: [PATCH 1/4] Criando tabela pai Client usando STI --- Gemfile | 19 +----- app/models/client.rb | 2 + config/database.yml | 74 ++------------------- db/migrate/20241017031706_create_clients.rb | 21 ++++++ db/schema.rb | 20 +++++- test/fixtures/clients.yml | 31 +++++++++ test/models/client_test.rb | 7 ++ 7 files changed, 86 insertions(+), 88 deletions(-) create mode 100644 app/models/client.rb create mode 100644 db/migrate/20241017031706_create_clients.rb create mode 100644 test/fixtures/clients.yml create mode 100644 test/models/client_test.rb diff --git a/Gemfile b/Gemfile index e3178da..0cd5a55 100644 --- a/Gemfile +++ b/Gemfile @@ -12,17 +12,6 @@ gem "pg", "~> 1.1" # Use the Puma web server [https://github.com/puma/puma] gem "puma", "~> 5.0" -# Build JSON APIs with ease [https://github.com/rails/jbuilder] -# gem "jbuilder" - -# Use Redis adapter to run Action Cable in production -# gem "redis", "~> 4.0" - -# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] -# gem "kredis" - -# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] -# gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] @@ -30,11 +19,6 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] # Reduces boot times through caching; required in config/boot.rb gem "bootsnap", require: false -# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] -# gem "image_processing", "~> 1.2" - -# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible -# gem "rack-cors" group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem @@ -42,7 +26,6 @@ group :development, :test do end group :development do - # Speed up commands on slow machines / big apps [https://github.com/rails/spring] - # gem "spring" + end diff --git a/app/models/client.rb b/app/models/client.rb new file mode 100644 index 0000000..45b2363 --- /dev/null +++ b/app/models/client.rb @@ -0,0 +1,2 @@ +class Client < ApplicationRecord +end diff --git a/config/database.yml b/config/database.yml index 88a63c1..9304a4e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,86 +1,22 @@ -# PostgreSQL. Versions 9.3 and up are supported. -# -# Install the pg driver: -# gem install pg -# On macOS with Homebrew: -# gem install pg -- --with-pg-config=/usr/local/bin/pg_config -# On macOS with MacPorts: -# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config -# On Windows: -# gem install pg -# Choose the win32 build. -# Install PostgreSQL and put its /bin directory on your path. -# -# Configure Using Gemfile -# gem "pg" -# default: &default adapter: postgresql encoding: unicode - # For details on connection pooling, see Rails configuration guide - # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + username: postgres + password: 'postgres' + host: localhost + port: 5432 development: <<: *default database: vmaxnet_api_development - # The specified database role being used to connect to postgres. - # To create additional roles in postgres see `$ createuser --help`. - # When left blank, postgres will use the default role. This is - # the same name as the operating system user running Rails. - #username: vmaxnet_api - - # The password associated with the postgres role (username). - #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 - - # The TCP port the server listens on. Defaults to 5432. - # If your server runs on a different port number, change accordingly. - #port: 5432 - - # Schema search path. The server defaults to $user,public - #schema_search_path: myapp,sharedapp,public - - # Minimum log levels, in increasing order: - # debug5, debug4, debug3, debug2, debug1, - # log, notice, warning, error, fatal, and panic - # Defaults to warning. - #min_messages: notice - -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. test: <<: *default database: vmaxnet_api_test -# As with config/credentials.yml, you never want to store sensitive information, -# like your database password, in your source code. If your source code is -# ever seen by anyone, they now have access to your database. -# -# Instead, provide the password or a full connection URL as an environment -# variable when you boot the app. For example: -# -# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" -# -# If the connection URL is provided in the special DATABASE_URL environment -# variable, Rails will automatically merge its configuration values on top of -# the values provided in this file. Alternatively, you can specify a connection -# URL environment variable explicitly: -# -# production: -# url: <%= ENV["MY_APP_DATABASE_URL"] %> -# -# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database -# for a full overview on how database connection configuration can be specified. -# production: <<: *default database: vmaxnet_api_production username: vmaxnet_api - password: <%= ENV["VMAXNET_API_DATABASE_PASSWORD"] %> + password: <%= ENV["VMAXNET_API_DATABASE_PASSWORD"] %> \ No newline at end of file diff --git a/db/migrate/20241017031706_create_clients.rb b/db/migrate/20241017031706_create_clients.rb new file mode 100644 index 0000000..813d192 --- /dev/null +++ b/db/migrate/20241017031706_create_clients.rb @@ -0,0 +1,21 @@ +class CreateClients < ActiveRecord::Migration[7.0] + def change + create_table :clients do |t| + t.string :type + t.string :first_name + t.string :how_find_us + t.string :nickname + t.string :cpf + t.string :rg + t.date :birth_date + t.string :legal_name + t.string :trade_name + t.string :cnpj + t.string :ie + t.date :foundation_date + t.boolean :has_partnership + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b783f98..00933b7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,8 +10,26 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 0) do +ActiveRecord::Schema[7.0].define(version: 2024_10_17_031706) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "clients", force: :cascade do |t| + t.string "type" + t.string "first_name" + t.string "how_find_us" + t.string "nickname" + t.string "cpf" + t.string "rg" + t.date "birth_date" + t.string "legal_name" + t.string "trade_name" + t.string "cnpj" + t.string "ie" + t.date "foundation_date" + t.boolean "has_partnership" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/test/fixtures/clients.yml b/test/fixtures/clients.yml new file mode 100644 index 0000000..1f06e29 --- /dev/null +++ b/test/fixtures/clients.yml @@ -0,0 +1,31 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + type: + first_name: MyString + how_find_us: MyString + nickname: MyString + cpf: MyString + rg: MyString + birth_date: 2024-10-17 + legal_name: MyString + trade_name: MyString + cnpj: MyString + ie: MyString + foundation_date: 2024-10-17 + has_partnership: false + +two: + type: + first_name: MyString + how_find_us: MyString + nickname: MyString + cpf: MyString + rg: MyString + birth_date: 2024-10-17 + legal_name: MyString + trade_name: MyString + cnpj: MyString + ie: MyString + foundation_date: 2024-10-17 + has_partnership: false diff --git a/test/models/client_test.rb b/test/models/client_test.rb new file mode 100644 index 0000000..c3c6d6c --- /dev/null +++ b/test/models/client_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ClientTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 2faa52542c4798a9ff81084146823fed4698af0a Mon Sep 17 00:00:00 2001 From: AlansidneyJunior Date: Thu, 17 Oct 2024 00:51:06 -0300 Subject: [PATCH 2/4] Criando tabelas filhas CorporateClient e IndividualClient e suas validacoes --- app/models/corporateClient.rb | 9 +++++++++ app/models/individualClient.rb | 8 ++++++++ db/schema.rb | 12 ------------ 3 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 app/models/corporateClient.rb create mode 100644 app/models/individualClient.rb diff --git a/app/models/corporateClient.rb b/app/models/corporateClient.rb new file mode 100644 index 0000000..0721fbf --- /dev/null +++ b/app/models/corporateClient.rb @@ -0,0 +1,9 @@ +class CorporateClient < Client + validates :legal_name, presence: true + validates :trade_name, presence: true + validates :cnpj, presence: true, uniqueness: true + validates :ie, presence: true + validates :foundation_date, presence: true + validates :has_partnership, inclusion: { in: [true, false] } + validates :how_find_us, presence: false +end \ No newline at end of file diff --git a/app/models/individualClient.rb b/app/models/individualClient.rb new file mode 100644 index 0000000..a480015 --- /dev/null +++ b/app/models/individualClient.rb @@ -0,0 +1,8 @@ +class IndividualClient < Client + validates :first_name, presence: true + validates :nickname, presence: false + validates :cpf, presence: true, uniqueness: true + validates :rg, presence: true + validates :birth_date, presence: true + validates :how_find_us, presence: false +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 00933b7..6ffcf5b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,15 +1,3 @@ -# 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.0].define(version: 2024_10_17_031706) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" From 1f79f6894b3866bbdfd34e5e695426142788a27b Mon Sep 17 00:00:00 2001 From: AlansidneyJunior Date: Thu, 17 Oct 2024 01:03:12 -0300 Subject: [PATCH 3/4] Criando e testando rotas index e show para tabelas Client, CorporateClient e IndividualClient --- app/controllers/clients_controller.rb | 11 +++++++++++ app/controllers/corporate_clients_controller.rb | 11 +++++++++++ app/controllers/individual_clients_controller.rb | 11 +++++++++++ config/routes.rb | 7 +++---- test/controllers/corporate_clients_controller_test.rb | 7 +++++++ .../controllers/individual_clients_controller_test.rb | 7 +++++++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 app/controllers/clients_controller.rb create mode 100644 app/controllers/corporate_clients_controller.rb create mode 100644 app/controllers/individual_clients_controller.rb create mode 100644 test/controllers/corporate_clients_controller_test.rb create mode 100644 test/controllers/individual_clients_controller_test.rb diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb new file mode 100644 index 0000000..f7acedd --- /dev/null +++ b/app/controllers/clients_controller.rb @@ -0,0 +1,11 @@ +class ClientsController < ApplicationController + def index + @clients = Client.all + render json: @clients + end + + def show + @client = Client.find(params[:id]) + render json: @client + end +end diff --git a/app/controllers/corporate_clients_controller.rb b/app/controllers/corporate_clients_controller.rb new file mode 100644 index 0000000..f3bb064 --- /dev/null +++ b/app/controllers/corporate_clients_controller.rb @@ -0,0 +1,11 @@ +class CorporateClientsController < ApplicationController + def index + @corporate_clients = CorporateClient.all + render json: @corporate_clients + end + + def show + @corporate_client = CorporateClient.find(params[:id]) + render json: @corporate_client + end +end diff --git a/app/controllers/individual_clients_controller.rb b/app/controllers/individual_clients_controller.rb new file mode 100644 index 0000000..4c0fe93 --- /dev/null +++ b/app/controllers/individual_clients_controller.rb @@ -0,0 +1,11 @@ +class IndividualClientsController < ApplicationController + def index + @individual_clients = IndividualClient.all + render json: @individual_clients + end + + def show + @individual_client = IndividualClient.find(params[:id]) + render json: @individual_client + end +end diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..eb19eac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,5 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Defines the root path route ("/") - # root "articles#index" + resources :corporate_clients, only: [:index, :show] + resources :individual_clients, only: [:index, :show] + resources :clients, only: [:index, :show] end diff --git a/test/controllers/corporate_clients_controller_test.rb b/test/controllers/corporate_clients_controller_test.rb new file mode 100644 index 0000000..2c69504 --- /dev/null +++ b/test/controllers/corporate_clients_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CorporateClientsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/individual_clients_controller_test.rb b/test/controllers/individual_clients_controller_test.rb new file mode 100644 index 0000000..e0a37f2 --- /dev/null +++ b/test/controllers/individual_clients_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class IndividualClientsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From 6db2dd08bb176ace677c1b1af0dbda07970374a6 Mon Sep 17 00:00:00 2001 From: AtanaelVmax22 Date: Thu, 24 Oct 2024 12:03:47 -0300 Subject: [PATCH 4/4] Corrigindo conflitos e testando todas as rotas de Address, CorporateClient e IndividualClient --- app/controllers/clients_controller.rb | 9 ----- app/models/corporateClient.rb | 2 +- app/models/individualClient.rb | 2 +- config/database.yml | 2 +- config/routes.rb | 1 - db/migrate/20241008044353_create_addresses.rb | 2 +- db/schema.rb | 39 +++++++++---------- 7 files changed, 22 insertions(+), 35 deletions(-) diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index f7acedd..b6b853c 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,11 +1,2 @@ class ClientsController < ApplicationController - def index - @clients = Client.all - render json: @clients - end - - def show - @client = Client.find(params[:id]) - render json: @client - end end diff --git a/app/models/corporateClient.rb b/app/models/corporateClient.rb index 0721fbf..04afaba 100644 --- a/app/models/corporateClient.rb +++ b/app/models/corporateClient.rb @@ -6,4 +6,4 @@ class CorporateClient < Client validates :foundation_date, presence: true validates :has_partnership, inclusion: { in: [true, false] } validates :how_find_us, presence: false -end \ No newline at end of file +end diff --git a/app/models/individualClient.rb b/app/models/individualClient.rb index a480015..0b65895 100644 --- a/app/models/individualClient.rb +++ b/app/models/individualClient.rb @@ -5,4 +5,4 @@ class IndividualClient < Client validates :rg, presence: true validates :birth_date, presence: true validates :how_find_us, presence: false -end \ No newline at end of file +end diff --git a/config/database.yml b/config/database.yml index 9304a4e..442239e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -19,4 +19,4 @@ production: <<: *default database: vmaxnet_api_production username: vmaxnet_api - password: <%= ENV["VMAXNET_API_DATABASE_PASSWORD"] %> \ No newline at end of file + password: <%= ENV["VMAXNET_API_DATABASE_PASSWORD"] %> diff --git a/config/routes.rb b/config/routes.rb index 96f553b..f1baaac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,5 @@ Rails.application.routes.draw do resources :corporate_clients, only: [:index, :show] resources :individual_clients, only: [:index, :show] - resources :clients, only: [:index, :show] resources :addresses, only: [:index, :show] end diff --git a/db/migrate/20241008044353_create_addresses.rb b/db/migrate/20241008044353_create_addresses.rb index 67684f4..e0973cc 100644 --- a/db/migrate/20241008044353_create_addresses.rb +++ b/db/migrate/20241008044353_create_addresses.rb @@ -1,5 +1,5 @@ class CreateAddresses < ActiveRecord::Migration[7.0] - def create_table + def change create_table :addresses do |t| t.integer :address_type t.string :street_name diff --git a/db/schema.rb b/db/schema.rb index 5b17e1e..b17b80e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,23 +1,3 @@ - -ActiveRecord::Schema[7.0].define(version: 2024_10_17_031706) do - # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" - - create_table "clients", force: :cascade do |t| - t.string "type" - t.string "first_name" - t.string "how_find_us" - t.string "nickname" - t.string "cpf" - t.string "rg" - t.date "birth_date" - t.string "legal_name" - t.string "trade_name" - t.string "cnpj" - t.string "ie" - t.date "foundation_date" - t.boolean "has_partnership" - # 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. @@ -30,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_10_09_033130) do +ActiveRecord::Schema[7.0].define(version: 2024_10_17_031706) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,7 +26,24 @@ t.string "complement" t.float "latitude" t.float "longitude" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "clients", force: :cascade do |t| + t.string "type" + t.string "first_name" + t.string "how_find_us" + t.string "nickname" + t.string "cpf" + t.string "rg" + t.date "birth_date" + t.string "legal_name" + t.string "trade_name" + t.string "cnpj" + t.string "ie" + t.date "foundation_date" + t.boolean "has_partnership" t.datetime "created_at", null: false t.datetime "updated_at", null: false end