removed redundant ribica name from subfolders

This commit is contained in:
Edin Dazdarevic
2015-01-22 22:20:34 +01:00
parent 13296062f4
commit 335e954bce
150 changed files with 18 additions and 18 deletions

BIN
front-api/.DS_Store vendored Normal file

Binary file not shown.

38
front-api/.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/
## Specific to RubyMotion:
.dat*
.repl_history
build/
## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/
## Environment normalisation:
/.bundle/
/lib/bundler/man/
# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
# everyone has it's on configuration :)
config.rb

22
front-api/Gemfile Normal file
View File

@@ -0,0 +1,22 @@
source 'https://rubygems.org'
platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
gem 'jruby-openssl'
end
platform :ruby do
gem 'pg'
gem "activerecord"
end
gem "sinatra"
gem "sinatra-activerecord"
gem "json"
gem 'puma'

63
front-api/Gemfile.lock Normal file
View File

@@ -0,0 +1,63 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
activerecord (4.2.0)
activemodel (= 4.2.0)
activesupport (= 4.2.0)
arel (~> 6.0)
activerecord-jdbc-adapter (1.3.13)
activerecord (>= 2.2)
activerecord-jdbcpostgresql-adapter (1.3.13)
activerecord-jdbc-adapter (~> 1.3.13)
jdbc-postgres (>= 9.1)
activesupport (4.2.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.0)
builder (3.2.2)
i18n (0.7.0)
jdbc-postgres (9.3.1102)
jruby-openssl (0.9.6-java)
json (1.8.1)
json (1.8.1-java)
minitest (5.5.0)
pg (0.17.1)
puma (2.10.2)
rack (>= 1.1, < 2.0)
puma (2.10.2-java)
rack (>= 1.1, < 2.0)
rack (1.6.0)
rack-protection (1.5.3)
rack
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sinatra-activerecord (2.0.3)
activerecord (>= 3.2)
sinatra (~> 1.0)
thread_safe (0.3.4)
thread_safe (0.3.4-java)
tilt (1.4.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
java
ruby
DEPENDENCIES
activerecord
activerecord-jdbcpostgresql-adapter
jruby-openssl
json
pg
puma
sinatra
sinatra-activerecord

34
front-api/README.md Normal file
View File

@@ -0,0 +1,34 @@
# Ribica front office API
## Getting started:
1. install postgresql and libraries (for native 'pg' gem to be able to compile)
2. run `bundle install`
3. create postgresql user protected with password
4. create postgresql database
5. copy config.rb.example to config.rb and change the information inside of it
6. run `rake db:schema:load`
7. run `gem install rerun`
## Running dev server (with autoreloading)
1. run `rerun ruby app.rb`
## Generating migrations
Sinatra has a different syntax than rails to generate migration:
`rake db:create_migration NAME=create_categories`
## Handling shared models
Some of models will necesarrily be shared between front and back office.
Migrations for those models *must be in front office* and
back office will just have a model file with custom code if needed.
When generating active scaffold - migration is generated by default so it
needs to be removed before using it.

21
front-api/app.rb Normal file
View File

@@ -0,0 +1,21 @@
require 'sinatra'
require 'sinatra/activerecord'
require './config'
require 'json'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
set :bind, '0.0.0.0'
before do
content_type :json
# TODO: before running to production change this so that only specific
# domain is allowed
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
end
Dir[File.dirname(__FILE__) + '/controllers/*.rb'].each {|file| require file }

View File

@@ -0,0 +1,12 @@
db = URI.parse('postgres://postgres:testni_hamo2@localhost/ribica')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)

2
front-api/config.ru Normal file
View File

@@ -0,0 +1,2 @@
require './app'
run Sinatra::Application

View File

@@ -0,0 +1,10 @@
get '/category' do
Category.order(:name).all.to_json(include: :sub_categories)
end
get '/category/:id' do
id = params[:id].to_i
Category.find(id).to_json(include: :sub_categories)
end

View File

@@ -0,0 +1,5 @@
# converts list of parameters to array of integers
def mass_to_i(*id_strings)
id_strings.map(&:to_i)
end

View File

@@ -0,0 +1,67 @@
def prepare_items_for_mass_display(items)
items.to_json(
:except => [:created_at, :current_input_price, :stock, :on_display],
:include => [
:unit ,
:multi_media_descriptions ,
:sub_category
])
end
def offset_and_limit_invalid?(offset, limit)
offset < 0 or limit <= 0 or limit > 100
end
# gets single item for detailed display (like comments etc. ) # TODO: change when comments are added
get '/item/:id' do |id_s|
item = Item.find(id_s.to_i)
prepare_items_for_mass_display(item)
end
# gets items regardless of classification ( useful for frontpage )
get '/item/offset/:offset/limit/:limit' do |offset_s, limit_s|
offset, limit = mass_to_i(offset_s, limit_s)
return [].to_json if offset_and_limit_invalid?(offset,limit)
items = Item.best_selling(offset,limit)
prepare_items_for_mass_display(items)
end
# gets items in section ( useful for page showing single section )
get '/item/section/:section_id/offset/:offset/limit/:limit' do |section_id_s, offset_s, limit_s|
section_id, offset, limit = mass_to_i(section_id_s, offset_s, limit_s)
input_invalid = offset_and_limit_invalid?(offset,limit) or section_id <= 0
return [].to_json if input_invalid
items = Item.best_selling_in_section(section_id, offset, limit)
prepare_items_for_mass_display(items)
end
# gets items in category ( useful for page showing single category )
get '/item/category/:category_id/offset/:offset/limit/:limit' do |category_id_s, offset_s, limit_s|
category_id, offset, limit = mass_to_i(category_id_s, offset_s, limit_s)
input_invalid = offset_and_limit_invalid?(offset,limit) or category_id <= 0
return [].to_json if input_invalid
items = Item.best_selling_in_category(category_id, offset,limit)
prepare_items_for_mass_display(items)
end
# gets items in sub category ( useful for page showing single sub_category )
get '/item/sub_category/:sub_category_id/offset/:offset/limit/:limit' do |sub_category_id_s, offset_s, limit_s|
sub_category_id, offset, limit = mass_to_i(sub_category_id_s, offset_s, limit_s)
input_invalid = offset_and_limit_invalid?(offset,limit) or sub_category_id <= 0
return [].to_json if input_invalid
items = Item.best_selling_in_sub_category(sub_category_id, offset, limit)
prepare_items_for_mass_display(items)
end

