Class: Tracker::Application::CreateStoryStory

Inherits:
Object
  • Object
show all
Includes:
Interactor
Defined in:
lib/tracker/application/create_story_story.rb

Instance Method Summary collapse

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tracker/application/create_story_story.rb', line 8

def perform
  child_story_id = context[:child_story_id]
  parent_story_id = context[:parent_story_id]

  story_story_attributes = {
    parent_story_id: parent_story_id,
    child_story_id: child_story_id
  }

  recursive_sql = Tracker::PG.recursive_parent_story_stories_sql(parent_story_id)

  invalid_story_stories = Tracker.pg[:story_stories].where("id in (#{recursive_sql})").where(
    parent_story_id: child_story_id
  )

  if Tracker.pg[:story_stories].where(story_story_attributes).any?
    fail!(message: :already_exists)
  else
    invalid_story_stories.delete
    story_story_id = Tracker.pg[:story_stories].insert(story_story_attributes)
    context[:story_story_id] = story_story_id
  end
end