| 1 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Interchange6::Schema::Result::Cart; | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Interchange6::Schema::Result::Cart | ||||||||||
| 8 | |||||||||||
| 9 | =cut | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | use Interchange6::Schema::Candy -components => | |||||||||
| 13 | 2 | ▶ | [qw(InflateColumn::DateTime TimeStamp)]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | The Cart class (table) is used for storing shopping carts with products in the | ||||||||||
| 18 | cart held in the related L<Interchange6::Schema::Result::CartProduct> class. | ||||||||||
| 19 | |||||||||||
| 20 | =head1 ACCESSORS | ||||||||||
| 21 | |||||||||||
| 22 | =head2 carts_id | ||||||||||
| 23 | |||||||||||
| 24 | Primary key. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 2 | primary_column carts_id => { | |||||||||
| 29 | data_type => "integer", | ||||||||||
| 30 | is_auto_increment => 1, | ||||||||||
| 31 | }; | ||||||||||
| 32 | |||||||||||
| 33 | =head2 name | ||||||||||
| 34 | |||||||||||
| 35 | The name of the cart. You might perhaps have a "main" cart that is used by | ||||||||||
| 36 | default for the current shopping session and also a "wishlist" cart. Other | ||||||||||
| 37 | uses might be "saved_items" so a user can save things for another time or | ||||||||||
| 38 | maybe on logout all cart items are moved to "previous_session" cart. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 2 | column name => { | |||||||||
| 43 | data_type => "varchar", | ||||||||||
| 44 | default_value => "", | ||||||||||
| 45 | size => 255, | ||||||||||
| 46 | }; | ||||||||||
| 47 | |||||||||||
| 48 | =head2 users_id | ||||||||||
| 49 | |||||||||||
| 50 | Foreign key constraint on L<Interchange6::Schema::Result::User/users_id> | ||||||||||
| 51 | via L</user> relationship. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 2 | column users_id => { | |||||||||
| 56 | data_type => "integer", | ||||||||||
| 57 | is_nullable => 1, | ||||||||||
| 58 | }; | ||||||||||
| 59 | |||||||||||
| 60 | =head2 sessions_id | ||||||||||
| 61 | |||||||||||
| 62 | Foreign key constraint on L<Interchange6::Schema::Result::Session/sessions_id> | ||||||||||
| 63 | via L</session> relationship. Is nullable. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 2 | column sessions_id => { | |||||||||
| 68 | data_type => "varchar", | ||||||||||
| 69 | is_nullable => 1, | ||||||||||
| 70 | size => 255, | ||||||||||
| 71 | }; | ||||||||||
| 72 | |||||||||||
| 73 | =head2 created | ||||||||||
| 74 | |||||||||||
| 75 | Date and time when this record was created returned as L<DateTime> object. | ||||||||||
| 76 | Value is auto-set on insert. | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | 2 | column created => { | |||||||||
| 81 | data_type => "datetime", | ||||||||||
| 82 | set_on_create => 1, | ||||||||||
| 83 | }; | ||||||||||
| 84 | |||||||||||
| 85 | =head2 last_modified | ||||||||||
| 86 | |||||||||||
| 87 | Date and time when this record was last modified returned as L<DateTime> object. | ||||||||||
| 88 | Value is auto-set on insert and update. | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | 2 | column last_modified => { | |||||||||
| 93 | data_type => "datetime", | ||||||||||
| 94 | set_on_create => 1, | ||||||||||
| 95 | set_on_update => 1, | ||||||||||
| 96 | }; | ||||||||||
| 97 | |||||||||||
| 98 | =head1 UNIQUE CONSTRAINTS | ||||||||||
| 99 | |||||||||||
| 100 | =head2 carts_name_sessions_id | ||||||||||
| 101 | |||||||||||
| 102 | On ( name, sessions_id ) | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | 2 | unique_constraint carts_name_sessions_id => [qw/ name sessions_id /]; | |||||||||
| 107 | |||||||||||
| 108 | =head1 RELATIONS | ||||||||||
| 109 | |||||||||||
| 110 | =head2 cart_products | ||||||||||
| 111 | |||||||||||
| 112 | Type: has_many | ||||||||||
| 113 | |||||||||||
| 114 | Related object: L<Interchange6::Schema::Result::CartProduct> | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 2 | has_many | |||||||||
| 119 | cart_products => "Interchange6::Schema::Result::CartProduct", "carts_id"; | ||||||||||
| 120 | |||||||||||
| 121 | =head2 session | ||||||||||
| 122 | |||||||||||
| 123 | Type: belongs_to | ||||||||||
| 124 | |||||||||||
| 125 | Related object: L<Interchange6::Schema::Result::Session> | ||||||||||
| 126 | |||||||||||
| 127 | =cut | ||||||||||
| 128 | |||||||||||
| 129 | 2 | belongs_to | |||||||||
| 130 | session => "Interchange6::Schema::Result::Session", | ||||||||||
| 131 | { sessions_id => "sessions_id" }, | ||||||||||
| 132 | { | ||||||||||
| 133 | is_deferrable => 1, | ||||||||||
| 134 | on_delete => "SET NULL", | ||||||||||
| 135 | join_type => "left" | ||||||||||
| 136 | }; | ||||||||||
| 137 | |||||||||||
| 138 | =head2 user | ||||||||||
| 139 | |||||||||||
| 140 | Type: belongs_to | ||||||||||
| 141 | |||||||||||
| 142 | Related object: L<Interchange6::Schema::Result::User> | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||
| 145 | |||||||||||
| 146 | 2 | belongs_to | |||||||||
| 147 | user => "Interchange6::Schema::Result::User", | ||||||||||
| 148 | { users_id => "users_id" }, | ||||||||||
| 149 | { | ||||||||||
| 150 | is_deferrable => 1, | ||||||||||
| 151 | on_delete => "CASCADE", | ||||||||||
| 152 | on_update => "CASCADE", | ||||||||||
| 153 | join_type => "left" | ||||||||||
| 154 | }; | ||||||||||
| 155 | |||||||||||
| 156 | =head1 METHODS | ||||||||||
| 157 | |||||||||||
| 158 | =head2 clone($name) | ||||||||||
| 159 | |||||||||||
| 160 | Return a clone of the with the new name. If a clone with the same name | ||||||||||
| 161 | and same session already exists, the clone is removed and recreated anew. | ||||||||||
| 162 | |||||||||||
| 163 | =cut | ||||||||||
| 164 | |||||||||||
| 165 | sub clone { | ||||||||||
| 166 | 10 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
clone: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 167 | 10 | ▶ | croak "Can't clone a cart without a name" unless $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 7 | ▶ | croak "Can't clone using the same name" if $name eq $self->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 7 | my $schema = $self->result_source->schema; | |||||||||
| 170 | 7 | my $guard = $schema->txn_scope_guard; | |||||||||
| 171 | 7 | ▶ | if (defined $self->sessions_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 4 | $schema->resultset('Cart')->search({ | |||||||||
| 173 | name => $name, | ||||||||||
| 174 | sessions_id => $self->sessions_id, | ||||||||||
| 175 | })->delete; | ||||||||||
| 176 | } | ||||||||||
| 177 | # the products are carried over by copy itself because it's an has_many | ||||||||||
| 178 | # https://metacpan.org/pod/DBIx::Class::Row#copy | ||||||||||
| 179 | 7 | my $clone = $self->copy({ name => $name }); | |||||||||
| 180 | 7 | $guard->commit; | |||||||||
| 181 | 7 | return $clone; | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 2 | 1; | |||||||||