Skip to content

Commit

Permalink
Merge branch 'dev' into feature/reservations-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
fmanimashaun authored Feb 17, 2024
2 parents 2b03605 + b9aaaca commit eb434d4
Show file tree
Hide file tree
Showing 20 changed files with 834 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
gem install --no-document rubocop -v '>= 1.0, < 2.0' # https://docs.rubocop.org/en/stable/installation/
[ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ror/.rubocop.yml
- name: Rubocop Report
run: rubocop --color
run: rubocop --color -A
nodechecker:
name: node_modules checker
runs-on: ubuntu-22.04
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ group :development do
# gem "spring"
end

group :test do
gem 'shoulda-matchers'
end

gem 'devise'
gem 'devise-jwt'
gem 'jsonapi-serializer'
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ GEM
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
shoulda-matchers (6.1.0)
activesupport (>= 5.2.0)
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
Expand Down Expand Up @@ -320,6 +322,7 @@ DEPENDENCIES
rswag-specs
rswag-ui
rubocop
shoulda-matchers
tzinfo-data

RUBY VERSION
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/api/v1/cars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class API::V1::CarsController < ApplicationController
before_action :set_car, only: %i[show update destroy]

def show
render json: {
status: {
code: 200,
message: 'Car fetched successfully'
},
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes]
},
status: :ok
end

def create
@car = Car.new(car_params)

if @car.save
render json: {
status: {
code: 200,
message: 'Car successfully created'
},
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes]
},
status: :created
else
render json: @car.errors, status: :unprocessable_entity
end
end

def update
if @car.update(car_params)
render json: {
status: {
code: 200,
message: 'Car successfully updated'
},
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes]
},
status: :ok
else
render json: @car.errors, status: :unprocessable_entity
end
end

def destroy
@car.destroy
render json: {
status: {
code: 200,
message: 'Car successfully deleted'
}
},
status: :ok
end

private

def set_car
@car = Car.includes(:car_detail).find(params[:id])
end

def car_params
params.require(:car).permit(
:name,
:description,
car_detail_attributes: %i[
engine_type_id
horsepower
torque
fuel_economy
seating_capacity
cargo_space
infotainment_system
safety_rating
tech_features
special_features
]
)
end
end
9 changes: 9 additions & 0 deletions app/serializers/car_with_details_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CarWithDetailsSerializer
include JSONAPI::Serializer

attributes :id, :name, :description

attribute :car_detail do |car|
CarDetailSerializer.new(car.car_detail).serializable_hash[:data][:attributes]
end
end
2 changes: 2 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: "BrunoyKino2019@"
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace :v1 do
resources :cities
resources :reservations
resources :cars, except: [:index]
resources :engine_type, except: [:index]
end
end

mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
get '/current_user', to: 'users/current_user#index'
Expand Down
Loading

0 comments on commit eb434d4

Please sign in to comment.