From ad50df8f64a28f2c2261b2d4647fefdeddb878f9 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Wed, 18 Oct 2023 16:45:25 +0200 Subject: [PATCH] wip --- .../websites/pages_controller.rb | 11 +++ .../websites/posts_controller.rb | 78 +------------------ .../api/osuny/communication/website/base.rb | 51 ++++++++++++ .../api/osuny/communication/website/page.rb | 9 +++ .../api/osuny/communication/website/post.rb | 50 ++++++++++++ config/routes/api.rb | 1 + 6 files changed, 125 insertions(+), 75 deletions(-) create mode 100644 app/controllers/api/osuny/communication/websites/pages_controller.rb create mode 100644 app/services/importers/api/osuny/communication/website/base.rb create mode 100644 app/services/importers/api/osuny/communication/website/page.rb create mode 100644 app/services/importers/api/osuny/communication/website/post.rb 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 000000000..f8ab5c320 --- /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 350d41a51..cc0f67ea7 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 000000000..22524bcfa --- /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 000000000..cf2d66d17 --- /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 000000000..71d924ecb --- /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 8c1950d03..61b6b6301 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 -- GitLab