Skip to content

Commit

Permalink
Merge 8e0e040 into ad97842
Browse files Browse the repository at this point in the history
  • Loading branch information
zarbielli committed Oct 20, 2018
2 parents ad97842 + 8e0e040 commit 212c347
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 19 deletions.
22 changes: 22 additions & 0 deletions indicaAi/app/controllers/local_ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Local Ratings Controller
class LocalRatingsController < ApplicationController
# POST /local_rating/:local_name/:user_id/:rate
def rating_a_place
@rating = LocalRating.new(local_rating_params)
if @rating.save
render json: { status: :ok }
else
render json: { status: :error }
end
end

private

def local_rating_params
{
local_id: params[:local_id],
user_identifier_id: params[:user_identifier_id],
value: params[:value]
}
end
end
2 changes: 1 addition & 1 deletion indicaAi/app/models/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ def self.find_by_name(params)

def self.find_local_ratings(local_id)
Local.find(local_id).local_ratings
end
end
end
16 changes: 12 additions & 4 deletions indicaAi/app/models/local_rating.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Description of Local Rating Class
class LocalRating < ApplicationRecord
# Validations
belongs_to :local
validates_associated :local
belongs_to :user_identifier
validates_uniqueness_of :user_identifier, scope: :local_id
validates :value, presence: true
validates :value, numericality: true
validates_numericality_of :value, less_than_or_equal_to: 5
validates_numericality_of :value, greater_than_or_equal_to: 0
validates :value, numericality: {
only_integer: true,
less_than_or_equal_to: 5,
greater_than_or_equal_to: 0
}

def self.find_local_rating(_params)
LocalRating.find(local_id).local_ratings
end
end
3 changes: 3 additions & 0 deletions indicaAi/app/models/user_identifier.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Description of User Identifier Class
class UserIdentifier < ApplicationRecord
has_many :favorite_locals, dependent: :destroy
has_many :local_ratings

validates :identifier, presence: true
validates_numericality_of :identifier
validates_uniqueness_of :identifier

def self.find_favorites(params)
UserIdentifier.find(params).favorite_locals
end
Expand Down
4 changes: 3 additions & 1 deletion indicaAi/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
get '/users', to: 'user_identifiers#index'
get '/local/favorites/:local_id', to: 'locals#list_favorites'
get '/user/favorites/:user_id', to: 'user_identifiers#list_favorites'

post '/local_ratings', to: 'local_ratings#rating_a_place'

# Post Requests
post '/favorite/create', to: 'favorite_locals#create', :defaults => { :format => 'json' }

Expand All @@ -14,4 +15,5 @@

# Deleted Requests
delete '/favorite/delete/:id', to: 'favorite_locals#destroy', :defaults => { :format => 'json' }

end
2 changes: 1 addition & 1 deletion indicaAi/db/migrate/20180917225656_create_local_ratings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :local_ratings do |t|
t.integer :value
t.references :local, foreign_key: true

t.references :user_identifier, foreign_key: true
t.timestamps
end
end
Expand Down
9 changes: 9 additions & 0 deletions indicaAi/db/migrate/20181013163629_create_user_rates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUserRates < ActiveRecord::Migration[5.1]
def change
create_table :user_rates do |t|
t.integer :rating

t.timestamps
end
end
end
4 changes: 4 additions & 0 deletions indicaAi/db/migrate/20181015191722_local_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class LocalRating < ActiveRecord::Migration[5.1]
def change
end
end
4 changes: 4 additions & 0 deletions indicaAi/db/migrate/20181015191738_local.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Local < ActiveRecord::Migration[5.1]
def change
end
end
4 changes: 4 additions & 0 deletions indicaAi/db/migrate/20181015191753_user_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserIdentifier < ActiveRecord::Migration[5.1]
def change
end
end
7 changes: 5 additions & 2 deletions indicaAi/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20181013160532) do
ActiveRecord::Schema.define(version: 20181015191753) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -27,9 +27,11 @@
create_table "local_ratings", force: :cascade do |t|
t.integer "value"
t.bigint "local_id"
t.bigint "user_identifier_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["local_id"], name: "index_local_ratings_on_local_id"
t.index ["user_identifier_id"], name: "index_local_ratings_on_user_identifier_id"
end

create_table "locals", force: :cascade do |t|
Expand All @@ -44,6 +46,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "local_ratings", "locals"
add_foreign_key "local_ratings", "user_identifiers"
end
23 changes: 23 additions & 0 deletions indicaAi/spec/controllers/local_ratings_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rails_helper'

RSpec.describe LocalRatingsController, type: :controller do
let!(:user_test) { create(:user_identifier) }
let!(:local_test) { create(:local) }
let!(:valid_params) do
{
local_id: local_test.id,
user_identifier_id: user_test.id,
value: Faker::Number.between(0, 5)
}
end
describe 'POST rating_a_place' do
it 'should create a LocalRatingsController' do
post :rating_a_place, params: valid_params
expect(response).to be_success
expect(assigns(:rating)).to have_attributes(valid_params)
expect(assigns(:rating)).to be_persisted
expect(assigns(:rating)).to be_a(LocalRating)
expect(assigns(:rating)).not_to eq(nil)
end
end
end
3 changes: 2 additions & 1 deletion indicaAi/spec/factories/local_rating.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :local_rating do
value { Faker::Number.between(0, 5) }
local { create(:local) }
local { FactoryBot.create :local }
user_identifier { FactoryBot.create :user_identifier }
end
end
38 changes: 29 additions & 9 deletions indicaAi/spec/models/local_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@
it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) }

describe 'testing validations' do
context 'ratings validation tests' do
let!(:note) { create(:local_rating) }
it 'should return a integer value' do
assert note.value.class == Integer
end
end
it 'should return a integer though a float was given' do
note2 = LocalRating.new
note2.value = 3.4
assert note2.value == 3
end

it { should belong_to(:local) }
it 'Must exist a user' do
local = Local.create(name: 'Teste')
local.save
rating = LocalRating.new
rating.value = 4
rating.local = local
assert rating.save == false
end

it { should validate_presence_of(:value) }
it 'Must exist a local' do
user = UserIdentifier.create(identifier: 345)
user.save
rating = LocalRating.new
rating.value = 4
rating.user_identifier = user
assert rating.save == false
end

it 'Must exist a value' do
local = Local.create(name: 'Teste')
local.save
user = UserIdentifier.create(identifier: 345)
user.save
rating = LocalRating.new
rating.user_identifier = user
rating.local = local
assert rating.save == false
end
it { should belong_to(:user_identifier) }
it { should belong_to(:local) }
it { should validate_presence_of(:value) }
it { should validate_numericality_of(:value) }

it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) }
end
end
9 changes: 9 additions & 0 deletions indicaAi/spec/models/local_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@
result = Local.find_local_ratings(id)
assert result.first, nota
end

it 'Testing Validade Local Existence - Should return true if local exist' do
local = create(:local)
name = local.name
exist = true

result = Local.validate_local_existence(name)
assert result, exist
end
end

0 comments on commit 212c347

Please sign in to comment.