From 74ed69d52c0c33f3fe11968d0c3c11ca3fbd82a8 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 4 Jun 2024 17:08:31 -0300 Subject: [PATCH] feat: poder actualizar el post si sabemos el uuid --- app/controllers/posts_controller.rb | 2 +- app/services/post_service.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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