diff --git a/README.md b/README.md index 340ba50..4924eda 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,11 @@ Once the server is running, open your web browser and go to 1 - else diff --git a/db/migrate/20230901123856_create_activities.rb b/db/migrate/20230901123856_create_activities.rb index 0df5525..b09b7db 100644 --- a/db/migrate/20230901123856_create_activities.rb +++ b/db/migrate/20230901123856_create_activities.rb @@ -1,6 +1,7 @@ class CreateActivities < ActiveRecord::Migration[7.0] def change create_table :activities do |t| + t.string :activity t.string :activity_type t.integer :participants t.float :price diff --git a/db/schema.rb b/db/schema.rb index add6f91..cc6fba2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,6 +15,7 @@ enable_extension "plpgsql" create_table "activities", force: :cascade do |t| + t.string "activity" t.string "activity_type" t.integer "participants" t.float "price" diff --git a/my_program.rb b/my_program.rb index 32c02c8..54be175 100644 --- a/my_program.rb +++ b/my_program.rb @@ -16,18 +16,21 @@ class MyProgram < Thor option :maxaccessibility def new - params = options.transform_keys(&:to_s) - result = ApiWrapper::Index::Organizer.call(params_to_validate: params) - if result.success? - console_print(result) - else - puts "Error: #{result.errors}" - end + result = ApiWrapper::Index::Organizer.call(params_to_validate: options.transform_keys(&:to_s)) + result.success? ? new_print(result) : puts("Error: #{result.errors}") + end + + desc 'list', 'List latest activities' + + def list + result = ApiWrapper::LatestActivities::Organizer.call(latest_activities_params: { 'order' => 'desc', + 'sort' => 'created_at' }) + result.success? ? list_print(result.activities.first(5)) : puts("Error: #{result.errors}") end private - def console_print(result) + def new_print(result) pastel = Pastel.new headers = colorized_headers(pastel) rows = colorized_rows(pastel, result.activity) @@ -37,6 +40,15 @@ def console_print(result) puts pastel.yellow("\nActivity saved to database") end + def list_print(activities) + pastel = Pastel.new + headers = colorized_headers(pastel) + rows = activities.map { |activity| colorized_rows_from_model(pastel, activity) } + + table = TTY::Table.new headers, rows + puts table.render(:ascii, padding: [0, 1, 0, 1]) + end + def colorized_headers(pastel) %w[Activity Type Participants Price Link Key Accessibility].map { |header| pastel.blue.bold(header) } end @@ -46,6 +58,18 @@ def colorized_rows(pastel, activity) row = row_data.map { |key| pastel.green(activity[key]) } [row] end + + def colorized_rows_from_model(pastel, activity) + [ + pastel.green(activity.activity), + pastel.green(activity.activity_type), + pastel.green(activity.participants), + pastel.green(activity.price), + pastel.green(activity.link), + pastel.green(activity.key), + pastel.green(activity.accessibility) + ] + end end MyProgram.start(ARGV)