Skip to content

Commit

Permalink
Merge pull request #20 from gustavothecoder/move-commands-to-domain-l…
Browse files Browse the repository at this point in the history
…ayer

Move commands to domain layer
  • Loading branch information
gustavothecoder committed Nov 7, 2023
2 parents c60b867 + a867bce commit 1d0046c
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 163 deletions.
16 changes: 8 additions & 8 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 All @@ -70,23 +70,23 @@ def open(episode_id)

desc "archive EPISODE_ID", "Archive the episode"
def archive(episode_id)
result = Pod::Commands::Archive.call(episode_id)
result = Pod::Commands::Archive::Runner.call(episode_id)

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

desc "dearchive EPISODE_ID", "Dearchive the episode."
def dearchive(episode_id)
result = Pod::Commands::Dearchive.call(episode_id)
result = Pod::Commands::Dearchive::Runner.call(episode_id)

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

desc "delete PODCAST_ID", "Delete the podcast from pod's database."
def delete(podcast_id)
result = Pod::Commands::Delete.call(podcast_id)
result = Pod::Commands::Delete::Runner.call(podcast_id)

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

desc "sync PODCAST_ID", "Sync the podcast."
Expand Down
12 changes: 8 additions & 4 deletions lib/pod/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
require_relative "commands/base_output"
require_relative "commands/add/runner"
require_relative "commands/add/output"
require_relative "commands/archive/runner"
require_relative "commands/archive/output"
require_relative "commands/dearchive/runner"
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/archive"
require_relative "commands/dearchive"
require_relative "commands/delete"
require_relative "commands/sync"
require_relative "commands/update"

Expand Down
24 changes: 0 additions & 24 deletions lib/pod/commands/archive.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 Archive < ::Pod::Commands::BaseOutput
module Commands
module Archive
class Output < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :episode_archived
Expand Down
26 changes: 26 additions & 0 deletions lib/pod/commands/archive/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Pod
module Commands
module Archive
class Runner < Pod::Commands::BaseRunner
def call(episode_id)
db = Infrastructure::Storage::SQL.new(db: pod_db_dir)

if db.query("select id from episodes where id = #{episode_id}").empty?
return build_failure_response(details: :not_found)
end

sql_code = <<~SQL
update episodes
set archived_at = '#{Time.now.iso8601}'
where id = #{episode_id};
SQL
db.execute(sql_code)

build_success_response(details: :episode_archived)
end
end
end
end
end
24 changes: 0 additions & 24 deletions lib/pod/commands/dearchive.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 Dearchive < ::Pod::Commands::BaseOutput
module Commands
module Dearchive
class Output < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :episode_dearchived
Expand Down
26 changes: 26 additions & 0 deletions lib/pod/commands/dearchive/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Pod
module Commands
module Dearchive
class Runner < Pod::Commands::BaseRunner
def call(episode_id)
db = Infrastructure::Storage::SQL.new(db: pod_db_dir)

if db.query("select id from episodes where id = #{episode_id}").empty?
return build_failure_response(details: :not_found)
end

sql_code = <<~SQL
update episodes
set archived_at = null
where id = #{episode_id};
SQL
db.execute(sql_code)

build_success_response(details: :episode_dearchived)
end
end
end
end
end
29 changes: 0 additions & 29 deletions lib/pod/commands/delete.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 Delete < ::Pod::Commands::BaseOutput
module Commands
module Delete
class Output < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :podcast_deleted
Expand Down
31 changes: 31 additions & 0 deletions lib/pod/commands/delete/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Pod
module Commands
module Delete
class Runner < Pod::Commands::BaseRunner
def call(podcast_id)
db = Infrastructure::Storage::SQL.new(db: pod_db_dir)

if db.query("select id from podcasts where id = #{podcast_id}").empty?
return build_failure_response(details: :not_found)
end

sql_code = <<~SQL
delete from episodes
where podcast_id = #{podcast_id};
SQL
db.execute(sql_code)

sql_code = <<~SQL
delete from podcasts
where id = #{podcast_id};
SQL
db.execute(sql_code)

build_success_response(details: :podcast_deleted)
end
end
end
end
end
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
4 changes: 0 additions & 4 deletions lib/pod/outputs/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

require_relative "text/init"
require_relative "text/podcasts"
require_relative "text/episodes"
require_relative "text/open"
require_relative "text/archive"
require_relative "text/dearchive"
require_relative "text/delete"
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::Archive do
RSpec.describe Pod::Commands::Archive::Output do
describe "#call" do
context "when episode was archived" do
it "generates the correct message" do
Expand Down
Loading

0 comments on commit 1d0046c

Please sign in to comment.