Skip to content

Commit

Permalink
[model] implement and test attributions; automatically create amazon …
Browse files Browse the repository at this point in the history
…attributions
  • Loading branch information
moritz committed Mar 25, 2011
1 parent 7bfadf9 commit de54b5d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 16 deletions.
26 changes: 26 additions & 0 deletions lib/Quelology/Model/Result/Attribution.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Quelology::Model::Result::Attribution;
use parent qw/DBIx::Class::Core/;
use utf8;

__PACKAGE__->table('attribution');
__PACKAGE__->add_columns(
'id',
medium_id => {
data_type => 'integer',
is_nullable => 0,
is_foreign_key => 1,
is_numeric => 1,
},
name => {
data_type => 'varchar',
is_nullable => 0,
},
url => {
data_type => 'varchar',
},
);

__PACKAGE__->set_primary_key('id');
__PACKAGE__->belongs_to('medium', 'Quelology::Model::Result::Medium', 'medium_id');

1;
4 changes: 4 additions & 0 deletions lib/Quelology/Model/Result/Medium.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package Quelology::Model::Result::Medium;
use parent qw/DBIx::Class::Core/;
use aliased Quelology::Model::DropPoint => 'DropPoint';
use utf8;
use strict;
use warnings;

__PACKAGE__->load_components(qw( Tree::NestedSet ));
__PACKAGE__->table('medium');
Expand All @@ -28,6 +30,8 @@ __PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraint(['asin']);
__PACKAGE__->belongs_to('root', 'Quelology::Model::Result::Medium', 'root_id');
__PACKAGE__->belongs_to('alias_for', 'Quelology::Model::Result::Medium', 'same_as');
__PACKAGE__->has_many('attributions', 'Quelology::Model::Result::Attribution',
'medium_id');

__PACKAGE__->tree_columns({
root_column => 'root_id',
Expand Down
15 changes: 14 additions & 1 deletion lib/Quelology/Model/ResultSet/Medium.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ sub websearch {
push @series, $r->root;
}
} else {
my $r = $self->create(_hash_from_xml_amazon($_));
my $h = _hash_from_xml_amazon($_);
my $r = $self->create($h);
$r->create_related('attributions',
{
name => 'amazon',
url => $h->{amazon_url},
},
);
push @media, $r;
}
}
Expand All @@ -86,6 +93,12 @@ sub from_asin {
if ($m) {
my $h = _hash_from_xml_amazon($m);
$row = $self->create($h);
$row->create_related('attributions',
{
name => 'amazon',
url => $h->{amazon_url},
},
);
} else {
confess "Failed to retrieve medium with asin '$asin': neither in DB nor in amazon";
}
Expand Down
8 changes: 8 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ CREATE TABLE user_info (
real_name VARCHAR(64),
email VARCHAR(255)
);

DROP TABLE IF EXISTS attribution CASCADE;
CREATE TABLE attribution (
id SERIAL PRIMARY KEY,
medium_id INTEGER NOT NULL REFERENCES medium (id),
name VARCHAR(64) NOT NULL,
url VARCHAR(255)
);
Loading

0 comments on commit de54b5d

Please sign in to comment.