Show
Ignore:
Timestamp:
02/08/08 02:44:39 (2 years ago)
Author:
eevee
Message:

Database refactoring. Renamed columns and tables to be more consistent and more readable. (#58)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • veekun/trunk/lib/Vee/Controller/Forum/Post.pm

    r327 r406  
    5151sub utils : LocalRegex('^(\d*)/(delete|undelete|nuke)') : Args(0) { 
    5252    my ( $self, $c ) = @_; 
    53     my ($postid, $method) = @{ $c->req->captures }; 
    54      
    55     my $forumid = undef; 
     53    my ($post_id, $method) = @{ $c->req->captures }; 
     54     
     55    my $forum_id = undef; 
    5656    # This is the post to be deleted, as well as the entire result set of things to be modified 
    57     my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $postid }, { prefetch => ['user', { 'thread' => 'forum' }], }) 
    58         or $c->vee_abort("There is no post with id of ", $postid, ". Perhaps another admin nuked it?"); 
    59     $c->vee_abort("The post with id of ", $postid, " has already been ", $method, "d.") 
     57    my $post = $c->model('DBIC::Posts')->find({ 'me.id' => $post_id }, { prefetch => ['user', { 'thread' => 'forum' }], }) 
     58        or $c->vee_abort("There is no post with id of ", $post_id, ". Perhaps another admin nuked it?"); 
     59    $c->vee_abort("The post with id of ", $post_id, " has already been ", $method, "d.") 
    6060        if (($post->flags =~ /deleted/ && $method eq 'delete') || ($post->flags !~ /deleted/ && $method eq 'undelete')); 
    6161    $c->vee_abort("You don't have permission to ", $method, " this post.") 
    62         unless (($c->user->obj->id == $post->userid && $method ne 'nuke') || $c->can_i("post$method")); 
     62        unless (($c->user->obj->id == $post->user_id && $method ne 'nuke') || $c->can_i("post$method")); 
    6363 
    6464    # Do everything in a transaction for rollback purposes 
     
    6666        my ($thread, $forum, $user) = ($post->thread, $post->thread->forum, $post->user); 
    6767        # If any of the post (or thread) counts are 0 or less, this will break. Of course, if that's the case, something is broken 
    68         return undef unless (($forum->postct >= 0 || $forum->threadct >= 0 || $thread->postct >= 0 || $user->postct >= 0) || ($method eq 'undelete')); 
    69         my $thread_postct = $c->model('DBIC::Posts')->count({ 'me.threadid' => $post->threadid, 'me.flags' => { '!=', 'deleted' } }); 
     68        return undef unless (($forum->post_count >= 0 || $forum->thread_count >= 0 || $thread->post_count >= 0 || $user->post_count >= 0) || ($method eq 'undelete')); 
     69        my $thread_postct = $c->model('DBIC::Posts')->count({ 'me.thread_id' => $post->thread_id, 'me.flags' => { '!=', 'deleted' } }); 
    7070        # Unfortunately, this must be done here, since this needs to include deleted posts as well 
    71         my ($lastpostid, $lasttime) = ((map {($_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'me.threadid' => $post->threadid }, { prefetch => 'thread', columns => [qw/me.id me.threadid thread.forumid/], order_by => 'me.time DESC' })), undef); 
    72  
    73         if ($method eq 'nuke') { $c->model('DBIC::Posts')->search({ 'me.id' => $postid})->delete; } 
     71        my ($last_post_id, $last_post_time) = ((map {($_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'me.thread_id' => $post->thread_id }, { prefetch => 'thread', columns => [qw/me.id me.thread_id thread.forum_id/], order_by => 'me.time DESC' })), undef); 
     72 
     73        if ($method eq 'nuke') { $c->model('DBIC::Posts')->search({ 'me.id' => $post_id })->delete; } 
    7474        else { toggleflag( $post, 'deleted' ) or return undef; }  
    7575 
    7676        # If there's only one post left, delete the thread as well 
    77         if ($thread_postct > 1 && $lastpostid == $post->id) { 
    78             ($lasttime, $lastpostid) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'me.threadid' => $post->threadid, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 
     77        if ($thread_postct > 1 && $last_post_id == $post->id) { 
     78            ($last_post_time, $last_post_id) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'me.thread_id' => $post->thread_id, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 
    7979        } elsif ($thread_postct <= 1) { 
    8080            #try moving this elsewhere later, i hate it here 
    81             if ($method eq 'nuke') { $c->model('DBIC::Threads')->search({ 'me.id' => $post->threadid })->delete; } 
     81            if ($method eq 'nuke') { $c->model('DBIC::Threads')->search({ 'me.id' => $post->thread_id })->delete; } 
    8282            elsif (($method eq 'undelete' && $thread_postct == 0) || ($method eq 'delete' && $thread_postct <= 1)) { 
    8383                toggleflag( $thread, 'deleted' ) or return undef; 
    8484            } 
    85             ($lasttime, $lastpostid) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forumid' => $forum->id, 'thread.flags' => { '!=', 'deleted' }, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 
    86             $forumid = $forum->id; 
    87             $lastpostid = 0 unless $lastpostid; 
     85            ($last_post_time, $last_post_id) = map {($_->time, $_->id)} $c->model('DBIC::Posts')->search({ 'thread.forum_id' => $forum->id, 'thread.flags' => { '!=', 'deleted' }, 'me.flags' => { '!=', 'deleted' } }, { prefetch => 'thread', rows => 1, order_by => 'me.time DESC' }); 
     86            $forum_id = $forum->id; 
     87            $last_post_id = 0 unless $last_post_id; 
    8888        } 
    8989             
    9090        if (($method eq 'nuke' && $post->flags !~ /deleted/) || ($method eq 'delete')) {  
    91             $thread->postct($thread->postct - 1); 
    92             $forum->postct($forum->postct - 1); 
    93             $forum->threadct($forum->threadct - 1) unless ($thread_postct > 1); 
    94             $user->postct($user->postct - 1); 
     91            $thread->post_count($thread->post_count - 1); 
     92            $forum->post_count($forum->post_count - 1); 
     93            $forum->thread_count($forum->thread_count - 1) unless ($thread_postct > 1); 
     94            $user->post_count($user->post_count - 1); 
    9595        } elsif ($method eq 'undelete') { 
    96             $thread->postct($thread->postct + 1); 
    97             $forum->postct($forum->postct + 1); 
    98             $forum->threadct($forum->threadct + 1) unless ($thread_postct > 1); 
    99             $user->postct($user->postct + 1); 
    100         } 
    101         # Only update lastpost and lasttime if this is actually the last post 
    102         if ($lasttime && $post->id == $thread->lastpostid) { 
    103             $thread->lastpostid($lastpostid); 
    104             $thread->lasttime($lasttime); 
     96            $thread->post_count($thread->post_count + 1); 
     97            $forum->post_count($forum->post_count + 1); 
     98            $forum->thread_count($forum->thread_count + 1) unless ($thread_postct > 1); 
     99            $user->post_count($user->post_count + 1); 
     100        } 
     101        # Only update last post and time if this is actually the last post 
     102        if ($last_post_time && $post->id == $thread->last_post_id) { 
     103            $thread->last_post_id($last_post_id); 
     104            $thread->last_post_time($last_post_time); 
    105105        } 
    106106        # Needs to be done here, in case the deleted post is the last post in a forum :( 
    107         $forum->lastpostid($lastpostid) if $post->id == $forum->lastpostid; 
     107        $forum->last_post_id($last_post_id) if $post->id == $forum->last_post_id; 
    108108        $thread->update if ($method ne 'nuke'); 
    109109        $forum->update; 
     
    114114 
    115115    $c->error("This error message should never be displayed unless someone is screwing with things; sorry.") unless $action; 
    116     $c->res->redirect("/forum/" . (($forumid) ? "$forumid" : "post/$postid")); 
     116    $c->res->redirect("/forum/" . (($forum_id) ? "$forum_id" : "post/$post_id")); 
    117117} 
    118118 
     
    125125sub edits : LocalRegex('^(\d*)/edits') { 
    126126    my ($self, $c) = @_; 
    127     my ($s, $postid) = ($c->stash, $c->req->captures->[0]); 
     127    my ($s, $post_id) = ($c->stash, $c->req->captures->[0]); 
    128128     
    129129    $c->vee_abort("You don't have permission to view post edits.") unless ($c->can_i('post_edits')); 
    130130 
    131     my $post   = $c->model('DBIC::Posts')->find({ 'me.id' => $postid}, { prefetch => [qw/user edits/, { thread => 'forum' }], order_by => 'edits.time DESC' }); 
     131    my $post   = $c->model('DBIC::Posts')->find({ 'me.id' => $post_id}, { prefetch => [qw/user edits/, { thread => 'forum' }], order_by => 'edits.time DESC' }); 
    132132     
    133133    $s->{post}      = $post; 
     
    143143 
    144144my $reply_fields = { 
    145     message => { type => 'textarea', rows => '10', cols => '100' }, 
     145    content => { type => 'textarea', rows => '10', cols => '100' }, 
    146146    id => { type => 'hidden' }, 
    147147}; 
     
    149149sub edit : LocalRegex('^(\d*)/edit') : Args(0) { 
    150150    my ($self, $c) = @_; 
    151     my ($s, $postid) = ($c->stash, $c->req->captures->[0]); 
    152      
    153     my $post_rs = $c->model('DBIC::Posts')->search({ 'me.id' => $postid }, { prefetch => 'user' })->single; 
     151    my ($s, $post_id) = ($c->stash, $c->req->captures->[0]); 
     152     
     153    my $post_rs = $c->model('DBIC::Posts')->search({ 'me.id' => $post_id }, { prefetch => 'user' })->single; 
    154154    $c->vee_abort("You must be logged in to edit posts.") unless $c->user; 
    155     $c->vee_abort("You don't have permission to edit posts.") unless ($c->can_i('post_edit') || $c->user->obj->id == $post_rs->userid); 
     155    $c->vee_abort("You don't have permission to edit posts.") unless ($c->can_i('post_edit') || $c->user->obj->id == $post_rs->user_id); 
    156156     
    157157    # Form submited stuff 
    158158    if ('post' eq lc $c->req->params->{submit}) { 
    159         my ($parsed_message, @bbcode_errors) = Vee::BBCode::validate_bbcode( $c->req->params->{message} ); 
     159        my ($parsed_message, @bbcode_errors) = Vee::BBCode::validate_bbcode( $c->req->params->{content} ); 
    160160        if (@bbcode_errors) { 
    161161            $c->vee_abort("Your post contains invalid bbcode.  Please go back and fix it."); 
     
    164164         
    165165        # If post wasn't edited, don't do anything further; results in redirect only 
    166         unless ($parsed_message eq $post_rs->message) { 
     166        unless ($parsed_message eq $post_rs->content) { 
    167167            my $post = $c->model('DBIC')->schema->txn_do( sub { 
    168168                my $edit = $c->model('DBIC::Edits')->create({ 
    169                     postid     => $post_rs->id, 
    170                     userid     => $c->user->obj->id, 
    171                     time       => time, 
    172                     oldmessage => $post_rs->message, 
     169                    post_id     => $post_rs->id, 
     170                    user_id     => $c->user->obj->id, 
     171                    time        => time, 
     172                    old_content => $post_rs->content, 
    173173                }); 
    174174                 
    175                 $post_rs->lasteditid( $edit->id ); 
    176                 $post_rs->message( $parsed_message ); 
     175                $post_rs->last_edit_id( $edit->id ); 
     176                $post_rs->content($parsed_message); 
    177177                $post_rs->update; 
    178178                 
     
    189189        copy_params => 1, 
    190190    ); 
    191     $form->force( id => $post_rs->threadid ); 
    192     $form->force( message => $c->req->params->{message} || $post_rs->message || '' ); 
     191    $form->force( id => $post_rs->thread_id ); 
     192    $form->force( content => $c->req->params->{content} || $post_rs->content || '' ); 
    193193     
    194194    $s->{page_header} = "Edit Post";