Commit 7759ad81 authored by Jyrki's avatar Jyrki 🐾

Add /addWatch command

parent c72f6f99
require "../../connection/redis"
require "../../bad_dragon/size"
require "json"
......@@ -23,7 +24,7 @@ module Repository
"#{KEY_BASE}:#{{{key}}}"
end
def self.add(user_id : Int64, toy_sku, toy_size)
def self.add(user_id : Int32, toy_sku, toy_size : BadDragon::Size)
Application.logger.info "adding watch #{toy_sku}/#{toy_size} for #{user_id}"
key = redis_key(toy_sku)
loop do
......@@ -40,8 +41,8 @@ module Repository
end
end
def self.remove(user_id, toy_sku, toy_size)
Application.logger.info "removing watch #{toy_sku}:#{toy_size} for #{user_id}"
def self.remove(user_id, toy_sku, toy_size : BadDragon::Size)
Application.logger.info "removing watch #{toy_sku}/#{toy_size} for #{user_id}"
key = redis_key(toy_sku)
loop do
......@@ -62,7 +63,7 @@ module Repository
JSON.parse(
redis.hget(redis_key(toy_sku), toy_size) || "[]"
).as_a.map(&.as_i64)
).as_a.map(&.as_i)
end
private def self.redis
......
......@@ -14,12 +14,12 @@ module UseCase
end
def call(_argv)
Repository::WatchTargets.add(2, "nova", "extralarge")
Repository::WatchTargets.add(2, "nova", "extralarge")
Repository::WatchTargets.add(3, "nova", "extralarge")
Repository::WatchTargets.remove(2, "nova", "extralarge")
Repository::WatchTargets.remove(2, "nova", "extralarge")
Repository::WatchTargets.remove(3, "nova", "extralarge")
Repository::WatchTargets.add(2, "nova", BadDragon::Size::Extralarge)
Repository::WatchTargets.add(2, "nova", BadDragon::Size::Extralarge)
Repository::WatchTargets.add(3, "nova", BadDragon::Size::Extralarge)
Repository::WatchTargets.remove(2, "nova", BadDragon::Size::Extralarge)
Repository::WatchTargets.remove(2, "nova", BadDragon::Size::Extralarge)
Repository::WatchTargets.remove(3, "nova", BadDragon::Size::Extralarge)
end
def _call(_argv)
......
require "./slash_commands/start"
require "./slash_commands/help"
require "./slash_commands/add_watch"
require "telegram_bot"
require "./base"
require "../../application"
require "../../bad_dragon/size"
require "../../repository/watch_targets"
module UseCase
module SlashCommands
class AddWatch < SlashCommands::Base
def self.command_name
"addWatch"
end
def self.help_arguments
"SKU SIZE"
end
def self.help_text
"Tell me which toy and size I should look for."
end
def call(bot, msg, params)
return log_warning if msg.from.nil?
return bot.reply(msg, usage) unless params.size == 2
sku = params.shift
size = BadDragon::Size.parse(params.shift)
Repository::WatchTargets.add(msg.from.not_nil!.id, sku, size)
bot.reply msg, "I will notify you if there is a #{sku} in #{size} in the inventory."
end
private def log_warning
Application.logger.warn "got an addWatch request using a message without a user -- ignoring"
end
private def usage
usage = <<-EOF
Usage: /addwatch SKU SIZE
Parameters:
SKU - the SKU of the toy
SIZE - the size of the toy
Allowed values for SIZE:
EOF
usage += "\n"
usage += BadDragon::Size.values.map(&.to_s).join(", ")
end
end
end
end
......@@ -9,6 +9,10 @@ module UseCase
raise NotImplementedError.new("Implement #{self.class}.command_name!")
end
def self.help_arguments
""
end
def self.help_text
""
end
......
......@@ -19,7 +19,8 @@ module UseCase
private def formatted_commands
{{ UseCase::SlashCommands::Base.subclasses }}.reject(&.help_text.empty?).map do |klass|
" ➢ /#{klass.command_name} - #{klass.help_text}"
help_arguments = klass.help_arguments.empty? ? "" : " #{klass.help_arguments}"
" ➢ /#{klass.command_name}#{help_arguments} - #{klass.help_text}"
end.join("\n")
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment