This commit is contained in:
Mohammad Kanaan 2026-05-30 19:15:24 +03:00
parent 362ed3596b
commit 5a9f9ee10c
11 changed files with 65 additions and 13 deletions

View file

@ -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

View file

@ -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

8
app/models/vote.rb Normal file
View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -0,0 +1,6 @@
class ChangeVotingMechanism < ActiveRecord::Migration[8.0]
def change
remove_column :price_reports, :upvotes
remove_column :price_reports, :downvotes
end
end

View file

@ -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

View file

@ -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

11
test/fixtures/votes.yml vendored Normal file
View file

@ -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

View file

@ -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"

7
test/models/vote_test.rb Normal file
View file

@ -0,0 +1,7 @@
require "test_helper"
class VoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end