/
repository_db_content_controller.rb
93 lines (76 loc) · 3.76 KB
/
repository_db_content_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
module Katello
module Concerns
module Api::V2::RepositoryDbContentController
extend ActiveSupport::Concern
included do
before_filter :find_optional_organization, :only => [:index]
end
extend ::Apipie::DSL::Concern
def index
collection = resource_class.scoped
collection = filter_by_repos(Repository.readable, collection)
collection = filter_by_repos([@repo], collection) if @repo && !@repo.puppet?
collection = filter_by_content_view_filter(@filter, collection) if @filter
collection = filter_by_content_view_version(@version, collection) if @version
collection = filter_by_repos(Repository.readable.in_organization(@organization), collection) if @organization
respond(:collection => scoped_search(collection.uniq, default_sort[0], default_sort[1]))
end
api :GET, "/compare/", N_("List :resource_id")
param :content_view_version_ids, Array, :desc => N_("content view versions to compare")
param :repository_id, :identifier, :desc => N_("Library repository id to restrict comparisons to")
def compare
fail _("No content_view_version_ids provided") if params[:content_view_version_ids].empty?
@versions = ContentViewVersion.readable.where(:id => params[:content_view_version_ids])
if @versions.count != params[:content_view_version_ids].uniq.length
missing = params[:content_view_version_ids] - @versions.pluck(:id)
fail HttpErrors::NotFound, _("Couldn't find content view versions '%s'") % missing.join(',')
end
collection = resource_class.scoped
repos = Katello::Repository.where(:content_view_version_id => @versions.pluck(:id))
repos = repos.where(:library_instance_id => @repo.id) if @repo
collection = scoped_search(filter_by_repos(repos, collection).uniq, default_sort[0], default_sort[1])
collection[:results] = collection[:results].map { |item| ContentViewVersionComparePresenter.new(item, @versions, @repo) }
respond_for_index(:collection => collection)
end
private
def default_sort
%w(id desc)
end
def filter_by_content_view_filter(filter, collection)
collection.where(:uuid => filter.send("#{singular_resource_name}_rules").pluck(:uuid))
end
def filter_by_repos(repos, collection)
collection.in_repositories(repos)
end
def filter_by_content_view_version(version, collection)
collection.where(:id => version.send(controller_name))
end
def find_content_resource
@resource = resource_class.with_uuid(params[:id]).first
if resource_class == Katello::Erratum
# also try to look up erratum by errata_id
@resource ||= Erratum.find_by_errata_id(params[:id])
end
if @resource.nil?
fail HttpErrors::NotFound, _("Failed to find %{content} with id '%{id}'.") %
{content: resource_name, id: params[:id]}
end
if params[:repository_id] && !@resource.repositories.include?(@repo)
fail HttpErrors::NotFound, _("Could not find %{content} with id '%{id}' in repository.") %
{content: resource_name, id: params[:id]}
end
end
end
end
end