Skip to content

Commit

Permalink
Feat: Added properties controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dave98 committed Jul 13, 2022
1 parent 4bbe9a7 commit e605768
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem "rack-cors"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ GEM
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.4)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-test (2.0.2)
rack (>= 1.3)
rails (7.0.3)
Expand Down Expand Up @@ -212,6 +214,7 @@ DEPENDENCIES
jbuilder
pg (~> 1.1)
puma (~> 5.0)
rack-cors
rails (~> 7.0.2, >= 7.0.2.4)
sprockets-rails
stimulus-rails
Expand Down
64 changes: 64 additions & 0 deletions app/controllers/properties_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class PropertiesController <ApplicationController
skip_before_action :require_login, only: :index
before_action :set_property, only: %i[ show update destroy]

def owned
render json: current_user.properties, status: :ok
end

def index
if current_user
render json: Property.where.not(user: current_user), status: :ok
else
render json: Property.all, status: :ok
end
end

def show
render json: @property, status: :ok
end

def create
property = Property.new(property_params)
property.user = current_user
if property.save
render json: property, status: :created
else
render json: property.errors, status: :unprocessable_entity
end
end

def update
if @property.update(property_params)
render json: @property
else
render json: @property.errors, status: :unprocessable_entity
end
end

def destroy
@property.destroy
end

private

def set_property
@property = Property.find(params[:id])
end

def property_params
params.permit(
:operationType,
:address,
:rentType,
:rentAmount,
:maintenance,
:propertyType,
:bedrooms,
:bathrooms,
:area,
:petsAllowed,
:about
)
end
end
7 changes: 6 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def create
end

def destroy
current_user.invalidate_token
if current_user && current_user.invalidate_token
render json: {}, status: :no_content
else
render json: nil, status: :internal_server_error
end
end

end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create

#------------------
def show
render json: current_user
render json: current_user, status: :ok
end

#------------------
Expand All @@ -36,4 +36,4 @@ def create_user_params
def update_user_params
params.permit(:name, :password)
end
end
end
2 changes: 1 addition & 1 deletion app/models/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class Property < ApplicationRecord
validates :bedrooms, presence: true
validates :bathrooms, presence: true
validates :area, presence: true
validates :petsAllowed, inclusion: { in: [true, false]}
validates :petsAllowed, inclusion: { in: [true, false], message: "should be true or false"}
validates :about, presence: true, length: { in: 20..500 }, allow_blank: false
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class User < ApplicationRecord

has_many :properties, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :liked_properties, through: :likes, source: :property
has_many :contacts, dependent: :destroy
has_many :contacted_properties, through: :contacts, source: :property

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, presence: :true, allow_blank: false
Expand Down
15 changes: 15 additions & 0 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Be sure to restart your server when you modify this file.
# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
# Read more: https://github.com/cyu/rack-cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "*"

resource "*",
headers: :any,
methods: %i[get post put patch delete options head]
end
end

10 changes: 9 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
Rails.application.routes.draw do
# User
get "/profile", to: "users#show"
patch "/profile", to: "users#update"
post "/signup", to: "users#create"

# Sessions
post "/login", to: "sessions#create"
post "/signup", to: "users#create"
delete "/logout", to: "sessions#destroy"

# Properties"
resource :properties do
get "/index", to: "properties#index"
get "/owned", to: "properties#owned"
end
end
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
many: { users: 100, properties: 300 },
}

current_config = :few
current_config = :many

User.destroy_all
Property.destroy_all
Expand Down Expand Up @@ -43,6 +43,7 @@
bedrooms: rand(1..10),
bathrooms: rand(1..5),
area: rand(1..10000),
petsAllowed: Faker::Boolean.boolean(true_ratio: 0.2),
about: Faker::Hipster.paragraph_by_chars(characters: 300) )
if property.valid?
property.save
Expand Down

0 comments on commit e605768

Please sign in to comment.