From 438a3d4cc71559fe34713f8bd1accd62ad7e4b7f Mon Sep 17 00:00:00 2001 From: Pasqualino Ferrentino Date: Tue, 4 Sep 2007 09:59:51 +0200 Subject: [PATCH] I am able to have the summary of the ads --- src/lib/Bcd/Data/Activities.pm | 28 ++++++++++++++++++++++ src/lib/Bcd/Data/Ads.pm | 11 +++++++++ src/script/bcd.pl | 2 +- src/t/TestActivities.pm | 53 ++++++++++++++++++++++++++++++++++++++++++ src/t/TestAds.pm | 11 +++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/lib/Bcd/Data/Activities.pm b/src/lib/Bcd/Data/Activities.pm index 94792e3..b7d03af 100644 --- a/src/lib/Bcd/Data/Activities.pm +++ b/src/lib/Bcd/Data/Activities.pm @@ -78,6 +78,34 @@ sub build_activity_tree_from{ return \@list; } +sub build_activity_tree_summary_from{ + my ($class, $stash, $root_id, $post_code) = @_; + + my $tree = $class->build_activity_tree_from($stash, $root_id); + + #ok, now for each activity I get the summary + $class->_populate_with_summary_this_tree($stash, $tree, $post_code); + + return $tree; +} + +sub _populate_with_summary_this_tree{ + my ($class, $stash, $tree, $post_code) = @_; + + #this is a recursive function just to add the number of ads for each activity + foreach(@{$tree}){ + if (ref($_->[1]) eq "ARRAY"){ + #ok, let's recurse + $class->_populate_with_summary_this_tree($stash, $_->[1], $post_code); + } else { + #ok, it is a scalar + #get the number of ads + my $ads = Bcd::Data::Ads->get_count_activity_post_code_ads($stash, $_->[1], $post_code); + push(@{$_}, $ads); + } + } +} + sub init_db{ my ($class, $stash) = @_; diff --git a/src/lib/Bcd/Data/Ads.pm b/src/lib/Bcd/Data/Ads.pm index 1fc5181..667f491 100644 --- a/src/lib/Bcd/Data/Ads.pm +++ b/src/lib/Bcd/Data/Ads.pm @@ -54,6 +54,11 @@ use constant { qq{ WHERE a.id_act = ? AND a.id_user = u.id AND u.id_ant_nest = ? AND a.id_state = }. Bcd::Constants::AdsConstants::ACTIVE, + GET_COUNT_ADS_FOR_ACTIVITY => + qq{SELECT COUNT(*) FROM ads as a, users as u }. + qq{ WHERE a.id_act = ? AND a.id_user = u.id AND u.id_ant_nest = ? AND a.id_state = }. + Bcd::Constants::AdsConstants::ACTIVE, + }; sub get_activity_post_code_ads_arr{ @@ -61,6 +66,12 @@ sub get_activity_post_code_ads_arr{ return $stash->select_all_arr(GET_ADS_FOR_THIS_ACTIVITY, $id_act, $post_code); } +sub get_count_activity_post_code_ads{ + my ($class, $stash, $id_act, $post_code) = @_; + my $row = $stash->select_one_row_arr(GET_COUNT_ADS_FOR_ACTIVITY, $id_act, $post_code); + return (!defined($row)) ? 0 : $row->[0]; +} + sub get_user_ads_ds{ my ($class, $stash, $user_id) = @_; return $stash->select_all_ds(GET_USER_ADS, $user_id); diff --git a/src/script/bcd.pl b/src/script/bcd.pl index be89e5d..c421e6f 100755 --- a/src/script/bcd.pl +++ b/src/script/bcd.pl @@ -43,7 +43,7 @@ use Getopt::Long; use Data::Dumper; use Pod::Usage; use constant { - BCD_VERSION => '0.5', + BCD_VERSION => '0.6', }; $SIG{CHLD} = 'IGNORE'; diff --git a/src/t/TestActivities.pm b/src/t/TestActivities.pm index 489b588..e76e24e 100644 --- a/src/t/TestActivities.pm +++ b/src/t/TestActivities.pm @@ -67,6 +67,59 @@ sub test_get_tree_from_command{ $self->_check_tree($tree, $st); } +sub test_get_summary_tree{ + my $self = shift; + my $stash= $self->{stash}; + + my $tree = Bcd::Data::Activities->build_activity_tree_summary_from + ($stash, + Bcd::Constants::ActivitiesConstants::PARENT_SERVICES, 8012898); + + #ok... I should have all nulls... because I don't have ads, for now. + $self->_check_tree_with_nulls($tree); + + #let's add a fake ad + my $id_presence = Bcd::Data::PublicSites->add_special_site_presence + ($stash, 26, 0, 2, 1, "a presence"); + + #ok, Now I should be able to create an ad + my $ad_id1 = Bcd::Data::Ads->create_new_ad + ($stash, 12, 16, "first ad", $id_presence, 90, 100, 0.1, 0.2); + + my $count = Bcd::Data::Ads->get_count_activity_post_code_ads + ($stash, 12, 8012898); + + $self->assert_equals(1, $count); + + $tree = Bcd::Data::Activities->build_activity_tree_summary_from + ($stash, + Bcd::Constants::ActivitiesConstants::PARENT_SERVICES, 8012898); + + $count = Bcd::Data::Ads->get_count_activity_post_code_ads + ($stash, 12, 8012898); + + $self->assert_equals(1, $count); + + #this is hard coded, if you change the activity this test will surely + #fail, but it is not so important + $self->assert_equals(1, $tree->[0]->[1]->[0]->[1]->[0]->[2]); +} + +sub _check_tree_with_nulls{ + my ($self, $tree) = @_; + + foreach(@{$tree}){ + if (ref($_->[1]) eq "ARRAY"){ + #ok, let's recurse + $self->_check_tree_with_nulls($_->[1]); + } else { + #ok, it is a scalar + #I should have zero ads. + $self->assert_equals(0, $_->[2]); + } + } +} + #This is a recursive function that checks the layout of the tree sub _check_tree{ my ($self, $tree, $st) = @_; diff --git a/src/t/TestAds.pm b/src/t/TestAds.pm index d35e62a..3503726 100644 --- a/src/t/TestAds.pm +++ b/src/t/TestAds.pm @@ -143,10 +143,21 @@ sub test_create_new_ad{ my $id_presence = Bcd::Data::PublicSites->add_special_site_presence ($stash, 16, 0, 2, 1, "a presence"); + my $count = Bcd::Data::Ads->get_count_activity_post_code_ads + ($stash, 1, 8012898); + + $self->assert_equals(0, $count); + #ok, Now I should be able to create an ad my $ad_id = Bcd::Data::Ads->create_new_ad ($stash, 1, 16, "a text ad", $id_presence, 90, 100, 0.1, 0.2); + #mmm I should have a count of 1 + $count = Bcd::Data::Ads->get_count_activity_post_code_ads + ($stash, 1, 8012898); + + $self->assert_equals(1, $count); + #ok, now let's try to examine the db my $ad = Bcd::Data::Ads->get_ad_hash($stash, $ad_id); -- 2.11.4.GIT