diff --git a/app/controllers/api/osuny/communication/websites/pages_controller.rb b/app/controllers/api/osuny/communication/websites/pages_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..f8ab5c3209ae02d769045f281bf0011cededaf86 --- /dev/null +++ b/app/controllers/api/osuny/communication/websites/pages_controller.rb @@ -0,0 +1,11 @@ +class Api::Osuny::Communication::Websites::PagesController < Api::Osuny::ApplicationController + skip_before_action :verify_authenticity_token, only: :import + before_action :verify_app_token, only: :import + + def import + Importers::Api::Osuny::Communication::Website::Page.new university: current_university, + website: website, + params: params[:page] + render json: :ok + end +end diff --git a/app/controllers/api/osuny/communication/websites/posts_controller.rb b/app/controllers/api/osuny/communication/websites/posts_controller.rb index 350d41a51f8660781d4af3467a17c937637adbde..cc0f67ea7ae47f3917bd2548e6d0e59a9167b0be 100644 --- a/app/controllers/api/osuny/communication/websites/posts_controller.rb +++ b/app/controllers/api/osuny/communication/websites/posts_controller.rb @@ -3,82 +3,10 @@ class Api::Osuny::Communication::Websites::PostsController < Api::Osuny::Applica before_action :verify_app_token, only: :import def import - create_post - import_blocks - import_categories + Importers::Api::Osuny::Communication::Website::Post.new university: current_university, + website: website, + params: params[:post] render json: :ok end - protected - - def create_post - post.language = website.default_language - post.update post_params - post.save - end - - def import_blocks - blocks.each do |b| - migration_identifier = b[:migration_identifier] - template_kind = b[:template_kind] - block = post.blocks - .where( - template_kind: template_kind, - migration_identifier: migration_identifier - ) - .first_or_initialize - block.university = current_university - data = b[:data].to_unsafe_hash - block.data = block.template.data.merge data - block.save - end - end - - def import_categories - categories.each do |c| - data = c.to_unsafe_hash - if data.has_key? 'name' - category = website.categories.where( - university: current_university, - website: website, - name: data['name'], - language: website.default_language - ).first_or_create - end - next if category.nil? - next if category.in?(post.categories) - post.categories << category - end - end - - def post - @post ||= website.posts.where( - university: current_university, - website: website, - migration_identifier: migration_identifier - ) - .first_or_initialize - end - - def blocks - return [] unless params[:post].has_key?(:blocks) - @blocks ||= params[:post][:blocks] - end - - def categories - return [] unless params[:post].has_key?(:categories) - @categories ||= params[:post][:categories] - end - - def migration_identifier - @migration_identifier ||= params[:migration_identifier] - end - - def post_params - params.require(:post) - .permit( - :title, :language, :meta_description, :summary, - ) - end - end diff --git a/app/services/importers/api/osuny/communication/website/base.rb b/app/services/importers/api/osuny/communication/website/base.rb new file mode 100644 index 0000000000000000000000000000000000000000..22524bcfa459708dbfa5d36fe557fece635a8d4f --- /dev/null +++ b/app/services/importers/api/osuny/communication/website/base.rb @@ -0,0 +1,51 @@ +class Importers::Api::Osuny::Communication::Website::Base + + attr_reader :university, :website, :params + + def initialize(university:, website:, params:) + @university = university + @website = website + @params = params.to_unsafe_hash + import + end + + protected + + def import + raise NotImplementedError + end + + def object + raise NotImplementedError + end + + def language + # TODO specific language set in params + website.default_language + end + + def migration_identifier + @migration_identifier ||= params[:migration_identifier] + end + + def blocks + return [] unless params.has_key?(:blocks) + @blocks ||= params[:blocks] + end + + def import_blocks + blocks.each do |b| + migration_identifier = b[:migration_identifier] + template_kind = b[:template_kind] + block = object.blocks + .where( + migration_identifier: migration_identifier, + template_kind: template_kind + ) + .first_or_initialize + block.university = university + block.data = block.template.data.merge b[:data] + block.save + end + end +end \ No newline at end of file diff --git a/app/services/importers/api/osuny/communication/website/page.rb b/app/services/importers/api/osuny/communication/website/page.rb new file mode 100644 index 0000000000000000000000000000000000000000..cf2d66d17774df9430fbc699cf30fbf6cd4eeaf2 --- /dev/null +++ b/app/services/importers/api/osuny/communication/website/page.rb @@ -0,0 +1,9 @@ +class Importers::Api::Osuny::Communication::Website::Page < Importers::Api::Osuny::Communication::Website::Base + + protected + + def import + # TODO + end + +end \ No newline at end of file diff --git a/app/services/importers/api/osuny/communication/website/post.rb b/app/services/importers/api/osuny/communication/website/post.rb new file mode 100644 index 0000000000000000000000000000000000000000..71d924ecb0d73171cc67739f0b8ffbc9f6f62b05 --- /dev/null +++ b/app/services/importers/api/osuny/communication/website/post.rb @@ -0,0 +1,50 @@ +class Importers::Api::Osuny::Communication::Website::Post < Importers::Api::Osuny::Communication::Website::Base + + protected + + def import + object.update post_params + object.save + import_blocks + import_categories + end + + def object + @object ||= website.posts.where( + university: university, + website: website, + migration_identifier: migration_identifier, + language: language + ).first_or_initialize + end + + def import_categories + categories.each do |c| + category = find_or_create_category c + next if category.nil? || category.in?(object.categories) + object.categories << category + end + end + + def find_or_create_category(data) + if data.has_key? 'name' + website.categories.where( + university: university, + website: website, + name: data['name'], + language: language + ).first_or_create + end + end + + def categories + return [] unless params.has_key?(:categories) + @categories ||= params[:categories] + end + + def post_params + ActionController::Parameters.new({ post: params }) + .require(:post) + .permit(:title, :language, :meta_description, :summary) + end +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 8c1950d03bb111156f5d033441356bf04dde7422..61b6b6301cb94156af34801cb245cca59223ed02 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -7,6 +7,7 @@ namespace :api do get 'websites' => 'websites#index' namespace :websites do post ':website_id/posts/import' => 'posts#import' + post ':website_id/pages/import' => 'pages#import' end end namespace :server do