diff --git a/app/controllers/api/v1/aeroplanes_controller.rb b/app/controllers/api/v1/aeroplanes_controller.rb index a0d706b..be4352c 100644 --- a/app/controllers/api/v1/aeroplanes_controller.rb +++ b/app/controllers/api/v1/aeroplanes_controller.rb @@ -5,4 +5,47 @@ def index render json: { aeroplanes: @aeroplanes } end + + # GET /aeroplanes/1 + def show + @aeroplane = Aeroplane.find(params[:id]) + render json: @aeroplane + end + + def create + @aeroplane = Aeroplane.new(aeroplane_params) + + if @aeroplane.save + render json: @aeroplane, status: :created, location: @aeroplane + else + render json: @aeroplane.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT /aeroplanes/1 + def update + if @aeroplane.update(aeroplane_params) + render json: @aeroplane + else + render json: @aeroplane.errors, status: :unprocessable_entity + end + end + + # DELETE /aeroplanes/1 + def destroy + @aeroplane.destroy! + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_aeroplane + @aeroplane = Aeroplane.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def aeroplane_params + params.require(:aeroplane).permit(:name, :model, :image, :description, :number_of_seats, :location, :fee, + :reserved) + end end diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb new file mode 100644 index 0000000..37b980a --- /dev/null +++ b/app/controllers/api/v1/reservations_controller.rb @@ -0,0 +1,53 @@ +class ReservationsController < ApplicationController + before_action :set_reservation, only: %i[show update destroy] + + # GET /reservations + def index + @reservations = Reservation.all + + render json: @reservations + end + + # GET /reservations/1 + def show + render json: @reservation + end + + # POST /reservations + def create + @reservation = Reservation.new(reservation_params) + + if @reservation.save + render json: @reservation, status: :created, location: @reservation + else + render json: @reservation.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT /reservations/1 + def update + if @reservation.update(reservation_params) + render json: @reservation + else + render json: @reservation.errors, status: :unprocessable_entity + end + end + + # DELETE /reservations/1 + def destroy + @reservation.destroy! + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_reservation + @reservation = Reservation.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def reservation_params + params.require(:reservation).permit(:reserved_date, :start_time, :end_time, :total_cost, :start_location, + :destination, :user_id, :aeroplane_id) + end +end