Skip to content

Commit

Permalink
Move episodes command to the domain layer
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavothecoder committed Nov 6, 2023
1 parent e2cddbf commit a867bce
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 53 deletions.
4 changes: 2 additions & 2 deletions lib/pod/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def podcasts
method_option :order_by, type: :string, default: "id", desc: "Choose how pod will order the episodes."
method_option :all, type: :boolean, default: false, desc: "List archived episodes too."
def episodes(podcast_id)
result = Pod::Commands::Episodes.call(podcast_id, options)
result = Pod::Commands::Episodes::Runner.call(podcast_id, options)

puts Pod::Outputs::Text::Episodes.call(result)
puts Pod::Commands::Episodes::Output.call(result)
end

desc "open EPISODE_ID", "Open a episode in the browser"
Expand Down
3 changes: 2 additions & 1 deletion lib/pod/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
require_relative "commands/dearchive/output"
require_relative "commands/delete/runner"
require_relative "commands/delete/output"
require_relative "commands/episodes/runner"
require_relative "commands/episodes/output"

require_relative "commands/init"
require_relative "commands/podcasts"
require_relative "commands/episodes"
require_relative "commands/open"
require_relative "commands/sync"
require_relative "commands/update"
Expand Down
43 changes: 0 additions & 43 deletions lib/pod/commands/episodes.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module Pod
module Outputs
module Text
class Episodes < ::Pod::Commands::BaseOutput
module Commands
module Episodes
class Output < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :records_found
Expand Down
45 changes: 45 additions & 0 deletions lib/pod/commands/episodes/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Pod
module Commands
module Episodes
class Runner < Pod::Commands::BaseRunner
ALL_COLUMNS = %w[id title release_date duration link].freeze
private_constant :ALL_COLUMNS

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: pod_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
end
end
end
end
1 change: 0 additions & 1 deletion lib/pod/outputs/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require_relative "text/init"
require_relative "text/podcasts"
require_relative "text/episodes"
require_relative "text/open"
require_relative "text/sync"
require_relative "text/update"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe Pod::Outputs::Text::Episodes do
RSpec.describe Pod::Commands::Episodes::Output do
describe "#call" do
context "when records are found" do
it "generates the correct message" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require_relative "../../support/test_helpers"
require_relative "../../../support/test_helpers"

RSpec.describe Pod::Commands::Episodes do
RSpec.describe Pod::Commands::Episodes::Runner do
describe "#call", :init_pod do
context "when there are episodes", :populate_db do
it "returns a success response with the table records" do
Expand Down

0 comments on commit a867bce

Please sign in to comment.