App-Zapzi
view release on metacpan or search on metacpan
lib/App/Zapzi/Articles.pm view on Meta::CPAN
sub add_article
{
my %args = @_;
croak 'Must provide title, source and folder'
unless $args{title} && $args{source} && $args{folder};
my $folder_rs = get_folder($args{folder});
croak "Folder $args{folder} does not exist" unless $folder_rs;
my $new_article = _articles()->create({title => $args{title},
folder => $folder_rs->id,
source => $args{source},
article_text =>
{text => $args{text}}});
croak "Could not create article" unless $new_article;
return $new_article;
}
sub move_article
{
my ($id, $new_folder) = @_;
my $article = get_article($id);
croak 'Article does not exist' unless $article;
my $new_folder_rs = get_folder($new_folder);
croak "Folder $new_folder does not exist" unless $new_folder_rs;
if (! $article->update({folder => $new_folder_rs->id}))
{
croak 'Could not move article';
}
return 1;
}
sub delete_article
{
my ($id) = @_;
my $article = get_article($id);
# Ignore if the article does not exist
return 1 unless $article;
return $article->delete;
}
sub export_article
{
my $id = shift;
my $rs = get_article($id);
return unless $rs;
my $html = sprintf("<html><head><meta charset=\"utf-8\">\n" .
"<title>%s</title></head><body>%s</body></html>\n",
$rs->title, $rs->article_text->text);
return $html;
}
# Convenience function to get the DBIx::Class::ResultSet object for
# this table.
sub _articles
{
return App::Zapzi::get_app()->database->schema->resultset('Article');
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::Zapzi::Articles - routines to access Zapzi articles
=head1 VERSION
version 0.017
=head1 DESCRIPTION
These routines allow access to Zapzi articles via the database.
=head1 METHODS
=head2 get_articles(folder)
Returns a resultset of articles that are in C<$folder>.
=head2 get_article(id)
Returns the resultset for the article identified by C<id>.
=head2 articles_summary(folder)
Return a summary of articles in C<folder> as a list of articles, each
item being a hash ref with keys id, created, source, text and title.
=head2 add_article(args)
Adds a new article. C<args> is a hash that must contain
=over 4
=item * C<title> - title of the article
=item * C<source> - source, eg file or URL, of the article
=item * C<folder> - name of the folder to store it in
( run in 1.187 second using v1.01-cache-2.11-cpan-e93a5daba3e )