Commit f719e97e0bc5025a6e2353ac094012e54f6c1275

Authored by Callum Dryden
1 parent f39fed7d
Exists in master and in 1 other branch production

Add a problems err and notices in api show action

app/controllers/api/v1/problems_controller.rb
1 class Api::V1::ProblemsController < ApplicationController 1 class Api::V1::ProblemsController < ApplicationController
2 respond_to :json, :xml 2 respond_to :json, :xml
  3 + FIELDS = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count}
3 4
4 def show 5 def show
5 result = benchmark("[api/v1/problems_controller/show] query time") do 6 result = benchmark("[api/v1/problems_controller/show] query time") do
6 - Problem.find(params[:id]) 7 + problem = Problem.only(FIELDS).find(params[:id])
  8 + problem.as_json(include: {errs: { include: :notices}})
7 end 9 end
8 10
  11 +
9 respond_to do |format| 12 respond_to do |format|
10 - format.any(:html, :json) { render :json => Yajl.dump(result) } # render JSON if no extension specified on path 13 + format.any(:html, :json) { render :json => result } # render JSON if no extension specified on path
11 format.xml { render :xml => result } 14 format.xml { render :xml => result }
12 end 15 end
13 end 16 end
14 17
15 def index 18 def index
16 query = {} 19 query = {}
17 - fields = %w{app_id app_name environment message where first_notice_at last_notice_at resolved resolved_at notices_count}  
18 20
19 if params.key?(:start_date) && params.key?(:end_date) 21 if params.key?(:start_date) && params.key?(:end_date)
20 start_date = Time.parse(params[:start_date]).utc 22 start_date = Time.parse(params[:start_date]).utc
@@ -23,7 +25,7 @@ class Api::V1::ProblemsController &lt; ApplicationController @@ -23,7 +25,7 @@ class Api::V1::ProblemsController &lt; ApplicationController
23 end 25 end
24 26
25 results = benchmark("[api/v1/problems_controller/index] query time") do 27 results = benchmark("[api/v1/problems_controller/index] query time") do
26 - Problem.where(query).with(:consistency => :strong).only(fields).to_a 28 + Problem.where(query).with(:consistency => :strong).only(FIELDS).to_a
27 end 29 end
28 30
29 respond_to do |format| 31 respond_to do |format|
spec/controllers/api/v1/problems_controller_spec.rb
@@ -9,7 +9,9 @@ describe Api::V1::ProblemsController do @@ -9,7 +9,9 @@ describe Api::V1::ProblemsController do
9 9
10 describe "GET /api/v1/problems/:id" do 10 describe "GET /api/v1/problems/:id" do
11 before do 11 before do
12 - Fabricate(:problem, :first_notice_at => Date.new(2012, 8, 01), :resolved_at => Date.new(2012, 8, 02)) 12 + notice = Fabricate(:notice)
  13 + err = Fabricate(:err, :notices => [notice])
  14 + problem = Fabricate(:problem, :errs => [err])
13 end 15 end
14 16
15 it "returns JSON if JSON is requested" do 17 it "returns JSON if JSON is requested" do
@@ -20,7 +22,7 @@ describe Api::V1::ProblemsController do @@ -20,7 +22,7 @@ describe Api::V1::ProblemsController do
20 it "returns the correct problem" do 22 it "returns the correct problem" do
21 requested_problem = Problem.first 23 requested_problem = Problem.first
22 get :show, :auth_token => @user.authentication_token, :format => "json", :id => requested_problem.id 24 get :show, :auth_token => @user.authentication_token, :format => "json", :id => requested_problem.id
23 - expect( response.body ).to eq(requested_problem.to_json) 25 + expect( response.body ).to eq(requested_problem.to_json(include: {errs: { include: :notices}}))
24 end 26 end
25 end 27 end
26 28