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

#createObject



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

def create
  # Check if a user is currently logged in.
  unless current_user
    render status: 403, json: { :status => 403 }
    return
  end

  # Check if we do have an article passed by the parameters.
  unless params[:article]
    render status: 400, json: { :status => 400, :error => "article data missing" }
    return
  end

  #check if an external referee is passed by the parameters
  unless params[:referee_id]
    render status: 400, json: { :status => 400, :error => "referee_id missing"  }
    return
  end

  #check if Article already exists by comparing external referee and current user of caller
  existing_articles = Goldencobra::Article.where(:creator_id => current_user.id, :external_referee_id => params[:referee_id])
  if existing_articles.any?
    render status: 423, json: { :status => 423, :error => "article already exists", :id => existing_articles.first.id  }
    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, :ancestry



37
38
39
40
41
42
43
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 37

def index
  render status: 200, json: Goldencobra::Article.select([:id,:title, :ancestry]).sort{ |a, b| 
      a[0] <=> b[0]
    }.map{ |a| 
      a.as_json(:only => [:id, :title], :methods => [:parent_path])
    }
end

#searchObject



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

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

#updateObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/goldencobra/api/v2/articles_controller.rb', line 85

def update
  unless current_user
    render status: 403, json: { :status => 403 }
    return
  end

  # Check if we do have an article passed by the parameters.
  unless params[:article]
    render status: 400, json: { :status => 400, :error => "article data missing" }
    return
  end

  #check if an external referee is passed by the parameters
  unless params[:referee_id]
    render status: 400, json: { :status => 400, :error => "referee_id missing"  }
    return
  end

  #check if Article already exists by comparing external referee and current user of caller
  existing_articles = Goldencobra::Article.where(:creator_id => current_user.id, :external_referee_id => params[:referee_id])
  if existing_articles.blank?
    render status: 423, json: { :status => 423, :error => "article not found", :id => nil  }
    return
  end

  # Try to save the article
  response = update_article(params[:article])
  if response.id.present?
    render status: 200, json: { :status => 200, :id => response.id }

    #Erst render ergebnis dann den rest machen
    if params[:images].present?
      params[:images].each do |key,value|
        existing_images = Goldencobra::Upload.where(:image_remote_url => value[:image][:image_url])
        if existing_images.blank?
          img = Goldencobra::Upload.create(value[:image])
        else
          img = existing_images.first
        end
        image_position = Goldencobra::Setting.for_key("goldencobra.article.image_positions").to_s.split(",").map(&:strip).first
        existing_articles.first.article_images.create(:image => img, :position => image_position)
      end
    end

  else
    render status: 500, json: { :status => 500, :error => response.errors, :id => nil }
  end
end