Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reservations controller #20

Merged
merged 13 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.all
fmanimashaun marked this conversation as resolved.
Show resolved Hide resolved
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)
fmanimashaun marked this conversation as resolved.
Show resolved Hide resolved
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
end
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 @@ -37,3 +37,7 @@
# 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)
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
Loading