Class: Podrb::Commands::Episodes::Runner

Inherits:
BaseRunner
  • Object
show all
Defined in:
lib/podrb/commands/episodes/runner.rb

Instance Method Summary collapse

Methods inherited from BaseRunner

call

Instance Method Details

#call(podcast_id, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/podrb/commands/episodes/runner.rb', line 8

def call(podcast_id, options = {})
  parsed_options = parse_options(options)

  columns = parsed_options["fields"] || ALL_COLUMNS
  sql_code = <<~SQL
    select #{columns.join(", ")}
    from episodes
    where podcast_id = #{podcast_id}
  SQL

  unless parsed_options["all"]
    sql_code << "and archived_at is null\n"
  end

  order_by = parsed_options["order_by"] || "id"
  sql_code << "order by #{order_by};\n"

  db = Infrastructure::Storage::SQL.new(db: podrb_db_dir)
  records = db.query(sql_code)

  build_success_response(
    details: records.empty? ? :not_found : :records_found,
    metadata: {records: records, columns: columns}
  )
rescue Infrastructure::Storage::Exceptions::WrongSyntax => exc
  cause = exc.message
  if cause.include?("no such column")
    invalid_column = cause.delete_prefix("no such column: ")
    build_failure_response(
      details: :invalid_column,
      metadata: {invalid_column: invalid_column}
    )
  end
end