diff --git a/.DS_Store b/.DS_Store index 175ed52..78f3bb9 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Gemfile b/Gemfile index 98767cc..78d2eea 100644 --- a/Gemfile +++ b/Gemfile @@ -70,3 +70,4 @@ gem 'carrierwave' gem 'sassc' gem 'rubocop', '~> 0.47.1' gem 'htmlbeautifier' +gem 'activerecord-import' diff --git a/Gemfile.lock b/Gemfile.lock index 1fa33bf..9c2cc67 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,8 @@ GEM activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) + activerecord-import (1.0.2) + activerecord (>= 3.2) activestorage (5.2.3) actionpack (= 5.2.3) activerecord (= 5.2.3) @@ -102,7 +104,7 @@ GEM io-like (0.3.0) jbuilder (2.9.1) activesupport (>= 4.2.0) - jquery-rails (4.3.3) + jquery-rails (4.3.5) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) @@ -125,7 +127,7 @@ GEM mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) - msgpack (1.2.10) + msgpack (1.3.0) nio4r (2.3.1) nokogiri (1.10.3) mini_portile2 (~> 2.4.0) @@ -173,12 +175,12 @@ GEM rb-inotify (0.10.0) ffi (~> 1.0) regexp_parser (1.5.1) - rspec-core (3.8.0) + rspec-core (3.8.1) rspec-support (~> 3.8.0) - rspec-expectations (3.8.3) + rspec-expectations (3.8.4) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) - rspec-mocks (3.8.0) + rspec-mocks (3.8.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-rails (3.8.2) @@ -189,7 +191,7 @@ GEM rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-support (~> 3.8.0) - rspec-support (3.8.0) + rspec-support (3.8.2) rubocop (0.47.1) parser (>= 2.3.3.1, < 3.0) powerpack (~> 0.1) @@ -245,9 +247,9 @@ GEM activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) - websocket-driver (0.7.0) + websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.3) + websocket-extensions (0.1.4) xpath (3.2.0) nokogiri (~> 1.8) @@ -255,6 +257,7 @@ PLATFORMS ruby DEPENDENCIES + activerecord-import bootsnap (>= 1.1.0) bootstrap byebug diff --git a/app/assets/javascripts/ad_api.coffee b/app/assets/javascripts/ad_api.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/ad_api.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/ad_api.scss b/app/assets/stylesheets/ad_api.scss new file mode 100644 index 0000000..b15c608 --- /dev/null +++ b/app/assets/stylesheets/ad_api.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Ad_API controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/ad_api_controller.rb b/app/controllers/ad_api_controller.rb new file mode 100644 index 0000000..20d6dba --- /dev/null +++ b/app/controllers/ad_api_controller.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true +class AdApiController < ApplicationController + def get_ads + 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],date: Date.today) + unless report + report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today) + end + + report.imp += 1 + reports.push(report) + + array.push( + img_url: ad.image.url, + body: ad.text, + ad_id: ad.id + ) + end + Report.import reports, on_duplicate_key_update: [:imp] + render json: array + end + + def update_clicks + report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id],date: Date.today) + unless report + report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today) + end + report.click += 1 + report.price += Ad.find(params[:ad_id]).price + report.save + end + +end + diff --git a/app/helpers/ad_api_helper.rb b/app/helpers/ad_api_helper.rb new file mode 100644 index 0000000..b9fd8b9 --- /dev/null +++ b/app/helpers/ad_api_helper.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +module AdApiHelper +end diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 0000000..eb2599f --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +class Report < ApplicationRecord +end diff --git a/app/views/ad/index.html.erb b/app/views/ad/index.html.erb index 7296a3d..e322ba3 100644 --- a/app/views/ad/index.html.erb +++ b/app/views/ad/index.html.erb @@ -14,14 +14,14 @@ <%= image_tag ad.image.to_s %>
<% end %>Find me in app/views/ad_api/click.html.erb
diff --git a/app/views/ad_api/view_make_report.html.erb b/app/views/ad_api/view_make_report.html.erb new file mode 100644 index 0000000..b5fd078 --- /dev/null +++ b/app/views/ad_api/view_make_report.html.erb @@ -0,0 +1,2 @@ +Find me in app/views/ad_api/view.html.erb
diff --git a/config/routes.rb b/config/routes.rb index 4d99a71..49eb81a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true Rails.application.routes.draw do + get '/view' => 'ad_api#get_ads' + get '/click' => 'ad_api#update_clicks' resources :ad - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20190528085221_create_ads.rb b/db/migrate/20190528085221_create_ads.rb index d76d3d3..03e4ae9 100644 --- a/db/migrate/20190528085221_create_ads.rb +++ b/db/migrate/20190528085221_create_ads.rb @@ -2,9 +2,9 @@ class CreateAds < ActiveRecord::Migration[5.2] def change create_table :ads do |t| - t.integer :advertiser_id, null: false, default: 0 # 広告主ID + t.integer :advertiser_id, null: false # 広告主ID t.string :image, null: false, default: '' # 広告の画像URL - t.integer :price, null: false, default: 0 # 広告の価格 + t.integer :price, null: false # 広告の価格 t.string :text, null: false, default: '' # 広告の説明文 t.timestamps diff --git a/db/migrate/20190604073026_add_report_column_to_ads.rb b/db/migrate/20190604073026_add_report_column_to_ads.rb deleted file mode 100644 index bf8d75f..0000000 --- a/db/migrate/20190604073026_add_report_column_to_ads.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true -class AddReportColumnToAds < ActiveRecord::Migration[5.2] - def change - add_column :ads, :click, :integer - add_column :ads, :imp, :integer - add_column :ads, :cv, :integer - end -end diff --git a/db/migrate/20190614041856_create_repos.rb b/db/migrate/20190614041856_create_repos.rb new file mode 100644 index 0000000..3e1ddf2 --- /dev/null +++ b/db/migrate/20190614041856_create_repos.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true +class CreateRepos < ActiveRecord::Migration[5.2] + def change + create_table :reports do |t| + t.integer :ad_id, null: false + t.integer :adspot_id, null: false + t.integer :click, null: false, default: 0 + t.integer :imp, null: false, default: 0 + t.integer :cv, null: false, default: 0 + t.integer :price, null: false, default: 0 + t.date :date, null: false, unique: true + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d6923dc..f78b812 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,25 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_06_04_073026) do +ActiveRecord::Schema.define(version: 2019_06_14_041856) do create_table 'ads', force: :cascade do |t| - t.integer 'advertiser_id', default: 0, null: false + t.integer 'advertiser_id', null: false t.string 'image', default: '', null: false - t.integer 'price', default: 0, null: false + t.integer 'price', null: false t.string 'text', default: '', null: false t.datetime 'created_at', null: false t.datetime 'updated_at', null: false - t.integer 'click' - t.integer 'imp' - t.integer 'cv' + end + + create_table 'reports', force: :cascade do |t| + t.integer 'ad_id', null: false + t.integer 'adspot_id', null: false + t.integer 'click', default: 0, null: false + t.integer 'imp', default: 0, null: false + t.integer 'cv', default: 0, null: false + t.integer 'price', default: 0, null: false + t.date 'date', null: false + t.datetime 'created_at', null: false + t.datetime 'updated_at', null: false end end diff --git a/public/media.site.htm b/public/media.site.htm new file mode 100644 index 0000000..43a7aec --- /dev/null +++ b/public/media.site.htm @@ -0,0 +1,58 @@ + + + +