Catalyst-Controller-Atompub
view release on metacpan or search on metacpan
samples/MyBlog/lib/Catalyst/Controller/Atompub/Collection.pm view on Meta::CPAN
# Retrieve Entries sorted in descending order
my $rs = $c->model('DBIC::Entries')->search({}, {
order_by => 'edited desc',
});
# Add Entries to the Feed
while (my $entry_resource = $rs->next) {
my $entry = XML::Atom::Entry->new(\$entry_resource->xml);
$feed->add_entry($entry);
}
# Return true on success
1;
}
# Create new Entry in the method with "Atompub(create)" attribute
sub create_entry :Atompub(create) {
my($self, $c) = @_;
# URI of the new Entry, which was determined by C::C::Atompub
my $uri = $self->entry_resource->uri;
# app:edited element, which was assigned by C::C::Atompub,
# is coverted into ISO 8601 format like '2007-01-01 00:00:00'
my $edited = $self->edited->iso;
# POSTed Entry (XML::Atom::Entry)
my $entry = $self->entry_resource->body;
# Create new Entry
$c->model('DBIC::Entries')->create({
uri => $uri,
edited => $edited,
xml => $entry->as_xml,
});
# Return true on success
1;
}
# Search the requested Entry in the method with "Atompub(read)"
# attribute
sub get_entry :Atompub(read) {
my($self, $c) = @_;
my $uri = $c->entry_resource->uri;
# Retrieve the Entry
my $rs = $c->model('DBIC::Entries')->find({ uri => $uri });
# Set the Entry
my $entry = XML::Atom::Entry->new(\$rs->xml);
$self->entry_resource->body($entry);
# Return true on success
1;
}
# Update the requested Entry in the method with "Atompub(update)"
# attribute
sub update_entry :Atompub(update) {
my($self, $c) = @_;
my $uri = $c->entry_resource->uri;
# app:edited element, which was assigned by C::C::Atompub,
# is coverted into ISO 8601 format like '2007-01-01 00:00:00'
my $edited = $self->edited->iso;
# PUTted Entry (XML::Atom::Entry)
my $entry = $self->entry_resource->body;
# Update the Entry
$c->model('DBIC::Entries')->find({ uri => $uri })->update({
uri => $uri,
edited => $edited,
xml => $entry->as_xml,
});
# Return true on success
1;
}
# Delete the requested Entry in the method with "Atompub(delete)"
# attribute
sub delete_entry :Atompub(delete) {
my($self, $c) = @_;
my $uri = $c->entry_resource->uri;
# Delete the Entry
$c->model('DBIC::Entries')->find({ uri => $uri })->delete;
# Return true on success
1;
}
# Access to http://localhost:3000/mycollection and get Feed Document
=head1 DESCRIPTION
Catalyst::Controller::Atompub::Collection provides the following features:
=over 4
=item * Pre-processing requests
L<Catalyst::Controller::Atompub::Collection> pre-processes the HTTP requests.
All you have to do is just writing CRUD operations in the subroutines
with I<Atompub> attribute.
=item * Media Resource support
Media Resources (binary data) as well as Entry Resources are supported.
A Media Link Entry, which has an I<atom:link> element to the newly created Media Resource,
is given by L<Catalyst::Controller::Atompub::Collection>.
=item * Media type check
L<Catalyst::Controller::Atompub::Collection> checks a media type of
( run in 0.679 second using v1.01-cache-2.11-cpan-39bf76dae61 )