Class: Goldencobra::Api::V2::ArticlesController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/goldencobra/api/v2/articles_controller.rb

Instance Method Summary collapse

Instance Method Details



157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 157

def breadcrumb
  breadcrumb = []
  @article.path.each do |art|
    breadcrumb << {
      title: art.breadcrumb_name,
      url: art.public_url
    }
  end
  respond_to do |format|
    format.json { render json: breadcrumb.to_json }
  end
end

#createObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 124

def create
  if existing_articles.any?
    render status: 423, json: {
      status: 423,
      error: "article already exists",
      id: existing_articles.first.id
    } and return
  end

  # Try to save the article
  response = create_article(params[:article])
  if response.id.present?
    render status: 200, json: { status: 200, id: response.id }
  else
    render status: 500, json: {
      status: 500,
      error: response.errors,
      id: nil
    }
  end
end

#indexjson

/api/v2/articles

map{ |c| [c.parent_path, c.id] }

Returns:

  • (json)

    Liefert Alle Artikel :id, :title, :parent_path



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
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 39

def index
  if params[:article_ids].present?
    index_with_ids
  else
    @articles = cached_articles

    respond_to do |format|
      format.json do
        render json: Oj.dump(
          { articles: articles_as_json },
          mode: :compat
        )
      end
      # Returns all publicly visible, active Articles
      format.xml do
        @articles = Goldencobra::Article.
                    new.
                    filter_with_permissions(
                      Goldencobra::Article.active,
                      nil
                    )
      end
    end
  end
end

#index_with_idsjson

Parameters:

  • methods (String)

    “beliebe Attribute des Artikels im JSON einfuegen”

Returns:

  • (json)

    liefert für Artikel mit einer bestimmten URL Kinderartikel zurück, die wiederum per IDs angefragt wurden

    im JSON Response befinden sich entweder alle Attribute, oder nur die mit params definierten



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 100

def index_with_ids
  article_ids = params[:article_ids]
  cache_key ||= ["indexarticles", article_ids]

  articles = Rails.cache.fetch(cache_key) do
    Goldencobra::Article.where("id IN (?)", article_ids)
  end
  respond_to do |format|
    format.json do
      if params[:methods].present?
        render json: Oj.dump(
          articles,
          each_serializer: Goldencobra::ArticleCustomSerializer,
          scope: params[:methods]
        )
      else
        render json: Oj.dump(articles)
      end
    end
  end
end

#searchObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 16

def search
  # Check if we have an argument.
  unless params[:q]
    render status: 200, json: { status: 200 }
    return
  end

  # Check if the query string contains something.
  if params[:q].length == 0
    render status: 200, json: { status: 200 }
  else
    # Search and return the result array.
    render status: 200, json: Goldencobra::Article.simple_search(
      ActionController::Base.helpers.sanitize(params[:q])
    ).to_json
  end
end

#showjson

Parameters:

  • methods (String)

    “beliebe Attribute des Artikels im JSON einfuegen”

Returns:

  • (json)

    Liefert Artikel mit einer bestimmten URL Im JSON Response befinden sich entweder alle Attribute, oder nur die mit params definierten



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 73

def show
  respond_to do |format|
    format.json do
      if params[:methods].present?
        render json: Oj.dump(
          @article.as_json,
          serializer: Goldencobra::ArticleCustomSerializer,
          scope: params[:methods]
        )
      else
        render json: Oj.dump(@article.as_json)
      end
    end
  end
end

#updateObject



147
148
149
150
151
152
153
154
155
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 147

def update
  if @article.update_attributes(params[:article])
    render status: 200, json: { status: 200, id: @article.id }
    # Erst render Ergebnis dann den Rest machen
    create_images(@article, params[:images])
  else
    render_error(response.errors, 500)
  end
end