| 1 | package Wishlist::Model; | ||||||||||
| 2 | 2 | ▶ | use Mojo::Base -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Passwords (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 0 | ▶ | has sqlite => sub { Carp::croak 'sqlite is required' }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | sub add_user { | ||||||||
|
Subroutine
add_user: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 10 | 1 | my ($self, $user) = @_; | |||||||||
| 11 | Carp::croak 'password is required' | ||||||||||
| 12 | 1 | ▶ | unless $user->{password}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 1 | $user->{password} = Passwords::password_hash($user->{password}); | |||||||||
| 14 | 1 | return $self | |||||||||
| 15 | ->sqlite | ||||||||||
| 16 | ->db | ||||||||||
| 17 | ->insert(users => $user) | ||||||||||
| 18 | ->last_insert_id; | ||||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 4 | ▶ | sub check_password { | ||||||||
|
Subroutine
check_password: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 22 | 4 | my ($self, $username, $password) = @_; | |||||||||
| 23 | 4 | ▶ | return undef unless $password; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 2 | my $user = $self | |||||||||
| 25 | ->sqlite | ||||||||||
| 26 | ->db | ||||||||||
| 27 | ->select( | ||||||||||
| 28 | 'users' => ['password'], | ||||||||||
| 29 | {username => $username}, | ||||||||||
| 30 | )->hash; | ||||||||||
| 31 | 2 | ▶ | return undef unless $user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | return Passwords::password_verify( | ||||||||||
| 33 | $password, | ||||||||||
| 34 | $user->{password}, | ||||||||||
| 35 | 2 | ); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 2 | ▶ | sub user { | ||||||||
|
Subroutine
user: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | 2 | my ($self, $username) = @_; | |||||||||
| 40 | 2 | my $sql = <<' SQL'; | |||||||||
| 41 | select | ||||||||||
| 42 | user.id, | ||||||||||
| 43 | user.name, | ||||||||||
| 44 | user.username, | ||||||||||
| 45 | ( | ||||||||||
| 46 | select | ||||||||||
| 47 | json_group_array(item) | ||||||||||
| 48 | from ( | ||||||||||
| 49 | select json_object( | ||||||||||
| 50 | 'id', items.id, | ||||||||||
| 51 | 'title', items.title, | ||||||||||
| 52 | 'url', items.url, | ||||||||||
| 53 | 'purchased', items.purchased | ||||||||||
| 54 | ) as item | ||||||||||
| 55 | from items | ||||||||||
| 56 | where items.user_id=user.id | ||||||||||
| 57 | ) | ||||||||||
| 58 | ) as items | ||||||||||
| 59 | from users user | ||||||||||
| 60 | where user.username=? | ||||||||||
| 61 | SQL | ||||||||||
| 62 | 2 | return $self | |||||||||
| 63 | ->sqlite | ||||||||||
| 64 | ->db | ||||||||||
| 65 | ->query($sql, $username) | ||||||||||
| 66 | ->expand(json => 'items') | ||||||||||
| 67 | ->hash; | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | sub all_users { | ||||||||
|
Subroutine
all_users: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 71 | 2 | my $self = shift; | |||||||||
| 72 | 2 | return $self | |||||||||
| 73 | ->sqlite | ||||||||||
| 74 | ->db | ||||||||||
| 75 | ->select( | ||||||||||
| 76 | 'users' => [qw/username name/], | ||||||||||
| 77 | undef, | ||||||||||
| 78 | {-asc => 'name'}, | ||||||||||
| 79 | ) | ||||||||||
| 80 | ->hashes; | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | sub add_item { | ||||||||
|
Subroutine
add_item: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 1 | my ($self, $user, $item) = @_; | |||||||||
| 85 | 1 | $item->{user_id} = $user->{id}; | |||||||||
| 86 | 1 | return $self | |||||||||
| 87 | ->sqlite | ||||||||||
| 88 | ->db | ||||||||||
| 89 | ->insert('items' => $item) | ||||||||||
| 90 | ->last_insert_id; | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub update_item { | ||||||||
|
Subroutine
update_item: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 94 | 0 | my ($self, $item, $purchased) = @_; | |||||||||
| 95 | return $self | ||||||||||
| 96 | ->sqlite | ||||||||||
| 97 | ->db | ||||||||||
| 98 | ->update( | ||||||||||
| 99 | 'items', | ||||||||||
| 100 | {purchased => $purchased}, | ||||||||||
| 101 | {id => $item->{id}}, | ||||||||||
| 102 | 0 | )->rows; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | sub remove_item { | ||||||||
|
Subroutine
remove_item: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 106 | 0 | my ($self, $item) = @_; | |||||||||
| 107 | return $self | ||||||||||
| 108 | ->sqlite | ||||||||||
| 109 | ->db | ||||||||||
| 110 | ->delete( | ||||||||||
| 111 | 'items', | ||||||||||
| 112 | {id => $item->{id}}, | ||||||||||
| 113 | 0 | )->rows; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 2 | 1; | |||||||||
| 117 | |||||||||||