fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-11-05 08:41 am

[dw-free] Implement v-gifts

[commit: http://hg.dwscoalition.org/dw-free/rev/660254fef85d]

http://bugs.dwscoalition.org/show_bug.cgi?id=215

Track how many times each vgift has been sold.

Patch by [personal profile] kareila.

Files modified:
  • bin/upgrading/update-db-general.pl
  • cgi-bin/DW/VirtualGift.pm
--------------------------------------------------------------------------------
diff -r 7cfdafa772e6 -r 660254fef85d bin/upgrading/update-db-general.pl
--- a/bin/upgrading/update-db-general.pl	Fri Nov 05 15:58:34 2010 +0800
+++ b/bin/upgrading/update-db-general.pl	Fri Nov 05 16:41:27 2010 +0800
@@ -32,6 +32,7 @@ CREATE TABLE vgift_ids (
     approved_why MEDIUMTEXT,
     description  MEDIUMTEXT,
     cost         INT UNSIGNED NOT NULL DEFAULT 0,
+    num_sold     INT UNSIGNED NOT NULL DEFAULT 0,
     mime_small   VARCHAR(255),
     mime_large   VARCHAR(255),
 
@@ -3918,6 +3919,10 @@ EOF
         do_alter( 'acctcode_promo', "ALTER TABLE acctcode_promo ADD COLUMN paid_months tinyint unsigned" );
     }
 
+    unless ( column_type( 'vgift_ids', 'num_sold' ) ) {
+        do_alter( 'vgift_ids', "ALTER TABLE vgift_ids ADD COLUMN num_sold INT UNSIGNED NOT NULL DEFAULT 0" );
+    }
+
     if ( $LJ::IS_DEV_SERVER ) {
         # strip constant definitions from user layers
         if ( table_relevant( "s2compiled2" ) && ! check_dbnote( "no_layer_constants" ) ) {
diff -r 7cfdafa772e6 -r 660254fef85d cgi-bin/DW/VirtualGift.pm
--- a/cgi-bin/DW/VirtualGift.pm	Fri Nov 05 15:58:34 2010 +0800
+++ b/cgi-bin/DW/VirtualGift.pm	Fri Nov 05 16:41:27 2010 +0800
@@ -18,7 +18,7 @@ use warnings;
 
 use constant PROPLIST => qw/ vgiftid name created_t creatorid active
                              approved approved_by approved_why
-                             custom featured description cost
+                             custom featured description cost num_sold
                              mime_small mime_large /;
 # NOTE: remember to update &validate if you add new props
 
@@ -88,8 +88,9 @@ sub create {
         # translate Perl nulls into MySQL nulls
         $vg{$_} = undef if exists $vg{$_} && $vg{$_} eq '';
     }
-    # don't allow created_t to be overridden
+    # don't allow created_t or num_sold to be overridden
     $vg{created_t} = time;
+    $vg{num_sold} = 0;
 
     # enforce active/approved defaults for new gifts
     if ( $vg{custom} && $vg{custom} eq 'Y' ) {
@@ -200,8 +201,8 @@ sub edit {
         # translate Perl nulls into MySQL nulls
         $vg{$_} = undef if exists $vg{$_} && $vg{$_} eq '';
     }
-    # don't allow created_t or vgiftid to be overridden
-    delete @vg{qw( created_t vgiftid )};
+    # don't allow created_t, vgiftid or num_sold to be overridden
+    delete @vg{qw( created_t vgiftid num_sold )};
 
     $vg{creatorid} = LJ::want_userid( $vg{creatorid} )
         if defined $vg{creatorid};
@@ -236,6 +237,25 @@ sub edit {
 
 sub mark_active   { $_[0]->edit( active => 'Y' ) }
 sub mark_inactive { $_[0]->edit( active => 'N' ) }
+
+sub mark_sold {
+    # this blindly increments, so not using the edit method
+    my ( $self ) = @_;
+    return undef unless $self->id;
+    my $key = 'num_sold';
+
+    my $dbh = LJ::get_db_writer();
+    $dbh->do( "UPDATE vgift_ids SET $key=$key+1 WHERE vgiftid=?",
+              undef, $self->id );
+    die $dbh->errstr if $dbh->err;
+
+    # update objects in memory
+    $self->{$key}++;
+    $self->_remove_from_memcache;  # LJ::MemCacheable
+    $self->_expire_relevant_keys( $key );
+
+    return $self;
+}
 
 sub tags {
     # taglist is a comma separated string of tagnames.
@@ -464,6 +484,7 @@ sub active { return $_[0]->_access( 'act
 sub active { return $_[0]->_access( 'active' )       || 'N' }
 sub custom { return $_[0]->_access( 'custom' )       || 'N' }
 sub featured { return $_[0]->_access( 'featured' )   || 'N' }
+sub num_sold { return $_[0]->_access( 'num_sold' )   ||  0  }
 sub creatorid { return $_[0]->_access( 'creatorid' ) ||  0  }
 sub created_t { return $_[0]->_access( 'created_t' ) }
 
@@ -601,7 +622,8 @@ sub can_be_deleted_by {
 sub can_be_deleted_by {
     my $self = shift;
 
-    # FIXME: if the vgift has been purchased, don't allow
+    # if the vgift has been purchased, don't allow
+    return 0 if $self->num_sold;
 
     # otherwise, same privileges as for edits
     return $self->can_be_edited_by( @_ );
@@ -774,6 +796,7 @@ sub validate {
     return $self->_valid_int( $val, $err, $key )  if $key eq 'vgiftid';
     return $self->_valid_int( $val, $err, $key )  if $key eq 'created_t';
     return $self->_valid_int( $val, $err, $key )  if $key eq 'cost';
+    return $self->_valid_int( $val, $err, $key )  if $key eq 'num_sold';
 
     # default case if no test defined for $key: assume invalid
     $$err = LJ::Lang::ml( 'vgift.error.validate.property', { key => $key } );
--------------------------------------------------------------------------------