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

F/models controllers #17

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
.env
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ruby '3.2.2'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', '~> 7.1.1'
gem 'rspec-rails'

# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
Expand Down Expand Up @@ -38,6 +39,7 @@ gem 'bootsnap', require: false
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri windows]
gem 'dotenv-rails'
end

group :development do
Expand Down
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ GEM
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
diff-lcs (1.5.0)
dotenv (2.8.1)
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
drb (2.1.1)
ruby2_keywords
erubi (1.12.0)
Expand Down Expand Up @@ -175,6 +180,23 @@ GEM
psych (>= 4.0.0)
reline (0.3.9)
io-console (~> 0.5)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.0.3)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
ruby2_keywords (0.0.5)
stringio (3.0.8)
thor (1.3.0)
Expand All @@ -197,9 +219,11 @@ PLATFORMS
DEPENDENCIES
bootsnap
debug
dotenv-rails
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.1)
rspec-rails
tzinfo-data

RUBY VERSION
Expand Down
53 changes: 53 additions & 0 deletions app/controllers/aeroplanes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class AeroplanesController < ApplicationController
before_action :set_aeroplane, only: %i[show update destroy]

# GET /aeroplanes
def index
@aeroplanes = Aeroplane.all

render json: @aeroplanes
end

# GET /aeroplanes/1
def show
render json: @aeroplane
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
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
53 changes: 53 additions & 0 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[show update destroy]

# GET /users
def index
@users = User.all

render json: @users
end

# GET /users/1
def show
render json: @user
end

# POST /users
def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /users/1
def destroy
@user.destroy!
end

private

# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name)
end
end
2 changes: 2 additions & 0 deletions app/models/aeroplane.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Aeroplane < ApplicationRecord
end
4 changes: 4 additions & 0 deletions app/models/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Reservation < ApplicationRecord
belongs_to :user
belongs_to :aeroplane
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ApplicationRecord
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rails.application.routes.draw do
resources :reservations
resources :aeroplanes
resources :users
# 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.
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20231101100616_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[7.1]
def change
create_table :users do |t|
t.string :name

t.timestamps
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20231101101057_create_aeroplanes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateAeroplanes < ActiveRecord::Migration[7.1]
def change
create_table :aeroplanes do |t|
t.string :name
t.string :model
t.string :image
t.string :description
t.integer :number_of_seats
t.string :location
t.decimal :fee
t.boolean :reserved

t.timestamps
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20231101102104_create_reservations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateReservations < ActiveRecord::Migration[7.1]
def change
create_table :reservations do |t|
t.date :reserved_date
t.date :start_time
t.date :end_time
t.decimal :total_cost
t.string :start_location
t.string :destination
t.references :user, null: false, foreign_key: true
t.references :aeroplane, null: false, foreign_key: true

t.timestamps
end
end
end
53 changes: 53 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/aeroplane_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Aeroplane, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/reservation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Reservation, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading
Loading