Skip to content

Commit

Permalink
Use Roles to implement starring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talina06 committed Jul 24, 2014
1 parent dbdb639 commit 1597f44
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
37 changes: 2 additions & 35 deletions lib/MetaCPAN/Server/Controller/User/Stargazer.pm
Expand Up @@ -7,6 +7,8 @@ use Moose;

BEGIN { extends 'Catalyst::Controller::REST' }

with 'MetaCPAN::Server::Role::MapStarring';

sub auto : Private {
my ( $self, $c ) = @_;
unless ( $c->user->looks_human ) {
Expand All @@ -20,39 +22,4 @@ sub auto : Private {
sub index : Path : ActionClass('REST') {
}

sub index_POST {
my ( $self, $c ) = @_;
my $req = $c->req;
my $star = $c->model('CPAN::Stargazer')->put(
{
user => $c->user->id,
author => $req->data->{author},
release => $req->data->{release},
module => $req->data->{module},
author => $req->data->{author},
},
{ refresh => 1 }
);
$self->status_created(
$c,
location => $c->uri_for(
join( q{/}, '/stargazer', $star->user, $star->module )
),
entity => $star->meta->get_data($star)
);
}

sub index_DELETE {
my ( $self, $c, $module ) = @_;
my $star = $c->model('CPAN::Stargazer')
->get( { user => $c->user->id, module => $module } );
if ($star) {
$star->delete( { refresh => 1 } );
$self->status_ok( $c, entity => $star->meta->get_data($star) );
}
else {
$self->status_not_found( $c, message => 'Entity could not be found' );
}
}

1;
43 changes: 43 additions & 0 deletions lib/MetaCPAN/Server/Role/MapStarring.pm
@@ -0,0 +1,43 @@
package MetaCPAN::Server::Role::MapStarring;

This comment has been minimized.

Copy link
@Talina06

Talina06 Jul 24, 2014

Author Contributor

@oalders : Did I do this right? Please let me know if the implementation needs to be changed.


use strict;
use warnings;

use Moose::Role;

sub index_POST {
my ( $self, $c ) = @_;
my $req = $c->req;
my $star = $c->model('CPAN::Stargazer')->put(
{
user => $c->user->id,
author => $req->data->{author},
release => $req->data->{release},
module => $req->data->{module},
author => $req->data->{author},
},
{ refresh => 1 }
);
$self->status_created(
$c,
location => $c->uri_for(
join( q{/}, '/stargazer', $star->user, $star->module )
),
entity => $star->meta->get_data($star)
);
}

sub index_DELETE {
my ( $self, $c, $module ) = @_;
my $star = $c->model('CPAN::Stargazer')
->get( { user => $c->user->id, module => $module } );
if ($star) {
$star->delete( { refresh => 1 } );
$self->status_ok( $c, entity => $star->meta->get_data($star) );
}
else {
$self->status_not_found( $c, message => 'Entity could not be found' );
}
}

1;

0 comments on commit 1597f44

Please sign in to comment.