From e73f61adb22db816eafb083e4445b8636bd556ee Mon Sep 17 00:00:00 2001 From: Prakash Poudel Date: Fri, 4 Dec 2020 04:15:22 +0545 Subject: [PATCH 1/3] Add v2 api - Add slide_locations_controller - Add slide_location_serializer - Add slide_serializer - Add slide_location routes api v2 --- .../storefront/slide_locations_controller.rb | 27 +++++++++++++++++++ .../storefront/slide_location_serializer.rb | 13 +++++++++ .../spree/v2/storefront/slide_serializer.rb | 14 ++++++++++ config/routes.rb | 8 ++++++ 4 files changed, 62 insertions(+) create mode 100644 app/controllers/spree/api/v2/storefront/slide_locations_controller.rb create mode 100644 app/serializers/spree/v2/storefront/slide_location_serializer.rb create mode 100644 app/serializers/spree/v2/storefront/slide_serializer.rb diff --git a/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb b/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb new file mode 100644 index 0000000..e2530e1 --- /dev/null +++ b/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb @@ -0,0 +1,27 @@ +module Spree + module Api + module V2 + module Storefront + class SlideLocationsController < ::Spree::Api::V2::BaseController + def show + render_serialized_payload { serialize_resource(resource) } + end + + private + + def scope + Spree::SlideLocation + end + + def resource + scope.find(id: params[:id]) + end + + def resource_serializer + Spree::V2::Storefront::SlideLocationSerializer + end + end + end + end + end +end diff --git a/app/serializers/spree/v2/storefront/slide_location_serializer.rb b/app/serializers/spree/v2/storefront/slide_location_serializer.rb new file mode 100644 index 0000000..bbf9ae8 --- /dev/null +++ b/app/serializers/spree/v2/storefront/slide_location_serializer.rb @@ -0,0 +1,13 @@ +module Spree + module V2 + module Storefront + class SlideLocationSerializer < BaseSerializer + set_type :slide_location + + attributes :name + + has_many :slides + end + end + end +end diff --git a/app/serializers/spree/v2/storefront/slide_serializer.rb b/app/serializers/spree/v2/storefront/slide_serializer.rb new file mode 100644 index 0000000..f88e1e2 --- /dev/null +++ b/app/serializers/spree/v2/storefront/slide_serializer.rb @@ -0,0 +1,14 @@ +module Spree + module V2 + module Storefront + class SlideSerializer < BaseSerializer + set_type :slide + + attributes :name, :slide_name,:slide_link, :slide_image, :preview, :thumbnail + + # has_one :image + + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index f9ea538..d6fd110 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,4 +8,12 @@ resources :slide_locations end + + namespace :api, defaults: { format: 'json' } do + namespace :v2 do + namespace :storefront do + resources :slide_locations, only: [:show] + end + end + end end From a670f63bc5997c9c9255ff382299f7a4f3758acf Mon Sep 17 00:00:00 2001 From: Prakash Poudel Date: Tue, 8 Dec 2020 16:33:51 +0545 Subject: [PATCH 2/3] Fix finder --- .../spree/api/v2/storefront/slide_locations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb b/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb index e2530e1..4c72373 100644 --- a/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb +++ b/app/controllers/spree/api/v2/storefront/slide_locations_controller.rb @@ -14,7 +14,7 @@ def scope end def resource - scope.find(id: params[:id]) + scope.find(params[:id]) end def resource_serializer From 95599d1ec051b9a3683e3d86e995269cce0debd7 Mon Sep 17 00:00:00 2001 From: Prakash Poudel Date: Wed, 9 Dec 2020 06:49:18 +0545 Subject: [PATCH 3/3] Image url for api --- app/models/spree/slide.rb | 7 +++++++ app/serializers/spree/v2/storefront/slide_serializer.rb | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/spree/slide.rb b/app/models/spree/slide.rb index 92bd691..1b355cd 100644 --- a/app/models/spree/slide.rb +++ b/app/models/spree/slide.rb @@ -1,4 +1,6 @@ class Spree::Slide < ActiveRecord::Base + include Rails.application.routes.url_helpers + has_and_belongs_to_many :slide_locations, class_name: 'Spree::SlideLocation', join_table: 'spree_slide_slide_locations' @@ -47,6 +49,11 @@ def thumbnail image_form(:thumbnail) end + def slide_image_url + # For Api + rails_blob_path(image, only_path: true) + end + private def image_form(form) diff --git a/app/serializers/spree/v2/storefront/slide_serializer.rb b/app/serializers/spree/v2/storefront/slide_serializer.rb index f88e1e2..c9a49de 100644 --- a/app/serializers/spree/v2/storefront/slide_serializer.rb +++ b/app/serializers/spree/v2/storefront/slide_serializer.rb @@ -4,9 +4,7 @@ module Storefront class SlideSerializer < BaseSerializer set_type :slide - attributes :name, :slide_name,:slide_link, :slide_image, :preview, :thumbnail - - # has_one :image + attributes :name, :body, :slide_name, :slide_link, :slide_image_url end end