Skip to content

Commit

Permalink
WIP Add special offers
Browse files Browse the repository at this point in the history
  • Loading branch information
daria committed Dec 6, 2024
1 parent 9c6f08b commit 0d030d7
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
c
CarFilterService.new(filter_params, per_page).call.by_cpecial_offer
q
qxit
n
83 changes: 55 additions & 28 deletions app/controllers/cars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
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
per_page = 18
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions app/models/car.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ class Car < ApplicationRecord
belongs_to :brand
belongs_to :model
belongs_to :generation

belongs_to :color
belongs_to :body_type
belongs_to :engine_name_type
belongs_to :engine_power_type
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
Expand All @@ -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
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
26 changes: 13 additions & 13 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +15,7 @@
resources :contacts
resources :about_companies do
collection do
patch :update_multiple
patch :update_multiple
end
end
resources :order_statuses
Expand All @@ -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
Expand All @@ -49,6 +49,7 @@
resources :cars do
collection do
get :total_pages
get :special_offer
end
end
resources :banks
Expand Down Expand Up @@ -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'#Создать программу
Expand All @@ -109,14 +110,13 @@
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
match "*unmatched", to: "errors#not_found", via: :all

# Обработка всех остальных маршрутов
match '*path', to: 'application#frontend', via: :all

end

5 changes: 5 additions & 0 deletions db/migrate/20241206160145_add_special_offer_to_car.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSpecialOfferToCar < ActiveRecord::Migration[7.2]
def change
add_column :cars, :special_offer, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/.env
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0d030d7

Please sign in to comment.