-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad_api_controller.rb
More file actions
37 lines (30 loc) · 853 Bytes
/
Copy pathad_api_controller.rb
File metadata and controls
37 lines (30 loc) · 853 Bytes
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
# frozen_string_literal: true
class AdApiController < ApplicationController
def view
array = []
reports = []
target_ids = Ad.pluck(:id).sample(params[:count].to_i)
ads = Ad.find(target_ids)
ads.each do |ad|
report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id])
unless report
report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id])
end
report.imp += 1
reports.push(report)
array.push(
img_url: ad.image,
body: ad.text,
ad_id: ad.id
)
end
Report.import reports, on_duplicate_key_update: [:imp]
render json: array
end
def click
report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id])
report.click += 1
report.totalcost += Ad.find(params[:ad_id]).price
report.save
end
end