Skip to content

Commit

Permalink
Merge pull request #271 from fga-eps-mds/feature/264-Adding-Locals-Image
Browse files Browse the repository at this point in the history
Feature/264 adding locals image
  • Loading branch information
min-ia committed Nov 6, 2018
2 parents 5fececa + dc2a279 commit 666f555
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 21 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ WORKDIR /indicaAi
COPY ./indicaAi /indicaAi

RUN bundle install
RUN rake db:migrate

6 changes: 6 additions & 0 deletions indicaAi/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem 'i18n', '~> 1.1', '>= 1.1.1'
# Upload files encoded as base64 strings directly to carrierwave
gem 'carrierwave'
gem 'carrierwave-base64', '~> 2.7'
# Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick
gem 'mini_magick', '~> 4.9', '>= 4.9.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use postgresql as the database for Active Record
Expand Down
15 changes: 15 additions & 0 deletions indicaAi/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ GEM
ast (2.4.0)
builder (3.2.3)
byebug (10.0.2)
carrierwave (1.2.3)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
carrierwave-base64 (2.7.0)
carrierwave (>= 0.8.0)
mime-types (~> 3.0)
concurrent-ruby (1.0.5)
coveralls (0.8.22)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -78,6 +85,10 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
method_source (0.9.1)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
mini_magick (4.9.2)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
Expand Down Expand Up @@ -188,11 +199,15 @@ PLATFORMS

DEPENDENCIES
byebug
carrierwave
carrierwave-base64 (~> 2.7)
coveralls (~> 0.8.22)
database_cleaner (~> 1.7)
factory_bot_rails (~> 4.11, >= 4.11.1)
faker (~> 1.9, >= 1.9.1)
i18n (~> 1.1, >= 1.1.1)
listen (>= 3.0.5, < 3.2)
mini_magick (~> 4.9, >= 4.9.2)
pg (>= 0.18, < 2.0)
puma (~> 3.7)
rails (~> 5.1.6)
Expand Down
12 changes: 12 additions & 0 deletions indicaAi/app/controllers/local_images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Local Images Controller
class LocalImagesController < ApplicationController
def receive_image
local = Local.find(params[:id])
return response_success('ERROR', 404) unless local.present?
images = params[:image]
images.each do |image|
LocalImage.create(image: image, local_id: params[:id])
end
response_success('SUCCESS', 'Images saved', 200)
end
end
20 changes: 9 additions & 11 deletions indicaAi/app/controllers/locals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ def search_locals
# POST /local/create
def create
@local = Local.new(local_params)
if @local.save
result = []
# create relationship between category and local
create_relationship_category_and_local(params)
# create tables in bd with opening hours
create_opening_hours
result << @local.as_json(methods: %i[opening_hours categories])
response_success('SUCCESS', 'Saved Local', result, 200)
else
response_error('ERROR', 'Local not saved', 422)
end
return response_error('ERROR', 'Local not saved', 422) unless @local.save
result = []
# create relationship between category and local
create_relationship_category_and_local(params)
# create tables in bd with opening hours
create_opening_hours
result <<
@local.as_json(methods: %i[opening_hours categories local_images])
response_success('SUCCESS', 'Saved Local', result, 200)
end

# GET /local/:id/rating
Expand Down
4 changes: 0 additions & 4 deletions indicaAi/app/models/image_local.rb

This file was deleted.

1 change: 1 addition & 0 deletions indicaAi/app/models/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Local < ApplicationRecord
has_many :opening_hours
has_many :category_and_locals
has_many :categories, through: :category_and_locals
has_many :local_images
validates :name, presence: true, length: { minimum: 2 }
validates :latitude, presence: true, numericality: {
greater_than_or_equal: 0, less_than_or_equal: 100
Expand Down
5 changes: 5 additions & 0 deletions indicaAi/app/models/local_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Model of LocalImage
class LocalImage < ApplicationRecord
belongs_to :local
mount_uploaders :images, ImageUploader
end
6 changes: 6 additions & 0 deletions indicaAi/app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Class of ImageUploader
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

storage :file
end
1 change: 1 addition & 0 deletions indicaAi/config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Load the Rails application.
require_relative 'application'
require 'carrierwave/orm/activerecord'

# Initialize the Rails application.
Rails.application.initialize!
1 change: 1 addition & 0 deletions indicaAi/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
post '/favorites', to: 'favorite_locals#create', :defaults => { :format => 'json' }
post '/locals', to: 'locals#create', :defaults => { :format => 'json' }
post '/local_ratings', to: 'local_ratings#rating_a_place'
post '/local/:id/images/', to: 'local_images#receive_image'

# Patch Requests
put '/favorites/:id', to: 'favorite_locals#update', :defaults => { :format => 'json' }
Expand Down
9 changes: 9 additions & 0 deletions indicaAi/db/migrate/20181027162156_create_local_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateLocalImages < ActiveRecord::Migration[5.1]
def change
create_table :local_images do |t|
t.text :image
t.references :local, foreign_key: true
t.timestamps
end
end
end
13 changes: 11 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: 20181024041749) do
ActiveRecord::Schema.define(version: 20181102154639) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -47,6 +47,14 @@
t.index ["local_id"], name: "index_image_locals_on_local_id"
end

create_table "local_images", force: :cascade do |t|
t.text "image"
t.bigint "local_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["local_id"], name: "index_local_images_on_local_id"
end

create_table "local_ratings", force: :cascade do |t|
t.integer "value"
t.bigint "local_id"
Expand Down Expand Up @@ -79,7 +87,7 @@
end

create_table "user_identifiers", force: :cascade do |t|
t.integer "identifier"
t.string "identifier"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -89,6 +97,7 @@
add_foreign_key "favorite_locals", "locals"
add_foreign_key "favorite_locals", "user_identifiers"
add_foreign_key "image_locals", "locals"
add_foreign_key "local_images", "locals"
add_foreign_key "local_ratings", "locals"
add_foreign_key "local_ratings", "user_identifiers"
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions indicaAi/spec/controllers/local_images_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe LocalImagesController, type: :controller do
let!(:local) { create(:local) }
images = [Faker::ChuckNorris.fact, Faker::ChuckNorris.fact]
let!(:valid_params) do
{
id: local.id,
image: images
}
end
describe 'POST #recive_image' do
it 'Should return sucess, Image Saved' do
post :receive_image, params: valid_params
expect(response).to be_success
end
end

describe 'POST #recive_image' do
it 'Should return have status 404' do
# local_id invalid
valid_params['id'] = 'hi'
post :receive_image, params: valid_params
expect(response).to have_http_status(404)
end
end
end
4 changes: 0 additions & 4 deletions indicaAi/spec/models/image_local_spec.rb

This file was deleted.

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

RSpec.describe LocalImage, type: :model do
describe 'validations' do
it { should belong_to(:local) }
end
end

0 comments on commit 666f555

Please sign in to comment.