From 88e3d1fecb26c97e2abf3b83896fecb1d42fca39 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 8 Jun 2016 06:56:05 +0200 Subject: [PATCH] Added expand format for data rest api (deliver records with filled reverences for "group_id: 123" add automatically "group: 'some name'"). --- app/controllers/application_controller.rb | 21 ++++++- app/controllers/organizations_controller.rb | 24 +++++++- app/controllers/tickets_controller.rb | 37 ++++++++++++ app/controllers/users_controller.rb | 36 +++++++++++- app/models/application_model.rb | 65 ++++++++++++++++++++- app/models/user.rb | 24 ++++---- 6 files changed, 190 insertions(+), 17 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c7cce27e4..3203eaca7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -400,6 +400,11 @@ class ApplicationController < ActionController::Base # set relations generic_object.param_set_associations(params) + if params[:expand] + render json: generic_object.attributes_with_relation_names, status: :created + return + end + model_create_render_item(generic_object) rescue => e logger.error e.message @@ -425,6 +430,11 @@ class ApplicationController < ActionController::Base # set relations generic_object.param_set_associations(params) + if params[:expand] + render json: generic_object.attributes_with_relation_names, status: :ok + return + end + model_update_render_item(generic_object) rescue => e logger.error e.message @@ -454,7 +464,7 @@ class ApplicationController < ActionController::Base if params[:expand] generic_object = object.find(params[:id]) - model_show_render_item(generic_object) + render json: generic_object.attributes_with_relation_names, status: :ok return end @@ -489,6 +499,15 @@ class ApplicationController < ActionController::Base object.all.offset(offset).limit(limit) end + if params[:expand] + list = [] + generic_objects.each {|generic_object| + list.push generic_object.attributes_with_relation_names + } + render json: list, status: :ok + return + end + if params[:full] assets = {} item_ids = [] diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 2fdc72f3c..f75ee53df 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -65,6 +65,15 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password} organizations = Organization.all.offset(offset).limit(per_page) end + if params[:expand] + list = [] + organizations.each {|organization| + list.push organization.attributes_with_relation_names + } + render json: list, status: :ok + return + end + if params[:full] assets = {} item_ids = [] @@ -112,11 +121,19 @@ curl http://localhost/api/v1/organizations/#{id} -v -u #{login}:#{password} return end end + + if params[:expand] + organization = Organization.find(params[:id]).attributes_with_relation_names + render json: organization, status: :ok + return + end + if params[:full] full = Organization.full(params[:id]) render json: full return end + model_show_render(Organization, params) end @@ -200,6 +217,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co model_destory_render(Organization, params) end + # GET /api/v1/organizations/search def search if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?(Z_ROLENAME_AGENT) @@ -231,7 +249,11 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co end if params[:expand] - render json: organization_all + list = [] + organization_all.each {|organization| + list.push organization.attributes_with_relation_names + } + render json: list, status: :ok return end diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 2c1c21fa8..f291c31b8 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -16,6 +16,15 @@ class TicketsController < ApplicationController access_condition = Ticket.access_condition(current_user) tickets = Ticket.where(access_condition).offset(offset).limit(per_page) + if params[:expand] + list = [] + tickets.each {|ticket| + list.push ticket.attributes_with_relation_names + } + render json: list, status: :ok + return + end + if params[:full] assets = {} item_ids = [] @@ -40,6 +49,12 @@ class TicketsController < ApplicationController ticket = Ticket.find(params[:id]) return if !ticket_permission(ticket) + if params[:expand] + result = ticket.attributes_with_relation_names + render json: result, status: :ok + return + end + if params[:full] full = Ticket.full(params[:id]) render json: full @@ -90,6 +105,12 @@ class TicketsController < ApplicationController article_create(ticket, params[:article]) end + if params[:expand] + result = ticket.attributes_with_relation_names + render json: result, status: :created + return + end + render json: ticket, status: :created end @@ -109,6 +130,12 @@ class TicketsController < ApplicationController article_create(ticket, params[:article]) end + if params[:expand] + result = ticket.attributes_with_relation_names + render json: result, status: :ok + return + end + render json: ticket, status: :ok else render json: ticket.errors, status: :unprocessable_entity @@ -290,6 +317,16 @@ class TicketsController < ApplicationController condition: params[:condition], current_user: current_user, ) + + if params[:expand] + list = [] + tickets.each {|ticket| + list.push ticket.attributes_with_relation_names + } + render json: list, status: :ok + return + end + assets = {} ticket_result = [] tickets.each do |ticket| diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e06200a6a..a9291ede0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -27,6 +27,15 @@ class UsersController < ApplicationController User.all.offset(offset).limit(per_page) end + if params[:expand] + list = [] + users.each {|user| + list.push user.attributes_with_relation_names + } + render json: list, status: :ok + return + end + if params[:full] assets = {} item_ids = [] @@ -65,6 +74,12 @@ class UsersController < ApplicationController # access deny return if !permission_check + if params[:expand] + user = User.find(params[:id]).attributes_with_relation_names + render json: user, status: :ok + return + end + if params[:full] full = User.full(params[:id]) render json: full @@ -203,6 +218,13 @@ class UsersController < ApplicationController objects: result, ) end + + if params[:expand] + user = User.find(user.id).attributes_with_relation_names + render json: user, status: :created + return + end + user_new = User.find(user.id).attributes_with_associations user_new.delete('password') render json: user_new, status: :created @@ -253,8 +275,14 @@ class UsersController < ApplicationController user.param_set_associations({ organization_ids: params[:organization_ids], organizations: params[:organizations] }) end + if params[:expand] + user = User.find(user.id).attributes_with_relation_names + render json: user, status: :ok + return + end + # get new data - user_new = User.find(params[:id]).attributes_with_associations + user_new = User.find(user.id).attributes_with_associations user_new.delete('password') render json: user_new, status: :ok rescue => e @@ -327,7 +355,11 @@ class UsersController < ApplicationController end if params[:expand] - render json: user_all + list = [] + user_all.each {|user| + list.push user.attributes_with_relation_names + } + render json: list, status: :ok return end diff --git a/app/models/application_model.rb b/app/models/application_model.rb index c470d0965..410bd75dd 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -175,7 +175,7 @@ returns =begin -get rellations of model based on params +get relations of model based on params model = Model.find(1) attributes = model.attributes_with_associations @@ -200,6 +200,69 @@ returns =begin +get relation name of model based on params + + model = Model.find(1) + attributes = model.attributes_with_relation_names + +returns + + hash with attributes, association ids, association names and relation name + +=end + + def attributes_with_relation_names + + # get relations + attributes = attributes_with_associations + self.class.reflect_on_all_associations.map { |assoc| + next if !respond_to?(assoc.name) + ref = send(assoc.name) + next if !ref + if ref.respond_to?(:first) + attributes[assoc.name.to_s] = [] + ref.each {|item| + if item[:login] + attributes[assoc.name.to_s].push item[:login] + next + end + next if !item[:name] + attributes[assoc.name.to_s].push item[:name] + } + if ref.count > 0 && attributes[assoc.name.to_s].empty? + attributes.delete(assoc.name.to_s) + end + next + end + if ref[:login] + attributes[assoc.name.to_s] = ref[:login] + next + end + next if !ref[:name] + attributes[assoc.name.to_s] = ref[:name] + } + + # fill created_by/updated_by + { + 'created_by_id' => 'created_by', + 'updated_by_id' => 'updated_by', + }.each {|source, destination| + next if !attributes[source] + user = User.lookup(id: attributes[source]) + next if !user + attributes[destination] = user.login + } + + # remove forbitten attributes + %w(password token tokens token_ids).each {|item| + attributes.delete(item) + } + + attributes + end + +=begin + remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id) result = Model.param_validation(params) diff --git a/app/models/user.rb b/app/models/user.rb index 27dd8d142..acbb49f9f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -174,20 +174,20 @@ returns result = [ { - :id => 2, - :o_id => 2, - :created_by_id => 3, - :created_at => '2013-09-28 00:57:21', - :object => "User", - :type => "created", + id: 2, + o_id: 2, + created_by_id: 3, + created_at: '2013-09-28 00:57:21', + object: "User", + type: "created", }, { - :id => 2, - :o_id => 2, - :created_by_id => 3, - :created_at => '2013-09-28 00:59:21', - :object => "User", - :type => "updated", + id: 2, + o_id: 2, + created_by_id: 3, + created_at: '2013-09-28 00:59:21', + object: "User", + type: "updated", }, ]