Skip to content

Commit f5373c4

Browse files
committed
relationship product/items working
1 parent 5b44861 commit f5373c4

File tree

102 files changed

+1079
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1079
-9
lines changed

.gitignore

100644100755
File mode changed.

.rspec

100644100755
File mode changed.

.ruby-version

100644100755
File mode changed.

Dockerfile

100644100755
File mode changed.

Gemfile

100644100755
File mode changed.

Gemfile.lock

100644100755
File mode changed.

README.md

100644100755
File mode changed.

Rakefile

100644100755
File mode changed.

app/controllers/application_controller.rb

100644100755
File mode changed.

app/controllers/clients_controller.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class ClientsController < ApplicationController
2+
before_action :set_client, only: [:show, :update, :destroy]
3+
4+
# GET /clients
5+
def index
6+
@clients = Client.all
7+
8+
render json: @clients
9+
end
10+
11+
# GET /clients/1
12+
def show
13+
render json: @client
14+
end
15+
16+
# POST /clients
17+
def create
18+
@client = Client.new(client_params)
19+
20+
if @client.save
21+
render json: @client, status: :created, location: @client
22+
else
23+
render json: @client.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /clients/1
28+
def update
29+
if @client.update(client_params)
30+
render json: @client
31+
else
32+
render json: @client.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /clients/1
37+
def destroy
38+
@client.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_client
44+
@client = Client.find(params[:id])
45+
end
46+
47+
# Only allow a trusted parameter "white list" through.
48+
def client_params
49+
params.require(:client).permit(:cuil_cuit, :email, :name, :vat_condition_id)
50+
end
51+
end

app/controllers/concerns/.keep

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class ContactPhonesController < ApplicationController
2+
before_action :set_contact_phone, only: [:show, :update, :destroy]
3+
4+
# GET /contact_phones
5+
def index
6+
@contact_phones = ContactPhone.all
7+
8+
render json: @contact_phones
9+
end
10+
11+
# GET /contact_phones/1
12+
def show
13+
render json: @contact_phone
14+
end
15+
16+
# POST /contact_phones
17+
def create
18+
@contact_phone = ContactPhone.new(contact_phone_params)
19+
20+
if @contact_phone.save
21+
render json: @contact_phone, status: :created, location: @contact_phone
22+
else
23+
render json: @contact_phone.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /contact_phones/1
28+
def update
29+
if @contact_phone.update(contact_phone_params)
30+
render json: @contact_phone
31+
else
32+
render json: @contact_phone.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /contact_phones/1
37+
def destroy
38+
@contact_phone.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_contact_phone
44+
@contact_phone = ContactPhone.find(params[:id])
45+
end
46+
47+
# Only allow a trusted parameter "white list" through.
48+
def contact_phone_params
49+
params.require(:contact_phone).permit(:phone, :client_id)
50+
end
51+
end

app/controllers/items_controller.rb

100644100755
+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ class ItemsController < ApplicationController
33

44
# GET /items
55
def index
6-
@items = Item.all
7-
6+
# @items = Item.all
7+
@product = Product.find(params[:producto_id])
8+
@items = @product.items
89
render json: @items
910
end
1011

app/controllers/products_controller.rb

100644100755
File mode changed.

app/controllers/users_controller.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class UsersController < ApplicationController
2+
before_action :set_user, only: [:show, :update, :destroy]
3+
4+
# GET /users
5+
def index
6+
@users = User.all
7+
8+
render json: @users
9+
end
10+
11+
# GET /users/1
12+
def show
13+
render json: @user
14+
end
15+
16+
# POST /users
17+
def create
18+
@user = User.new(user_params)
19+
20+
if @user.save
21+
render json: @user, status: :created, location: @user
22+
else
23+
render json: @user.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /users/1
28+
def update
29+
if @user.update(user_params)
30+
render json: @user
31+
else
32+
render json: @user.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /users/1
37+
def destroy
38+
@user.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_user
44+
@user = User.find(params[:id])
45+
end
46+
47+
# Only allow a trusted parameter "white list" through.
48+
def user_params
49+
params.require(:user).permit(:username, :password, :token, :token_created_at)
50+
end
51+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class VatConditionsController < ApplicationController
2+
before_action :set_vat_condition, only: [:show, :update, :destroy]
3+
4+
# GET /vat_conditions
5+
def index
6+
@vat_conditions = VatCondition.all
7+
8+
render json: @vat_conditions
9+
end
10+
11+
# GET /vat_conditions/1
12+
def show
13+
render json: @vat_condition
14+
end
15+
16+
# POST /vat_conditions
17+
def create
18+
@vat_condition = VatCondition.new(vat_condition_params)
19+
20+
if @vat_condition.save
21+
render json: @vat_condition, status: :created, location: @vat_condition
22+
else
23+
render json: @vat_condition.errors, status: :unprocessable_entity
24+
end
25+
end
26+
27+
# PATCH/PUT /vat_conditions/1
28+
def update
29+
if @vat_condition.update(vat_condition_params)
30+
render json: @vat_condition
31+
else
32+
render json: @vat_condition.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /vat_conditions/1
37+
def destroy
38+
@vat_condition.destroy
39+
end
40+
41+
private
42+
# Use callbacks to share common setup or constraints between actions.
43+
def set_vat_condition
44+
@vat_condition = VatCondition.find(params[:id])
45+
end
46+
47+
# Only allow a trusted parameter "white list" through.
48+
def vat_condition_params
49+
params.require(:vat_condition).permit(:code, :description)
50+
end
51+
end

