Skip to content

Commit

Permalink
Merge pull request #20 from fmanimashaun/feature/reservations-controller
Browse files Browse the repository at this point in the history
Feature/reservations controller
  • Loading branch information
fmanimashaun authored Feb 17, 2024
2 parents b9aaaca + eb434d4 commit bd4ab6a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
60 changes: 60 additions & 0 deletions app/controllers/api/v1/reservations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class API::V1::ReservationsController < ApplicationController
before_action :set_reservation, only: %i[destroy show update]

def index
@data = Reservation.where(user_id:current_user.id)
render json: {
status: { code: 200, message: 'Reservations retrieved successfully.' },
reservations: @data.map do |reservation|
ReservationSerializer.new(reservation).serializable_hash[:data][:attributes]
end
}, status: :ok
end

def destroy
@reservation.destroy!
render json: {
status: { code: 200, message: 'Reservation deleted successfully.' }
}, status: :ok
end

def show
render json: {
status: { code: 200, message: 'Reservation fetched successfully' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
}
end

def create
@reservation = Reservation.new(reservation_params)
if @reservation.save!
render json: {
status: { code: 201, message: 'Reservation created successfully' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
}, status: :created
else
render json: @reservation.errors, status: :unprocessable_entity
end
end

def update
if @reservation.update(reservation_params)
render json: {
status: { code: 200, message: 'Reservation created successfully' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
}, status: :ok
else
render json: @reservation.errors, status: :unprocessable_entity
end
end

private

def set_reservation
@reservation = Reservation.find params[:id]
end

def reservation_params
params.require(:reservation).permit(:date, :city_id, :car_id, :user_id)
end
end
2 changes: 1 addition & 1 deletion app/serializers/reservation_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ReservationSerializer
include JSONAPI::Serializer
attributes :date
attributes :id, :date, :city, :car, :user
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace :api do
namespace :v1 do
resources :cities
resources :reservations
resources :cars, except: [:index]
resources :engine_type, except: [:index]
end
Expand Down
6 changes: 5 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# create the admin user
User.find_or_create_by!(username: Rails.application.credentials.admin[:username]) do |user|
users = User.find_or_create_by!(username: Rails.application.credentials.admin[:username]) do |user|
user.password = Rails.application.credentials.admin[:password]
user.password_confirmation = Rails.application.credentials.admin[:password]
user.email = Rails.application.credentials.admin[:email]
Expand Down Expand Up @@ -35,7 +35,11 @@
cities.each { |city_name| City.find_or_create_by!(name: city_name) }

# Create car models
car_names = ['Toyota Camry', 'Honda Accord', 'Ford Mustang', 'Chevrolet Malibu', 'Hyundai Sonata', 'Nissan Altima', 'Volkswagen Passat', 'Subaru Legacy', 'Kia Optima', 'Audi A4', 'BMW 3 Series', 'Mercedes-Benz C-Class', 'Lexus ES', 'Volvo S60', 'Infiniti Q50', 'Acura TLX', 'Cadillac CT5', 'Lincoln MKZ', 'Alfa Romeo Giulia', 'Jaguar XE']
car_names.each_with_index { |car_name, index| Car.find_or_create_by!(name: car_name, description: "Description #{index + 1}") }

Reservation.create!(date: Date.tomorrow, car: Car.first, city: City.first, user: User.first)
Reservation.create!(date: Date.tomorrow, car:Car.last, city: City.last, user: User.last)
# Create engine types
engine_types = [
"4-cylinder",
Expand Down
7 changes: 7 additions & 0 deletions spec/requests/api/v1/reservations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe 'API::V1::Reservations', type: :request do
describe 'GET /index' do
pending "add some examples (or delete) #{__FILE__}"
end
end

0 comments on commit bd4ab6a

Please sign in to comment.