diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 282a784a..8b103422 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -117,7 +117,7 @@ class PostsController < ApplicationController service = PostService.new(site: site, usuarie: current_usuarie, params: params) - @post = service.create + @post = service.create_or_update if post.persisted? site.touch diff --git a/app/services/post_service.rb b/app/services/post_service.rb index 3045bd9c..bb49c5bd 100644 --- a/app/services/post_service.rb +++ b/app/services/post_service.rb @@ -3,6 +3,26 @@ # Este servicio se encarga de crear artículos y guardarlos en git, # asignándoselos a une usuarie PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do + + # Si estamos pasando el UUID con los parámetros, el post quizás + # existe. + # + # @return [Post] + def create_or_update + uuid = params.require(base).permit(:uuid).values.first + + binding.pry + + if uuid.blank? + create + elsif (indexed_post = site.indexed_posts.find_by(post_id: uuid)).present? + self.post = indexed_post.post + update + else + create + end + end + # Crea un artículo nuevo # # @return Post