summaryrefslogtreecommitdiff
path: root/app/controllers/api
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-03-10 19:21:00 -0600
committerHombreLaser <sebastian-440@live.com>2023-03-10 19:21:00 -0600
commit8f04e87b466e79fa2086d69d9c49f5af89b68cb9 (patch)
tree4c779a2801b7359c9cb94da1492a9087f6788d75 /app/controllers/api
parentf39235d865412d75625779a557a6c51736798dfa (diff)
Añade CompaniesController
Diffstat (limited to 'app/controllers/api')
-rw-r--r--app/controllers/api/companies_controller.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb
new file mode 100644
index 0000000..4317682
--- /dev/null
+++ b/app/controllers/api/companies_controller.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+module Api
+ # CompaniesController
+ class CompaniesController < MasterController
+ skip_before_action :validate_jwt, only: %i[show index]
+ skip_before_action :assert_master_role, onlt: %i[show index]
+
+ def index
+ @companies = Company.all
+
+ render json: serialized_collection.serializable_hash, status: 200
+ end
+
+ def show
+ @company = Company.find_by(short_name: params[:id])
+
+ render json: serialized_object.serializable_hash, status: :ok and return if @company
+
+ render json: { error_message: "No se encontró la compañía #{params[:short_name]}" }, status: :not_found
+ end
+
+ def create
+ @company = Company.new(permitted_params)
+
+ render json: serialized_object.serializable_hash, status: :ok and return if @company.save
+
+ render json: { error_messages: @company.errors.full_messages }, status: :unprocessable_entity
+ end
+
+ private
+
+ def serialized_object
+ Serializers::CompanySerializer.new(@company)
+ end
+
+ def serialized_collection
+ Serializers::CompanySerializer.new(@companies.page(params[:page]))
+ end
+
+ def permitted_params
+ params.permit(:name, :country, :short_name, :logo)
+ end
+ end
+end