From 43ada0203313bba034fdb45a7e8231868deaf868 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Wed, 8 Feb 2017 17:35:47 +0630 Subject: [PATCH] add new api --- app/assets/javascripts/api/products.coffee | 3 +++ app/assets/javascripts/products.coffee | 3 +++ app/assets/stylesheets/api/products.scss | 3 +++ app/assets/stylesheets/products.scss | 3 +++ app/controllers/api/products_controller.rb | 19 +++++++++++++++++++ app/helpers/api/products_helper.rb | 2 ++ app/helpers/products_helper.rb | 2 ++ app/models/product_category.rb | 3 +++ app/models/product_type.rb | 2 ++ app/views/api/products/index.json.jbuilder | 7 +++++++ .../20170208065307_create_product_types.rb | 7 +++++++ ...0170208065316_create_product_categories.rb | 8 ++++++++ .../api/products_controller_test.rb | 7 +++++++ test/controllers/products_controller_test.rb | 7 +++++++ test/fixtures/product_categories.yml | 11 +++++++++++ test/fixtures/product_types.yml | 11 +++++++++++ test/models/product_category_test.rb | 7 +++++++ test/models/product_type_test.rb | 7 +++++++ 18 files changed, 112 insertions(+) create mode 100644 app/assets/javascripts/api/products.coffee create mode 100644 app/assets/javascripts/products.coffee create mode 100644 app/assets/stylesheets/api/products.scss create mode 100644 app/assets/stylesheets/products.scss create mode 100644 app/controllers/api/products_controller.rb create mode 100644 app/helpers/api/products_helper.rb create mode 100644 app/helpers/products_helper.rb create mode 100644 app/models/product_category.rb create mode 100644 app/models/product_type.rb create mode 100644 app/views/api/products/index.json.jbuilder create mode 100644 db/migrate/20170208065307_create_product_types.rb create mode 100644 db/migrate/20170208065316_create_product_categories.rb create mode 100644 test/controllers/api/products_controller_test.rb create mode 100644 test/controllers/products_controller_test.rb create mode 100644 test/fixtures/product_categories.yml create mode 100644 test/fixtures/product_types.yml create mode 100644 test/models/product_category_test.rb create mode 100644 test/models/product_type_test.rb diff --git a/app/assets/javascripts/api/products.coffee b/app/assets/javascripts/api/products.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api/products.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/products.coffee b/app/assets/javascripts/products.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/products.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/products.scss b/app/assets/stylesheets/api/products.scss new file mode 100644 index 0000000..2c5c0fb --- /dev/null +++ b/app/assets/stylesheets/api/products.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/products controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/products.scss b/app/assets/stylesheets/products.scss new file mode 100644 index 0000000..89e2e8d --- /dev/null +++ b/app/assets/stylesheets/products.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the products controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb new file mode 100644 index 0000000..fb50181 --- /dev/null +++ b/app/controllers/api/products_controller.rb @@ -0,0 +1,19 @@ +class Api::ProductsController < ApplicationController + skip_before_filter :verify_authenticity_token + + def index + session_token=params[:session_token] + check_member= Member.authenticate_session_token(session_token) + if !check_member.nil? + categories =ProductCategory.all + arr_category=Array.new + categories.each do |cat| + str={:id => cat.id,:name => cat.name} + arr_category.push(str) + end + @out=true,arr_category + else + @out=false,"Sorry!Unauthorized user!" + end + end +end diff --git a/app/helpers/api/products_helper.rb b/app/helpers/api/products_helper.rb new file mode 100644 index 0000000..fd46f1c --- /dev/null +++ b/app/helpers/api/products_helper.rb @@ -0,0 +1,2 @@ +module Api::ProductsHelper +end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 0000000..ab5c42b --- /dev/null +++ b/app/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/models/product_category.rb b/app/models/product_category.rb new file mode 100644 index 0000000..a5cfee2 --- /dev/null +++ b/app/models/product_category.rb @@ -0,0 +1,3 @@ +class ProductCategory < ApplicationRecord + +end diff --git a/app/models/product_type.rb b/app/models/product_type.rb new file mode 100644 index 0000000..685876d --- /dev/null +++ b/app/models/product_type.rb @@ -0,0 +1,2 @@ +class ProductType < ApplicationRecord +end diff --git a/app/views/api/products/index.json.jbuilder b/app/views/api/products/index.json.jbuilder new file mode 100644 index 0000000..b5299b2 --- /dev/null +++ b/app/views/api/products/index.json.jbuilder @@ -0,0 +1,7 @@ +if @out[0] == true + json.set! :status, @out[0] + json.set! :data, @out[1] +else + json.set! :status, @out[0] + json.set! :message,@out[1] +end \ No newline at end of file diff --git a/db/migrate/20170208065307_create_product_types.rb b/db/migrate/20170208065307_create_product_types.rb new file mode 100644 index 0000000..a19cae8 --- /dev/null +++ b/db/migrate/20170208065307_create_product_types.rb @@ -0,0 +1,7 @@ +class CreateProductTypes < ActiveRecord::Migration[5.0] + def change + create_table :product_types do |t| + t.string :name + end + end +end diff --git a/db/migrate/20170208065316_create_product_categories.rb b/db/migrate/20170208065316_create_product_categories.rb new file mode 100644 index 0000000..6455e61 --- /dev/null +++ b/db/migrate/20170208065316_create_product_categories.rb @@ -0,0 +1,8 @@ +class CreateProductCategories < ActiveRecord::Migration[5.0] + def change + create_table :product_categories do |t| + t.string :name + t.references :product_type + end + end +end diff --git a/test/controllers/api/products_controller_test.rb b/test/controllers/api/products_controller_test.rb new file mode 100644 index 0000000..a760017 --- /dev/null +++ b/test/controllers/api/products_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Api::ProductsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb new file mode 100644 index 0000000..bccd91d --- /dev/null +++ b/test/controllers/products_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/product_categories.yml b/test/fixtures/product_categories.yml new file mode 100644 index 0000000..80aed36 --- /dev/null +++ b/test/fixtures/product_categories.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/product_types.yml b/test/fixtures/product_types.yml new file mode 100644 index 0000000..80aed36 --- /dev/null +++ b/test/fixtures/product_types.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/product_category_test.rb b/test/models/product_category_test.rb new file mode 100644 index 0000000..6f788eb --- /dev/null +++ b/test/models/product_category_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductCategoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/product_type_test.rb b/test/models/product_type_test.rb new file mode 100644 index 0000000..c48b8f1 --- /dev/null +++ b/test/models/product_type_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProductTypeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end