diff --git a/.byebug_history b/.byebug_history index 4886822c9..dc0e6664e 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,3 +1,5 @@ +c +CarFilterService.new(filter_params, per_page).call.by_cpecial_offer q qxit n diff --git a/app/controllers/cars_controller.rb b/app/controllers/cars_controller.rb index 1593912d1..77a45979a 100644 --- a/app/controllers/cars_controller.rb +++ b/app/controllers/cars_controller.rb @@ -1,5 +1,5 @@ class CarsController < ApplicationController - before_action :set_car, only: [:show, :update, :destroy] + before_action :set_car, only: [ :show, :update, :destroy ] skip_before_action :verify_authenticity_token def index @@ -7,27 +7,26 @@ def index filtered_cars = CarFilterService.new(filter_params, per_page).call paginated_cars = filtered_cars.page(params[:page]).per(params[:per_page] || per_page) - if params[:price_asc] == 'true' + if params[:price_asc] == "true" paginated_cars = paginated_cars.order(price: :asc) end - if params[:price_desc] == 'true' + if params[:price_desc] == "true" paginated_cars = paginated_cars.order(price: :desc) end - if params[:mileage] == 'true' - paginated_cars = paginated_cars.order('history_cars.last_mileage ASC') + if params[:mileage] == "true" + paginated_cars = paginated_cars.order("history_cars.last_mileage ASC") end - if params[:newest] == 'true' + if params[:newest] == "true" paginated_cars = paginated_cars.order(year: :desc) end if request.format.html? render file: "#{Rails.root}/public/index.html", layout: false - elsif params[:coll] == 'all' + elsif params[:coll] == "all" render json: filtered_cars, each_serializer: CarSerializer else render json: paginated_cars, each_serializer: CarSerializer end - end def show @@ -114,16 +113,16 @@ def filters :year_from, :max_price, :gearbox_type_name, :body_type_name, :drive_type_name, :owners_count, :engine_name_type_name) # Добавлено result = CarFilterDataService.call(filters) - if params[:price_asc] == 'true' + if params[:price_asc] == "true" result = result.order(price: :asc) end - if params[:price_desc] == 'true' + if params[:price_desc] == "true" result = result.order(price: :desc) end - if params[:mileage] == 'true' - result = result.order('history_cars.last_mileage ASC') + if params[:mileage] == "true" + result = result.order("history_cars.last_mileage ASC") end - if params[:newest] == 'true' + if params[:newest] == "true" result = result.order(year: :desc) end render json: result @@ -151,32 +150,60 @@ def download_pdf car = Car.find(params[:id]) pdf = generate_pdf(car) # Метод для генерации PDF - send_data pdf.render, filename: "#{car.brand}_#{car.id}.pdf", type: 'application/pdf', disposition: 'attachment' + send_data pdf.render, filename: "#{car.brand}_#{car.id}.pdf", type: "application/pdf", disposition: "attachment" end def add_car render file: "#{Rails.root}/public/index.html", layout: false end - private - def set_car - @car = Car.find_by(id: params[:id]) - if @car.nil? - render file: "#{Rails.root}/public/404.html", status: :not_found, layout: false - end + def special_offer + per_page = 18 + filtered_cars = CarFilterService.new(filter_params, per_page).call.where(special_offer: true) + paginated_cars = filtered_cars.page(params[:page]).per(params[:per_page] || per_page) + + if params[:price_asc] == "true" + paginated_cars = paginated_cars.order(price: :asc) + end + if params[:price_desc] == "true" + paginated_cars = paginated_cars.order(price: :desc) + end + if params[:mileage] == "true" + paginated_cars = paginated_cars.order("history_cars.last_mileage ASC") + end + if params[:newest] == "true" + paginated_cars = paginated_cars.order(year: :desc) end - def car_params - params.require(:car).permit(:model_id, :brand_id, :year, :price, :description, - :color_id, :body_type_id, :engine_name_type_id, :engine_power_type_id, :engine_capacity_type_id, :gearbox_type_id, - :drive_type_id, :generation_id, :online_view_available, :complectation_name) + if request.format.html? + render file: "#{Rails.root}/public/index.html", layout: false + elsif params[:coll] == "all" + render json: filtered_cars, each_serializer: CarSerializer + else + render json: paginated_cars, each_serializer: CarSerializer end + end - def filter_params - params.permit(:id, :brand_name, :model_name, :generation_name, - :year_from, :max_price, :gearbox_type_name, :body_type_name, - :drive_type_name, :owners_count, :engine_name_type_name, :unique_id) + private + + def set_car + @car = Car.find_by(id: params[:id]) + if @car.nil? + render file: "#{Rails.root}/public/404.html", status: :not_found, layout: false end + end + + def car_params + params.require(:car).permit(:model_id, :brand_id, :year, :price, :description, + :color_id, :body_type_id, :engine_name_type_id, :engine_power_type_id, :engine_capacity_type_id, :gearbox_type_id, + :drive_type_id, :generation_id, :online_view_available, :complectation_name) + end + + def filter_params + params.permit(:id, :brand_name, :model_name, :generation_name, + :year_from, :max_price, :gearbox_type_name, :body_type_name, + :drive_type_name, :owners_count, :engine_name_type_name, :unique_id) + end def generate_pdf(car) # Логика для генерации PDF diff --git a/app/models/car.rb b/app/models/car.rb index e5815eb86..4059402b6 100644 --- a/app/models/car.rb +++ b/app/models/car.rb @@ -2,7 +2,7 @@ class Car < ApplicationRecord belongs_to :brand belongs_to :model belongs_to :generation - + belongs_to :color belongs_to :body_type belongs_to :engine_name_type @@ -10,7 +10,7 @@ class Car < ApplicationRecord belongs_to :engine_capacity_type belongs_to :gearbox_type belongs_to :drive_type - + has_many :call_requests has_many :images, dependent: :destroy has_many :history_cars, dependent: :destroy @@ -26,21 +26,21 @@ class Car < ApplicationRecord scope :by_year_from, -> (year) { where('year >= ?', year) if year.present? } scope :by_price, -> (max_price) { where('price <= ?', max_price) if max_price.present? } - - scope :by_gearbox_type, -> (gearbox_type_name) { - joins(:gearbox_type).where(gearbox_types: { name: gearbox_type_name }) if gearbox_type_name.present? + + scope :by_gearbox_type, -> (gearbox_type_name) { + joins(:gearbox_type).where(gearbox_types: { name: gearbox_type_name }) if gearbox_type_name.present? } - scope :by_body_type, -> (body_type_name) { - joins(:body_type).where(body_types: { name: body_type_name }) if body_type_name.present? + scope :by_body_type, -> (body_type_name) { + joins(:body_type).where(body_types: { name: body_type_name }) if body_type_name.present? } - scope :by_drive_type, -> (drive_type_name) { - joins(:drive_type).where(drive_types: { name: drive_type_name }) if drive_type_name.present? + scope :by_drive_type, -> (drive_type_name) { + joins(:drive_type).where(drive_types: { name: drive_type_name }) if drive_type_name.present? } - scope :by_owners_count, -> (owners_count) { - joins(:history_cars).where(history_cars: { previous_owners: owners_count }) if owners_count.present? + scope :by_owners_count, -> (owners_count) { + joins(:history_cars).where(history_cars: { previous_owners: owners_count }) if owners_count.present? } - scope :by_engine_name_type, -> (engine_name_type_name) { + scope :by_engine_name_type, -> (engine_name_type_name) { joins(:engine_name_type).where(engine_name_types: { name: engine_name_type_name }) if engine_name_type_name.present? } - + end diff --git a/config/database.yml b/config/database.yml index 44bbc6c87..08ff39d12 100644 --- a/config/database.yml +++ b/config/database.yml @@ -18,7 +18,7 @@ default: &default # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - username: andrey + username: dasha password: password host: localhost port: 5432 diff --git a/config/environments/development.rb b/config/environments/development.rb index 6fd0d2b0e..b262b0191 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -82,9 +82,11 @@ #config.hosts << "youautoplus.ru" #config.hosts << "www.youautoplus.ru" + config.hosts << "localhost:3000" + # Закомментированные домены оставляем для будущего использования - config.hosts << "usecar.ru" - config.hosts << "www.usecar.ru" + #config.hosts << "usecar.ru" + #config.hosts << "www.usecar.ru" # config.hosts << "usecarmax.ru" # config.hosts << "www.usecarmax.ru" diff --git a/config/routes.rb b/config/routes.rb index 85dff1d43..046c6fd21 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,9 @@ Rails.application.routes.draw do - resources :banners do - collection do + resources :banners do + collection do get :banner_all - end - end + end + end resources :engine_capacity_types resources :engine_power_types resources :engine_name_types @@ -15,7 +15,7 @@ resources :contacts resources :about_companies do collection do - patch :update_multiple + patch :update_multiple end end resources :order_statuses @@ -26,14 +26,14 @@ get :car_show get :all_extras post :update_multiple - patch :update_multiple + patch :update_multiple end end resources :categories resources :images do collection do post :update_multiple - patch :update_multiple + patch :update_multiple end end resources :drive_types @@ -49,6 +49,7 @@ resources :cars do collection do get :total_pages + get :special_offer end end resources :banks @@ -77,13 +78,13 @@ post 'exchange' => 'exchanges#create'#Создать обмен - + get 'installment' => 'installments#index'#Рассрочка post 'installment' => 'installments#create'#Создать рассрочку - + get 'buyout' => 'buyouts#index'#Выкуп post 'buyout' => 'buyouts#create'#Создать выкуп - + get 'credit' => 'credits#top_programs'#Топ программ get 'credits' => 'credits#index '#Список программ post 'credit' => 'credits#create'#Создать программу @@ -109,7 +110,7 @@ get 'admin/about' => 'about_companies#index'#О компании get 'admin/banners' => 'cars#add_car'#Добавить автомобиль get 'privacy' => 'cars#add_car'#Политика конфиденциальности - + match "/404", to: "errors#not_found", via: :all @@ -117,6 +118,5 @@ # Обработка всех остальных маршрутов match '*path', to: 'application#frontend', via: :all - + end - \ No newline at end of file diff --git a/db/migrate/20241206160145_add_special_offer_to_car.rb b/db/migrate/20241206160145_add_special_offer_to_car.rb new file mode 100644 index 000000000..dbf4cc7c4 --- /dev/null +++ b/db/migrate/20241206160145_add_special_offer_to_car.rb @@ -0,0 +1,5 @@ +class AddSpecialOfferToCar < ActiveRecord::Migration[7.2] + def change + add_column :cars, :special_offer, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index d688928ae..a3a1fd8ac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_12_05_143511) do +ActiveRecord::Schema[7.2].define(version: 2024_12_06_160145) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -94,6 +94,7 @@ t.bigint "generation_id", null: false t.boolean "online_view_available", default: true t.string "complectation_name" + t.boolean "special_offer", default: false t.index ["body_type_id"], name: "index_cars_on_body_type_id" t.index ["brand_id"], name: "index_cars_on_brand_id" t.index ["color_id"], name: "index_cars_on_color_id" diff --git a/public/.env b/public/.env index 9e9f14623..b377d62cd 100644 --- a/public/.env +++ b/public/.env @@ -1,4 +1,4 @@ #REACT_APP_BASE_URL=https://youautoplus.ru #REACT_APP_BASE_URL=https://youautoplus.ru #REACT_APP_BASE_URL=https://usecarmax.ru -REACT_APP_BASE_URL=https://usecar.ru +REACT_APP_BASE_URL=http://localhost:3000