View File

@@ -0,0 +1,10 @@
get '/section' do
Section.order(:name).all.to_json(:include =>
[:categories => { :include => :sub_categories }])
end
get '/section/:id' do
Section.find(params[:id].to_i).to_json(:include => [
:categories => { :include => :sub_categories } ])
end

View File

@@ -0,0 +1,7 @@
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
end
end
end

View File

@@ -0,0 +1,8 @@
class CreateSubCategories < ActiveRecord::Migration
def change
create_table :sub_categories do |t|
t.string :name
t.integer :category_id
end
end
end

View File

@@ -0,0 +1,7 @@
class CreateSections < ActiveRecord::Migration
def change
create_table :sections do |t|
t.string :name
end
end
end

View File

@@ -0,0 +1,9 @@
class AddSectionidToCategories < ActiveRecord::Migration
def self.up
add_column :categories, :section_id, :integer
end
def self.down
remove_column :categories, :section_id
end
end

View File

@@ -0,0 +1,8 @@
class CreateUnits < ActiveRecord::Migration
def change
create_table :units do |t|
t.string :name
t.string :short_name, limit: 4
end
end
end

View File

@@ -0,0 +1,18 @@
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.string :code, limit: 10
t.decimal :current_input_price, precision: 5, scale: 2
t.decimal :list_price, precision: 5, scale: 2
t.integer :unit_id
t.decimal :units_in_pack, precision: 5, scale: 3
t.text :description
t.integer :sub_category_id
t.integer :stock
t.boolean :on_display
t.timestamps null: false
end
end
end

View File

@@ -0,0 +1,7 @@
class CreateMediaTypes < ActiveRecord::Migration
def change
create_table :media_types do |t|
t.string :name
end
end
end

View File

@@ -0,0 +1,11 @@
class CreateMultiMediaDescriptions < ActiveRecord::Migration
def change
create_table :multi_media_descriptions do |t|
t.string :url
t.integer :item_id
t.integer :media_type_id
t.timestamps null: false
end
end
end

View File

@@ -0,0 +1,5 @@
class AddTagsToItem < ActiveRecord::Migration
def change
add_column :items, :tags, :string
end
end

68
front-api/db/schema.rb Normal file
View File

@@ -0,0 +1,68 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150118082022) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "categories", force: :cascade do |t|
t.string "name"
t.integer "section_id"
end
create_table "items", force: :cascade do |t|
t.string "name"
t.string "code", limit: 10
t.decimal "current_input_price", precision: 5, scale: 2
t.decimal "list_price", precision: 5, scale: 2
t.integer "unit_id"
t.decimal "units_in_pack", precision: 5, scale: 3
t.text "description"
t.integer "sub_category_id"
t.integer "stock"
t.boolean "on_display"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tags"
end
create_table "media_types", force: :cascade do |t|
t.string "name"
end
create_table "multi_media_descriptions", force: :cascade do |t|
t.string "url"
t.integer "item_id"
t.integer "media_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sections", force: :cascade do |t|
t.string "name"
end
create_table "sub_categories", force: :cascade do |t|
t.string "name"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "units", force: :cascade do |t|
t.string "name"
t.string "short_name", limit: 4
end
end

View File

@@ -0,0 +1,4 @@
class Category < ActiveRecord::Base
has_many :sub_categories
belongs_to :section
end

12
front-api/models/item.rb Normal file
View File

@@ -0,0 +1,12 @@
class Item < ActiveRecord::Base
belongs_to :unit
has_many :multi_media_descriptions
belongs_to :sub_category
# TODO: change "best selling" algorithm when get some data - currently it's only sorted by earnings
scope :best_selling, -> (offset, limit) { where(:on_display => true).order("(list_price - current_input_price) DESC").limit(limit).offset(offset) }
scope :best_selling_in_sub_category, -> (sub_category_id, offset, limit) { best_selling(offset, limit).where(sub_category_id: sub_category_id) }
scope :best_selling_in_category, -> (category_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [:category] ).where(["category_id = ?", category_id]) }
scope :best_selling_in_section, -> (section_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [category: [ :section ]] ).where( ["section_id = ?", section_id] ) }
end

View File

@@ -0,0 +1,3 @@
class MediaType < ActiveRecord::Base
has_many :multi_media_descriptions
end

View File

@@ -0,0 +1,5 @@
class MultiMediaDescription < ActiveRecord::Base
belongs_to :item
belongs_to :media_type
end

View File

@@ -0,0 +1,3 @@
class Section < ActiveRecord::Base
has_many :categories
end

View File

@@ -0,0 +1,5 @@
class SubCategory < ActiveRecord::Base
belongs_to :category
end

2
front-api/models/unit.rb Normal file
View File

@@ -0,0 +1,2 @@
class Unit < ActiveRecord::Base
end

3
front-api/rakefile.rb Normal file
View File

@@ -0,0 +1,3 @@
require "./app"
require "./config"
require "sinatra/activerecord/rake"