app/jobs/application_job.rb

100644100755
File mode changed.

app/models/application_record.rb

100644100755
File mode changed.

app/models/client.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Client < ApplicationRecord
2+
belongs_to :vat_condition
3+
has_many :contact_phone, inverse_of: :client
4+
end

app/models/concerns/.keep

100644100755
File mode changed.

app/models/contact_phone.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ContactPhone < ApplicationRecord
2+
belongs_to :client
3+
end

app/models/item.rb

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Item < ApplicationRecord
2+
belongs_to :product
23
end

app/models/product.rb

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Product < ApplicationRecord
2-
belongs_to :items
2+
has_many :items
33
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User < ApplicationRecord
2+
end

app/models/vat_condition.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class VatCondition < ApplicationRecord
2+
end

app/serializers/client_serializer.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ClientSerializer < ActiveModel::Serializer
2+
attributes :id, :cuil_cuit, :email, :name
3+
has_one :vat_condition
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ContactPhoneSerializer < ActiveModel::Serializer
2+
attributes :id, :phone
3+
has_one :client
4+
end

app/serializers/item_serializer.rb

100644100755
File mode changed.

app/serializers/product_serializer.rb

100644100755
-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
class ProductSerializer < ActiveModel::Serializer
22
attributes :id, :unique_code, :description, :detail, :unit_price
3-
has_one :items
43
end

app/serializers/user_serializer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class UserSerializer < ActiveModel::Serializer
2+
attributes :id, :username, :password, :token, :token_created_at
3+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class VatConditionSerializer < ActiveModel::Serializer
2+
attributes :id, :code, :description
3+
end

config.ru

100644100755
File mode changed.

config/application.rb

100644100755
File mode changed.

config/boot.rb

100644100755
File mode changed.

config/credentials.yml.enc

100644100755
File mode changed.

config/database.yml

100644100755
File mode changed.

config/environment.rb

100644100755
File mode changed.

config/environments/development.rb

100644100755
File mode changed.

config/environments/production.rb

100644100755
File mode changed.

config/environments/test.rb

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi

config/initializers/application_controller_renderer.rb

100644100755
File mode changed.

config/initializers/backtrace_silencers.rb

100644100755
File mode changed.

config/initializers/cors.rb

100644100755
File mode changed.

config/initializers/filter_parameter_logging.rb

100644100755
File mode changed.

config/initializers/inflections.rb

100644100755
File mode changed.

config/initializers/mime_types.rb

100644100755
File mode changed.

config/initializers/wrap_parameters.rb

100644100755
File mode changed.

config/locales/en.yml

100644100755
File mode changed.

config/puma.rb

100644100755
File mode changed.

config/routes.rb

100644100755
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Rails.application.routes.draw do
2-
resources :products do
2+
resources :users
3+
resources :contact_phones
4+
resources :clients
5+
resources :vat_conditions
6+
resources :products, as: :productos, path: "productos" do
37
resources :items
48
end
59
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

config/spring.rb

100644100755
File mode changed.

db/migrate/20191122184119_create_items.rb

100644100755
File mode changed.

db/migrate/20191122184134_create_products.rb

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateVatConditions < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :vat_conditions do |t|
4+
t.string :code
5+
t.string :description
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateClients < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :clients do |t|
4+
t.string :cuil_cuit
5+
t.string :email
6+
t.string :name
7+
t.references :vat_condition, null: false, foreign_key: true
8+
9+
t.timestamps
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateContactPhones < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :contact_phones do |t|
4+
t.string :phone
5+
t.references :client, null: false, foreign_key: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateUsers < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :users do |t|
4+
t.string :username
5+
t.string :password
6+
t.string :token
7+
t.datetime :token_created_at
8+
9+
t.timestamps
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveItemsFromProduct < ActiveRecord::Migration[6.0]
2+
def change
3+
remove_reference :products, :items, null: false, foreign_key: true
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class AddProductFromItem < ActiveRecord::Migration[6.0]
2+
def change
3+
end
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddProductToItem < ActiveRecord::Migration[6.0]
2+
def change
3+
add_reference :items, :product, null: false, foreign_key: true
4+
end
5+
end

0 commit comments

Comments
 (0)