| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Slovo::Command::prodan::products; | 
| 2 | 2 |  |  | 2 |  | 51747 | use Mojo::Base 'Slovo::Command', -signatures; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 49 |  | 
| 3 | 2 |  |  | 2 |  | 1505 | use Mojo::File qw(path); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 100 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use Mojo::Loader qw(data_section file_is_binary); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 75 |  | 
| 5 | 2 |  |  | 2 |  | 21 | use Mojo::Util qw(encode decode getopt dumper); | 
|  | 2 |  |  |  |  | 50 |  | 
|  | 2 |  |  |  |  | 96 |  | 
| 6 | 2 |  |  | 2 |  | 316 | use YAML::XS qw(Dump DumpFile LoadFile); | 
|  | 2 |  |  |  |  | 2275 |  | 
|  | 2 |  |  |  |  | 99 |  | 
| 7 | 2 |  |  | 2 |  | 11 | use Mojo::JSON qw(to_json); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1823 |  | 
| 8 |  |  |  |  |  |  | has description => 'Manage products on the command line'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has usage   => sub { shift->extract_usage }; | 
| 11 |  |  |  |  |  |  | has actions => sub { [qw(create update dump delete list)] }; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 5 |  |  | 5 | 1 | 86666 | sub run ($self, @args) { | 
|  | 5 |  |  |  |  | 16 |  | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 10 |  | 
| 14 | 5 |  | 100 |  |  | 26 | my $action    = shift @args || 'list'; | 
| 15 | 5 |  |  |  |  | 13 | my $a_pattern = '^(?:' . join('|', @{$self->actions}) . ')$'; | 
|  | 5 |  |  |  |  | 20 |  | 
| 16 |  |  |  |  |  |  | $action =~ $a_pattern | 
| 17 |  |  |  |  |  |  | || STDERR->say('Only ' | 
| 18 | 5 | 50 | 0 |  |  | 78 | . join(',', @{$self->actions}) | 
|  | 0 |  |  |  |  | 0 |  | 
| 19 |  |  |  |  |  |  | . ' actions are supported.' | 
| 20 |  |  |  |  |  |  | . $/ | 
| 21 |  |  |  |  |  |  | . $/ | 
| 22 |  |  |  |  |  |  | . $self->usage) | 
| 23 |  |  |  |  |  |  | && return; | 
| 24 | 5 |  |  |  |  | 36 | getopt \@args, | 
| 25 |  |  |  |  |  |  | 'f|file=s'  => \(my $file  = ''), | 
| 26 |  |  |  |  |  |  | 'w|where=s' => \(my $where = ''), | 
| 27 |  |  |  |  |  |  | 'l|limit=i' => \(my $limit = 100), | 
| 28 |  |  |  |  |  |  | 'o|ofset=s' => \(my $ofset = 0); | 
| 29 | 5 |  |  |  |  | 2609 | my $file_actions  = join('|', @{$self->actions}[0 .. 2]); | 
|  | 5 |  |  |  |  | 16 |  | 
| 30 | 5 |  |  |  |  | 34 | my $where_actions = join('|', @{$self->actions}[3 .. 4]); | 
|  | 5 |  |  |  |  | 12 |  | 
| 31 | 5 | 100 |  |  |  | 89 | if ($action =~ /$file_actions/) { | 
|  |  | 50 |  |  |  |  |  | 
| 32 | 4 | 100 | 50 |  |  | 24 | $file | 
| 33 |  |  |  |  |  |  | || STDERR->say( | 
| 34 |  |  |  |  |  |  | 'Please profide a YAML file to read data from' . $/ . $/ . $self->usage) | 
| 35 |  |  |  |  |  |  | && return; | 
| 36 | 2 | 50 | 0 |  |  | 15 | $file =~ /\.(ya?ml|json)$/ | 
| 37 |  |  |  |  |  |  | || STDERR->say( | 
| 38 |  |  |  |  |  |  | 'Only YAML and json files are supported right now.' . $/ . $/ . $self->usage) | 
| 39 |  |  |  |  |  |  | && return; | 
| 40 | 2 |  |  |  |  | 16 | $action = "_$action"; | 
| 41 | 2 |  |  |  |  | 10 | $self->$action($file); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | elsif ($action =~ /$where_actions/) { | 
| 44 | 1 | 50 | 33 |  |  | 6 | $action eq 'delete' | 
| 45 |  |  |  |  |  |  | && STDERR->say('Please provide a WHERE clause for DELETE!') | 
| 46 |  |  |  |  |  |  | && return; | 
| 47 | 1 |  |  |  |  | 4 | $action = "_$action"; | 
| 48 | 1 |  |  |  |  | 6 | $self->$action($where, $limit, $ofset); | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 3 |  |  |  |  | 116 | return; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 1 |  |  | 1 |  | 3 | sub _create ($self, $file) { | 
|  | 1 |  |  |  |  | 27 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 54 | 1 |  |  |  |  | 8 | my $products = LoadFile $file; | 
| 55 | 1 |  |  |  |  | 331 | my $db       = $self->app->dbx->db; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # INSERT | 
| 58 | 1 |  |  |  |  | 313 | for (@$products) { | 
| 59 |  |  |  |  |  |  | do { | 
| 60 | 4 |  |  |  |  | 6360 | say encode utf8 => "Inserting $_->{alias}, $_->{sku}"; | 
| 61 |  |  |  |  |  |  | $db->insert( | 
| 62 |  |  |  |  |  |  | 'products' => { | 
| 63 |  |  |  |  |  |  | alias  => $_->{alias}, | 
| 64 |  |  |  |  |  |  | sku    => $_->{sku}, | 
| 65 |  |  |  |  |  |  | title  => $_->{title}, | 
| 66 |  |  |  |  |  |  | p_type => $_->{p_type}, | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # The data is NOT encoded to UTF8 by to_json | 
| 69 | 4 |  |  |  |  | 60 | properties => to_json($_->{properties})}); | 
| 70 |  |  |  |  |  |  | } unless $db->select('products', ['id'], {alias => $_->{alias}, sku => $_->{sku},}) | 
| 71 | 4 | 50 |  |  |  | 5508 | ->hash; | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 1 |  |  |  |  | 1710 | return; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 1 |  |  | 1 |  | 3 | sub _update ($self, $file) { | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 42 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 77 | 1 |  |  |  |  | 6 | my $products = LoadFile $file; | 
| 78 | 1 |  |  |  |  | 308 | my $db       = $self->app->dbx->db; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # UPDATE all the products found in the file | 
| 81 | 1 |  |  |  |  | 386 | for (@$products) { | 
| 82 | 2 |  |  |  |  | 3881 | say encode utf8 => "Updating $_->{alias}, $_->{sku}"; | 
| 83 | 2 |  |  |  |  | 31 | $_->{properties} = to_json($_->{properties}); | 
| 84 | 2 |  |  |  |  | 60 | $db->update('products', $_, {sku => $_->{sku}}); | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 1 |  |  |  |  | 3297 | return; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  | 0 |  | 0 | sub _delete ($self, $where, $limit, $offset) { | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 90 | 0 |  |  |  |  | 0 | Carp::croak "Action delete - Not implemented"; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 1 |  |  | 1 |  | 3 | sub _list ($self, $where, $limit, $offset) { | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 94 | 1 |  |  |  |  | 7 | STDOUT->say('Action list - Not implemented' . $/, $self->usage); | 
| 95 | 1 |  |  |  |  | 9961 | return; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | 1; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =encoding utf8 | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head1 NAME | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Slovo::Command::prodan::products - manage products on the command line | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | slovo prodan products create --from ./products.yaml | 
| 108 |  |  |  |  |  |  | slovo prodan products update --from ./products.yaml | 
| 109 |  |  |  |  |  |  | slovo prodan products list   --where "alias like'%лечителката%'" | 
| 110 |  |  |  |  |  |  | slovo prodan products delete --where "alias like'%лечителката%'" | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | Slovo::Command::prodan::products is a command to easily create, list, update or | 
| 115 |  |  |  |  |  |  | delete a bunch of products on the command line. For now only adding products | 
| 116 |  |  |  |  |  |  | from (and dumping to) YAML files is supported. In the future CSV and XLS files | 
| 117 |  |  |  |  |  |  | may be supported too. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | The idea is that YAML is very human friendly and a user can edit such a file | 
| 120 |  |  |  |  |  |  | and then feed it to this command to create or update the items in this file. | 
| 121 |  |  |  |  |  |  | Example files with product items can be found in the test folder of this | 
| 122 |  |  |  |  |  |  | distribution. | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | This command is still alfa quality and its functionality may change often. | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | L, | 
| 129 |  |  |  |  |  |  | L, | 
| 130 |  |  |  |  |  |  | L | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =cut | 
| 134 |  |  |  |  |  |  |  |