Created
September 21, 2011 10:49
-
-
Save aerith/1231786 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub insert_post { | |
my ($class, $c, $parameters) = @_; | |
my $post_data = { | |
$class->data_from_parameters($parameters), | |
published => \'now()', | |
created => \'now()', | |
modified => \'now()', | |
}; | |
$c->db->txn_begin; | |
my $post = $c->db->insert(posts => $post_data); | |
goto CREATE_ROLLBACK unless defined $post; | |
{ | |
$c->db->delete(post_tag_maps => { | |
post_id => $post->id | |
}); | |
my $post_tags = delete $parameters->{post_tags}; | |
my @post_tags = map { $_ =~ s{(^\s+|\s+$)}{}g; $_ } (split /\s+/, $post_tags); | |
foreach my $post_tag (@post_tags) { | |
my $tag = $c->db->single(post_tags => { name => $post_tag }); | |
unless (defined $tag) { | |
$tag = $c->db->insert(post_tags => { | |
name => $post_tag, | |
created => \'now()', | |
modified => \'now()', | |
}); | |
} | |
goto CREATE_ROLLBACK unless defined $tag; | |
{ | |
my $post_tag_map = $c->db->insert(post_tag_maps => { | |
post_tag_id => $tag->id, | |
post_id => $post->id, | |
created => \'now()', | |
modified => \'now()', | |
}); | |
goto CREATE_ROLLBACK unless defined $post_tag_map; | |
} | |
} | |
} | |
$c->db->txn_commit; | |
return $post; | |
CREATE_ROLLBACK: { | |
$c->db->txn_rollback; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment