Skip to content

Commit

Permalink
Merge 935eeb1 into a83bd79
Browse files Browse the repository at this point in the history
  • Loading branch information
Ridersk committed Nov 15, 2018
2 parents a83bd79 + 935eeb1 commit 197a6d1
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 28 deletions.
26 changes: 17 additions & 9 deletions indicaAi/app/controllers/favorite_locals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
class FavoriteLocalsController < ApplicationController
# Post /favorite/create
def create
@favorite = FavoriteLocal.new(favorite_params)
if @favorite.save
response_success('SUCCESS', 'Saved favorite', @favorite, 200)
else
response_error('ERROR', 'Favorite not saved', 422)
if (user = UserIdentifier.find_by(identifier: params[:user_identifier]))
@favorite = FavoriteLocal.new(favorite_create_params(params, user))
result = { favorite: @favorite, user: user }
if @favorite.save
response_success('SUCCESS', 'Favorite saved', result, 200)
else response_error('ERROR', 'Favorite not saved', 422)
end
else response_error('ERROR', 'User not found', 422)
end
end

Expand All @@ -16,7 +19,7 @@ def update

if @favorite.nil?
response_error('ERROR', 'Favorite not found', 404)
elsif @favorite.update(favorite_params)
elsif @favorite.update(favorite_update_params)
response_success('SUCCESS', 'Updated favorite', @favorite, 200)
else
response_error('ERROR', 'Favorite not updated', 422)
Expand All @@ -31,14 +34,19 @@ def destroy
response_error('ERROR', 'Favorite not found', 404)
elsif @favorite.destroy
response_success('SUCCESS', 'Deleted favorite', @favorite, 200)
else
response_error('ERROR', 'Favorite not deleted', 422)
end
end

private

def favorite_params
def favorite_create_params(params, user)
{
user_identifier_id: user.id,
local_id: params[:local_id]
}
end

def favorite_update_params
params.permit(:user_identifier_id, :local_id)
end
end
3 changes: 2 additions & 1 deletion indicaAi/app/controllers/locals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def local_params
:latitude,
:longitude,
:address,
:telephone
:telephone,
:publicity
)
end

Expand Down
10 changes: 7 additions & 3 deletions indicaAi/app/controllers/user_identifiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ def show_user

# list all favorites of user
def list_favorites
@favorites = UserIdentifier.find_favorites(params[:id])
@user = UserIdentifier.find_by_id(params[:id])
json_response(user: @user, favorites: @favorites)
if (user = UserIdentifier.find_by(identifier: params[:user_identifier]))
@favorites = UserIdentifier.find_favorites(user.id)
@user = UserIdentifier.find_by_id(user.id)
json_response(user: @user, favorites: @favorites)
else
response_error('ERROR', 'User not found', 422)
end
end
end
1 change: 1 addition & 0 deletions indicaAi/app/models/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Local < ApplicationRecord
has_many :categories, through: :category_and_locals
has_many :local_images
validates :name, presence: true, length: { minimum: 2 }
validates :publicity, inclusion: { in: %w[true false] }
validates :latitude, presence: true, numericality: {
greater_than_or_equal: 0, less_than_or_equal: 100
}
Expand Down
2 changes: 1 addition & 1 deletion indicaAi/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
get '/locals/:id_local/ratings', to: 'locals#show_ratings'
get '/users', to: 'user_identifiers#index'
get '/users/:id', to: 'user_identifiers#show_user'
get '/users/:id/favorites/', to: 'user_identifiers#list_favorites'
get '/users/:user_identifier/favorites/', to: 'user_identifiers#list_favorites'
get '/categories', to: 'categories#index'
get '/categories/:id', to: 'categories#show_category'
get '/categories/:id/locals', to: 'categories#list_locals'
Expand Down
5 changes: 5 additions & 0 deletions indicaAi/db/migrate/20181108180716_add_publicity_tolocals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPublicityTolocals < ActiveRecord::Migration[5.1]
def change
add_column :locals, :publicity, :string, default: 'false'
end
end
3 changes: 2 additions & 1 deletion 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: 20181102154639) do
ActiveRecord::Schema.define(version: 20181108180716) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -74,6 +74,7 @@
t.float "longitude"
t.string "address"
t.string "telephone"
t.string "publicity", default: "false"
end

create_table "opening_hours", force: :cascade do |t|
Expand Down
23 changes: 21 additions & 2 deletions indicaAi/spec/controllers/favorite_locals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
let!(:valid_params) do
{
'local_id' => local_test.id,
'user_identifier_id' => user_test.id
'user_identifier' => user_test.identifier
}
end
describe 'Post create' do
it 'should returns success saved favorite' do
post :create, params: valid_params
expect(response).to be_success
expect(assigns(:favorite)).to have_attributes(valid_params)
expect(assigns(:favorite)).to be_persisted
expect(assigns(:favorite)).to be_a(FavoriteLocal)
expect(assigns(:favorite)).not_to eq(nil)
Expand All @@ -28,6 +27,26 @@
end
end

RSpec.describe FavoriteLocalsController, type: :controller do
let!(:user_test) { create(:user_identifier) }
let!(:local_test) { create(:local) }
let!(:local_test2) { create(:local) }
let!(:valid_params) do
{
'local_id' => local_test.id,
'user_identifier' => user_test.identifier
}
end
describe 'Post error' do
it 'should return error user not found' do
# user_identifier invalid
valid_params['user_identifier'] = nil
post :create, params: valid_params
expect(response).to have_http_status(422)
end
end
end

RSpec.describe FavoriteLocalsController, type: :controller do
let!(:user_test) { create(:user_identifier) }
let!(:local_test) { create(:local) }
Expand Down
12 changes: 4 additions & 8 deletions indicaAi/spec/controllers/user_identifiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@
end

RSpec.describe UserIdentifiersController, type: :controller do
let!(:users_test) { create(:user_identifier) }
let!(:users_test) { create_list(:user_identifier, 10) }
let!(:user) { users_test.first }
let!(:favorites_test) do
create_list(:favorite_local, 10, user_identifier: users_test)
create_list(:favorite_local, 10, user_identifier: user)
end
describe 'GET list_favorites' do
it 'should returns list of favorites to assign @favorites ' do
get :list_favorites, params: { id: users_test.id }
should route(
:get, "/users/#{users_test.id}/favorites"
).to(
action: :list_favorites, id: users_test.id
)
get :list_favorites, params: { user_identifier: user.identifier }
expect(response).to be_success
assert assigns(:favorites) == favorites_test
expect(assigns(:favorites).length).to eq(10)
Expand Down
3 changes: 1 addition & 2 deletions indicaAi/spec/requests/favorite_locals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
let!(:valid_params) do
{
'local_id' => local_test.id,
'user_identifier_id' => user_test.id
'user_identifier' => user_test.identifier
}
end
describe 'POST /favorites' do
it 'should returns success created favorite' do
expect do
post '/favorites', params: valid_params
end.to change(FavoriteLocal, :count).by(+1)
expect(FavoriteLocal.last).to have_attributes(valid_params)
expect(response).to have_http_status(200)
end
it 'should returns error not created favorite' do
Expand Down
2 changes: 1 addition & 1 deletion indicaAi/spec/requests/user_identifiers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
create_list(:favorite_local, 10, user_identifier: user_test)
end
describe 'GET /users/:id/favorites' do
before { get "/users/#{user_test.id}/favorites" }
before { get "/users/#{user_test.identifier}/favorites" }
it 'return favorites by user in json' do
expect(json).not_to be_empty
expect(json['favorites'][0]['user_identifier_id']).to eq(user_test.id)
Expand Down

0 comments on commit 197a6d1

Please sign in to comment.