From 5a9f9ee10cf2d18190f32efd5a3571161a3241b4 Mon Sep 17 00:00:00 2001 From: Mohammad Kanaan Date: Sat, 30 May 2026 19:15:24 +0300 Subject: [PATCH] WIP 1 --- app/controllers/price_reports_controller.rb | 9 +++++++-- app/models/price_report.rb | 2 ++ app/models/vote.rb | 8 ++++++++ config/routes.rb | 4 +++- db/migrate/20260530152329_create_votes.rb | 11 +++++++++++ db/migrate/20260530161118_change_voting_mechanism.rb | 6 ++++++ test/controllers/price_reports_controller_test.rb | 4 ++-- test/fixtures/price_reports.yml | 10 +++++----- test/fixtures/votes.yml | 11 +++++++++++ test/models/price_report_test.rb | 6 +++--- test/models/vote_test.rb | 7 +++++++ 11 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 app/models/vote.rb create mode 100644 db/migrate/20260530152329_create_votes.rb create mode 100644 db/migrate/20260530161118_change_voting_mechanism.rb create mode 100644 test/fixtures/votes.yml create mode 100644 test/models/vote_test.rb diff --git a/app/controllers/price_reports_controller.rb b/app/controllers/price_reports_controller.rb index 17b0fff..b1ca075 100644 --- a/app/controllers/price_reports_controller.rb +++ b/app/controllers/price_reports_controller.rb @@ -3,7 +3,8 @@ class PriceReportsController < ApplicationController # GET /price_reports def index - @price_reports = PriceReport.all + @product = Product.find(params[:product_id]) + @price_reports = @product.price_reports render json: @price_reports end @@ -33,6 +34,10 @@ class PriceReportsController < ApplicationController end end + def upvote + + end + # DELETE /price_reports/1 def destroy @price_report.destroy! @@ -46,6 +51,6 @@ class PriceReportsController < ApplicationController # Only allow a list of trusted parameters through. def price_report_params - params.expect(price_report: [ :price, :discount_price, :upvotes, :downvotes, :reported_at, :product_id, :store_id, :guest_id ]) + params.require(:price_report).permit(:price, :discount_price, :store_id) end end diff --git a/app/models/price_report.rb b/app/models/price_report.rb index b81230b..d0f6e57 100644 --- a/app/models/price_report.rb +++ b/app/models/price_report.rb @@ -31,4 +31,6 @@ class PriceReport < ApplicationRecord belongs_to :store belongs_to :user has_many :moderation_logs, as: :moderatable, dependent: :destroy + has_many :votes, as: :votable, dependent: :destroy + end diff --git a/app/models/vote.rb b/app/models/vote.rb new file mode 100644 index 0000000..642fb84 --- /dev/null +++ b/app/models/vote.rb @@ -0,0 +1,8 @@ +class Vote < ApplicationRecord + belongs_to :user + belongs_to :votable, polymorphic: true + + enum :direction, {upvote: 0, downvote: 1} + + validates :user_id, uniqueness: { scope: [:votable_type, :votable_id], message: "has already voted on this item" } +end diff --git a/config/routes.rb b/config/routes.rb index c73bc0a..a568599 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,9 @@ Rails.application.routes.draw do resources :users resources :stores resources :chains - resources :products + resources :products do + resources :price_reports, only: [:index, :create] + end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/db/migrate/20260530152329_create_votes.rb b/db/migrate/20260530152329_create_votes.rb new file mode 100644 index 0000000..db7bb1d --- /dev/null +++ b/db/migrate/20260530152329_create_votes.rb @@ -0,0 +1,11 @@ +class CreateVotes < ActiveRecord::Migration[8.0] + def change + create_table :votes do |t| + t.references :user, null: false, foreign_key: true + t.string :votable=references{polymorphic} + t.integer :direction + + t.timestamps + end + end +end diff --git a/db/migrate/20260530161118_change_voting_mechanism.rb b/db/migrate/20260530161118_change_voting_mechanism.rb new file mode 100644 index 0000000..8c0ba6c --- /dev/null +++ b/db/migrate/20260530161118_change_voting_mechanism.rb @@ -0,0 +1,6 @@ +class ChangeVotingMechanism < ActiveRecord::Migration[8.0] + def change + remove_column :price_reports, :upvotes + remove_column :price_reports, :downvotes + end +end diff --git a/test/controllers/price_reports_controller_test.rb b/test/controllers/price_reports_controller_test.rb index cb794e3..be308a6 100644 --- a/test/controllers/price_reports_controller_test.rb +++ b/test/controllers/price_reports_controller_test.rb @@ -12,7 +12,7 @@ class PriceReportsControllerTest < ActionDispatch::IntegrationTest test "should create price_report" do assert_difference("PriceReport.count") do - post price_reports_url, params: { price_report: { discount_price: @price_report.discount_price, downvotes: @price_report.downvotes, guest_id: @price_report.guest_id, price: @price_report.price, product_id: @price_report.product_id, reported_at: @price_report.reported_at, store_id: @price_report.store_id, upvotes: @price_report.upvotes } }, as: :json + post price_reports_url, params: { price_report: { discount_price: @price_report.discount_price, downvotes: @price_report.downvotes, user_id: @price_report.user_id, price: @price_report.price, product_id: @price_report.product_id, reported_at: @price_report.reported_at, store_id: @price_report.store_id, upvotes: @price_report.upvotes } }, as: :json end assert_response :created @@ -24,7 +24,7 @@ class PriceReportsControllerTest < ActionDispatch::IntegrationTest end test "should update price_report" do - patch price_report_url(@price_report), params: { price_report: { discount_price: @price_report.discount_price, downvotes: @price_report.downvotes, guest_id: @price_report.guest_id, price: @price_report.price, product_id: @price_report.product_id, reported_at: @price_report.reported_at, store_id: @price_report.store_id, upvotes: @price_report.upvotes } }, as: :json + patch price_report_url(@price_report), params: { price_report: { discount_price: @price_report.discount_price, downvotes: @price_report.downvotes, user_id: @price_report.user_id, price: @price_report.price, product_id: @price_report.product_id, reported_at: @price_report.reported_at, store_id: @price_report.store_id, upvotes: @price_report.upvotes } }, as: :json assert_response :success end diff --git a/test/fixtures/price_reports.yml b/test/fixtures/price_reports.yml index 3444137..2d04fcc 100644 --- a/test/fixtures/price_reports.yml +++ b/test/fixtures/price_reports.yml @@ -12,21 +12,21 @@ # upvotes :integer # created_at :datetime not null # updated_at :datetime not null -# guest_id :integer not null # product_id :integer not null # store_id :integer not null +# user_id :bigint not null # # Indexes # -# index_price_reports_on_guest_id (guest_id) # index_price_reports_on_product_id (product_id) # index_price_reports_on_store_id (store_id) +# index_price_reports_on_user_id (user_id) # # Foreign Keys # -# fk_rails_... (guest_id => guests.id) # fk_rails_... (product_id => products.id) # fk_rails_... (store_id => stores.id) +# fk_rails_... (user_id => users.id) # one: price: 9.99 @@ -36,7 +36,7 @@ one: reported_at: 2026-05-17 14:11:07 product: one store: one - guest: one + user: one two: price: 9.99 @@ -46,4 +46,4 @@ two: reported_at: 2026-05-17 14:11:07 product: two store: two - guest: two + user: two diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml new file mode 100644 index 0000000..a73ae3c --- /dev/null +++ b/test/fixtures/votes.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user: one + votable=references{polymorphic}: MyString + direction: 1 + +two: + user: two + votable=references{polymorphic}: MyString + direction: 1 diff --git a/test/models/price_report_test.rb b/test/models/price_report_test.rb index 3fec053..4f5d96c 100644 --- a/test/models/price_report_test.rb +++ b/test/models/price_report_test.rb @@ -10,21 +10,21 @@ # upvotes :integer # created_at :datetime not null # updated_at :datetime not null -# guest_id :integer not null # product_id :integer not null # store_id :integer not null +# user_id :bigint not null # # Indexes # -# index_price_reports_on_guest_id (guest_id) # index_price_reports_on_product_id (product_id) # index_price_reports_on_store_id (store_id) +# index_price_reports_on_user_id (user_id) # # Foreign Keys # -# fk_rails_... (guest_id => guests.id) # fk_rails_... (product_id => products.id) # fk_rails_... (store_id => stores.id) +# fk_rails_... (user_id => users.id) # require "test_helper" diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb new file mode 100644 index 0000000..f567e87 --- /dev/null +++ b/test/models/vote_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class VoteTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end