Permalink
Browse files

favorites api

  • Loading branch information...
1 parent a35f459 commit 465f24dc3d37e1b9885cece4db9f5faf20936a78 @monken monken committed Jul 16, 2011
@@ -0,0 +1,17 @@
+package MetaCPAN::Document::Favorite;
+use Moose;
+use ElasticSearchX::Model::Document;
+use MetaCPAN::Types qw(:all);
+use DateTime;
+use MetaCPAN::Util;
+
+has release_id => ( is => 'ro', required => 1, parent => 1, lazy_build => 1 );
+has [qw(author release user distribution)] => ( is => 'ro', required => 1 );
+has date => ( is => 'ro', isa => 'DateTime', default => sub { DateTime->now } );
+
+sub _build_release_id {
+ my $self = shift;
+ return MetaCPAN::Util::digest($self->author, $self->release);
+}
+
+__PACKAGE__->meta->make_immutable;
@@ -0,0 +1,16 @@
+package MetaCPAN::Server::Controller::Favorite;
+use Moose;
+BEGIN { extends 'MetaCPAN::Server::Controller' }
+
+sub index : Chained('/') : PathPart('favorite') : CaptureArgs(0) {
+}
+
+sub get : Chained('index') : PathPart('') : Args(1) {
+ my ( $self, $c, $id ) = @_;
+ eval {
+ $c->stash(
+ $c->model('CPAN::Favorite')->inflate(0)->get($id)->{_source} );
+ } or $c->detach('/not_found');
+}
+
+1;
@@ -0,0 +1,47 @@
+package MetaCPAN::Server::Controller::User::Favorite;
+
+use Moose;
+BEGIN { extends 'Catalyst::Controller::REST' }
+
+sub auto : Private {
+ my ( $self, $c ) = @_;
+ ( $c->stash->{pause} ) = $c->user->get_identities('pause');
+}
+
+sub index : Path : ActionClass('REST') {
+}
+
+sub index_POST {
+ my ( $self, $c ) = @_;
+ my $pause = $c->stash->{pause};
+ my $req = $c->req;
+ my $favorite = $c->model('CPAN::Favorite')->put(
+ { user => $pause->key,
+ author => $req->data->{author},
+ release => $req->data->{release},
+ distribution => $req->data->{distribution},
+ author => $req->data->{author},
+ },
+ { refresh => 1 }
+ );
+ $self->status_created(
+ $c,
+ location => $c->uri_for( '/favorite/' . $favorite->_id ),
+ entity => $favorite->meta->get_data($favorite)
+ );
+}
+
+sub index_DELETE {
+ my ( $self, $c, $id ) = @_;
+ my $favorite = $c->model('CPAN::Favorite')->get($id);
+ if ($favorite) {
+ $favorite->delete( { refresh => 1 } );
+ $self->status_ok( $c,
+ entity => $favorite->meta->get_data($favorite) );
+ }
+ else {
+ $self->status_not_found( $c, message => 'Entity could not be found' );
+ }
+}
+
+1;
@@ -2,7 +2,7 @@ package MetaCPAN::Server::Role::Request;
use Moose::Role;
-around header => sub {
+around [qw(content_type header)] => sub {
my ($orig, $self) = (shift,shift);
my $header = $self->$orig(@_);
return unless($header);
@@ -5,16 +5,20 @@ package MetaCPAN::Server::Test;
use strict;
use warnings;
use Plack::Test;
-use HTTP::Request::Common;
+use HTTP::Request::Common qw(POST GET DELETE);
use JSON::XS;
use base 'Exporter';
-our @EXPORT = qw(POST GET test_psgi app decode_json);
+our @EXPORT = qw(POST GET DELETE test_psgi app encode_json decode_json);
BEGIN { $ENV{METACPAN_SERVER_CONFIG_LOCAL_SUFFIX} = 'testing'; }
my $app = require MetaCPAN::Server;
-sub app { $app }
-
+MetaCPAN::Server->model('User::Account')->put(
+ { identity => [ { name => 'pause', key => 'MO' } ],
+ access_token => [ { client => 'testing', token => 'testing' } ]
+ }, { refresh => 1 }
+);
+sub app {$app}
1;
@@ -34,4 +38,4 @@ L<Plack::Test/test_psgi>
=head2 app
-Returns the L<MetaCPAN::Web> psgi app.
+Returns the L<MetaCPAN::Web> psgi app.
@@ -0,0 +1,35 @@
+
+use strict;
+use warnings;
+use Test::More;
+use MetaCPAN::Server::Test;
+
+test_psgi app, sub {
+ my $cb = shift;
+ ok( my $res = $cb->(
+ POST '/user/favorite?access_token=testing',
+ Content => encode_json(
+ { distribution => 'Moose',
+ release => 'Moose-1.10',
+ author => 'DOY'
+ }
+ )
+ ),
+ "POST favorite"
+ );
+ is($res->code, 201, 'status created');
+ ok(my $location = $res->header('location'), "location header set");
+ ok($res = $cb->( GET $location ), "GET $location");
+ is($res->code, 200, 'found');
+ my $json = decode_json($res->content);
+ (my $id = $location) =~ s/^.*\///;
+ is($json->{user}, 'MO', 'user is mo');
+ ok($res = $cb->( DELETE "/user/favorite/$id?access_token=testing" ), "DELETE $location");
+ is($res->code, 200, 'status ok');
+ ok($res = $cb->( GET "$location?access_token=testing" ), "GET $location");
+ is($res->code, 404, 'not found');
+
+
+};
+
+done_testing;

0 comments on commit 465f24d

Please sign in to comment.