diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb new file mode 100644 index 0000000..eb03a1f --- /dev/null +++ b/app/controllers/api/v1/reservations_controller.rb @@ -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 diff --git a/app/serializers/reservation_serializer.rb b/app/serializers/reservation_serializer.rb index 1c5330a..b9a2eab 100644 --- a/app/serializers/reservation_serializer.rb +++ b/app/serializers/reservation_serializer.rb @@ -1,4 +1,4 @@ class ReservationSerializer include JSONAPI::Serializer - attributes :date + attributes :id, :date, :city, :car, :user end diff --git a/config/routes.rb b/config/routes.rb index 463d31a..ce27b18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ namespace :api do namespace :v1 do resources :cities + resources :reservations resources :cars, except: [:index] resources :engine_type, except: [:index] end diff --git a/db/seeds.rb b/db/seeds.rb index c8f1c33..4fc5c20 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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] @@ -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", diff --git a/spec/requests/api/v1/reservations_spec.rb b/spec/requests/api/v1/reservations_spec.rb new file mode 100644 index 0000000..37cee33 --- /dev/null +++ b/spec/requests/api/v1/reservations_spec.rb @@ -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