Skip to content

Commit

Permalink
Merge pull request #19 from gustavothecoder/move-add-command-to-domai…
Browse files Browse the repository at this point in the history
…n-layer

Move add command to the domain layer
  • Loading branch information
gustavothecoder authored Nov 6, 2023
2 parents d8301f0 + cdde658 commit c60b867
Show file tree
Hide file tree
Showing 29 changed files with 104 additions and 101 deletions.
4 changes: 2 additions & 2 deletions lib/pod/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def init
def add(feed)
puts "Adding the podcast..."

result = Pod::Commands::Add.call(feed, options)
result = Pod::Commands::Add::Runner.call(feed, options)

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

desc "podcasts", "List the podcast records"
Expand Down
7 changes: 5 additions & 2 deletions lib/pod/commands.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require_relative "commands/base"
require_relative "commands/base_runner"
require_relative "commands/base_output"
require_relative "commands/add/runner"
require_relative "commands/add/output"

require_relative "commands/init"
require_relative "commands/add"
require_relative "commands/podcasts"
require_relative "commands/episodes"
require_relative "commands/open"
Expand Down
66 changes: 0 additions & 66 deletions lib/pod/commands/add.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 Add < ::Pod::Outputs::Base
module Commands
module Add
class Output < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :successfully_added
Expand Down
68 changes: 68 additions & 0 deletions lib/pod/commands/add/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require_relative "../../infrastructure/feed_parser"

module Pod
module Commands
module Add
class Runner < Pod::Commands::BaseRunner
def call(feed, options = {})
parsed_feed = Infrastructure::FeedParser.call(feed)
parsed_options = parse_options(options)

if missing_data?(parsed_feed)
return build_failure_response(details: :badly_formatted)
end

db = Infrastructure::Storage::SQL.new(db: pod_db_dir)
db.transaction do
podcast = parsed_feed.podcast
podcast_feed = parsed_options["sync_url"] || podcast.feed
return build_failure_response(details: :missing_sync_url) if podcast_feed.nil?

db.execute <<-SQL
insert into podcasts
(name, description, feed, website)
values (
"#{escape_double_quotes(podcast.name)}",
"#{escape_double_quotes(podcast.description)}",
"#{escape_double_quotes(podcast_feed)}",
"#{escape_double_quotes(podcast.website)}"
);
SQL

inserted_podcast_id = db.query("select id from podcasts order by id desc limit 1;").first.id
parsed_feed.episodes.each do |e|
db.execute <<-SQL
insert into episodes
(title, release_date, podcast_id, duration, link, external_id)
values (
"#{escape_double_quotes(e.title)}",
"#{escape_double_quotes(e.release_date)}",
#{inserted_podcast_id},
"#{escape_double_quotes(e.duration)}",
"#{escape_double_quotes(e.link)}",
"#{e.external_id}"
);
SQL
end
end

build_success_response(details: :successfully_added)
rescue Infrastructure::Storage::Exceptions::ConstraintViolation
build_failure_response(details: :already_added)
end

private

def missing_data?(feed)
feed.podcast.nil? || feed.episodes.nil?
end

def escape_double_quotes(str)
str.gsub("\"", "\"\"")
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pod/commands/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down
4 changes: 2 additions & 2 deletions lib/pod/outputs/base.rb → lib/pod/commands/base_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "tabulo"

module Pod
module Outputs
class Base
module Commands
class BaseOutput
def self.call(context = {})
new(context).call
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pod
module Commands
class Base
class BaseRunner
def self.call(*args)
command = new
if command.method(:call).parameters.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/dearchive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/episodes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pod
module Commands
class Episodes < Base
class Episodes < Pod::Commands::BaseRunner
ALL_COLUMNS = %w[id title release_date duration link].freeze
private_constant :ALL_COLUMNS

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Pod
module Commands
class Init < Base
class Init < Pod::Commands::BaseRunner
def call
return build_failure_response(details: :home_not_found) if home_dir.nil?

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/open.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pod
module Commands
class Open < Base
class Open < Pod::Commands::BaseRunner
def call(episode_id, options = {})
parsed_options = parse_options(options)

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/podcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pod
module Commands
class Podcasts < Base
class Podcasts < Pod::Commands::BaseRunner
ALL_COLUMNS = %w[id name description feed website].freeze
private_constant :ALL_COLUMNS

Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Pod
module Commands
class Sync < Base
class Sync < Pod::Commands::BaseRunner
def call(podcast_id)
db = Infrastructure::Storage::SQL.new(db: pod_db_dir)
podcast = db.query("select feed from podcasts where id = #{podcast_id}").first
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/commands/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Pod
module Commands
class Update < Base
class Update < Pod::Commands::BaseRunner
def call(podcast_id, options = {})
parsed_options = parse_options(options)
return build_failure_response(details: :invalid_options) if parsed_options.empty?
Expand Down
2 changes: 0 additions & 2 deletions lib/pod/outputs/text.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require_relative "base"
require_relative "text/init"
require_relative "text/add"
require_relative "text/podcasts"
require_relative "text/episodes"
require_relative "text/open"
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Archive < ::Pod::Outputs::Base
class Archive < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :episode_archived
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/dearchive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Dearchive < ::Pod::Outputs::Base
class Dearchive < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :episode_dearchived
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Delete < ::Pod::Outputs::Base
class Delete < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :podcast_deleted
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/episodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Episodes < ::Pod::Outputs::Base
class Episodes < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :records_found
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Init < ::Pod::Outputs::Base
class Init < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :already_initialized
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Open < ::Pod::Outputs::Base
class Open < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :not_found
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/podcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Podcasts < ::Pod::Outputs::Base
class Podcasts < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :records_found
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Sync < ::Pod::Outputs::Base
class Sync < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :podcast_synchronized
Expand Down
2 changes: 1 addition & 1 deletion lib/pod/outputs/text/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Pod
module Outputs
module Text
class Update < ::Pod::Outputs::Base
class Update < ::Pod::Commands::BaseOutput
def call
case @context[:details]
when :podcast_updated
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::Add do
RSpec.describe Pod::Commands::Add::Output do
describe "#call" do
context "when the podcast is successfully added" 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::Add do
RSpec.describe Pod::Commands::Add::Runner do
before do
@db = Infrastructure::Storage::SQL.new(db: TestHelpers::Path.db_dir)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
end

if test.metadata[:populate_db]
Pod::Commands::Add.call("./spec/fixtures/soft_skills_engineering.xml")
Pod::Commands::Add.call(
Pod::Commands::Add::Runner.call("./spec/fixtures/soft_skills_engineering.xml")
Pod::Commands::Add::Runner.call(
"./spec/fixtures/fabio_akita.xml",
{"sync_url" => "https://www.fabio.com/feed.xml"}
)
Expand Down

0 comments on commit c60b867

Please sign in to comment.