From f55571c654d37cae5773822f2f466268ad0ccee8 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Fri, 3 Nov 2023 14:51:49 +0200 Subject: [PATCH 1/2] Updated controller and models:params and validations and routes. --- app/controllers/aeroplanes_controller.rb | 19 +++++++++++-------- app/models/aeroplane.rb | 6 +++++- app/serializers/user_serializer.rb | 2 +- config/routes.rb | 7 ++++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/controllers/aeroplanes_controller.rb b/app/controllers/aeroplanes_controller.rb index ca5bfe2..05800ed 100644 --- a/app/controllers/aeroplanes_controller.rb +++ b/app/controllers/aeroplanes_controller.rb @@ -1,5 +1,7 @@ class AeroplanesController < ApplicationController before_action :set_aeroplane, only: %i[show update destroy] + before_action :authenticate_user! + # GET /aeroplanes def index @@ -14,15 +16,16 @@ def show end # POST /aeroplanes - 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 +def create + @aeroplane = Aeroplane.new(aeroplane_params) + # @aeroplane.user = User.id + + if @aeroplane.save + render json: @aeroplane, status: :created, location: @aeroplane + else + render json: { errors: @aeroplane.errors.full_messages }, status: :unprocessable_entity end +end # PATCH/PUT /aeroplanes/1 def update diff --git a/app/models/aeroplane.rb b/app/models/aeroplane.rb index 235a06b..4b50035 100644 --- a/app/models/aeroplane.rb +++ b/app/models/aeroplane.rb @@ -2,6 +2,10 @@ class Aeroplane < ApplicationRecord has_many :reservations, dependent: :destroy validates :name, presence: true + validates :model, presence: true validates :image, presence: true - validates :fee, numericality: { greater_than_or_equal_to: 0.0 } + validates :description, presence: true + validates :number_of_seats, presence: true + validates :location, presence: true + validates :fee, numericality: { greater_than_or_equal_to: 0 } end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 0e3a648..4ffa29a 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -1,6 +1,6 @@ class UserSerializer include JSONAPI::Serializer - attributes :id, :email, :created_at, :name + attributes :id, :email, :created_at, :name, :role attribute :created_date do |user| user.created_at&.strftime('%m/%d/%Y') diff --git a/config/routes.rb b/config/routes.rb index 8844af3..c749177 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,15 +8,16 @@ sessions: 'users/sessions', registrations: 'users/registrations' } - + namespace :api do namespace :v1 do resources :users do - resources :reservations - resources :aeroplanes + resources :reservations + resources :aeroplanes end end end + resources :aeroplanes # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. From 631b8fecf9ba5acd8cc8b22ba1f87d9a3f9fadf7 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Fri, 3 Nov 2023 14:53:42 +0200 Subject: [PATCH 2/2] Linter fixes --- app/controllers/aeroplanes_controller.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/controllers/aeroplanes_controller.rb b/app/controllers/aeroplanes_controller.rb index 05800ed..03bd155 100644 --- a/app/controllers/aeroplanes_controller.rb +++ b/app/controllers/aeroplanes_controller.rb @@ -1,7 +1,6 @@ class AeroplanesController < ApplicationController before_action :set_aeroplane, only: %i[show update destroy] - before_action :authenticate_user! - + before_action :authenticate_user! # GET /aeroplanes def index @@ -16,16 +15,16 @@ def show end # POST /aeroplanes -def create - @aeroplane = Aeroplane.new(aeroplane_params) - # @aeroplane.user = User.id - - if @aeroplane.save - render json: @aeroplane, status: :created, location: @aeroplane - else - render json: { errors: @aeroplane.errors.full_messages }, status: :unprocessable_entity + def create + @aeroplane = Aeroplane.new(aeroplane_params) + # @aeroplane.user = User.id + + if @aeroplane.save + render json: @aeroplane, status: :created, location: @aeroplane + else + render json: { errors: @aeroplane.errors.full_messages }, status: :unprocessable_entity + end end -end # PATCH/PUT /aeroplanes/1 def update