Last active
August 29, 2015 13:56
-
-
Save willert/8917364 to your computer and use it in GitHub Desktop.
Uesers
This file contains hidden or 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
package Wellmax::Controller::Users; | |
use Moose; | |
use namespace::autoclean; | |
use Data::Dumper; | |
BEGIN { extends 'Catalyst::Controller' } | |
__PACKAGE__->config( | |
action => { | |
'*' => { | |
Consumes => 'JSON', | |
Path => '', | |
} | |
} | |
); | |
sub end :Private { | |
my ( $self, $ctx ) = @_; | |
$ctx->forward($ctx->view('JSON')); | |
} | |
sub error :Private { | |
my ( $self, $ctx, $code, $reason ) = @_; | |
$reason ||= 'Unknown Error'; | |
$code ||= 500; | |
$ctx->res->status($code); | |
$ctx->stash->{error} = $reason; | |
} | |
# list of all users | |
sub list_users :GET Args(0) { | |
my ( $self, $ctx ) = @_; | |
my $rs = $ctx->model->resultset('Users'); | |
my @users; | |
while (my $user = $rs->next) { | |
push @users, $user->TO_JSON; | |
} | |
$ctx->stash->{data} = \@users; | |
} | |
sub user : ChainedArgs(1) { | |
my ( $self, $ctx, $username ) = @_; | |
my $user = $ctx->model->resultset('Users')->find($username); | |
$ctx->detach('error', [404, "User does not exist: $username"]) unless $user; | |
$ctx->stash( user => $user ); | |
} | |
# fetch a specific user | |
sub retrieve_user : Chained(user) GET Args(0) { | |
my ( $self, $ctx ) = @_; | |
my $user = $ctx->stash->{user}; | |
$ctx->stash->{data} = $user->TO_JSON; | |
} | |
# fetch a specific user | |
sub user_address : Chained(user) GET PathPart(address) Args(0) { | |
my ( $self, $ctx ) = @_; | |
my $user = $ctx->stash->{user}; | |
my $address = $user->address; | |
$ctx->detach('error', [404, "User's address unknown: $username"]) unless $address; | |
$ctx->stash->{data} = $address->TO_JSON; | |
} | |
# create a new user | |
sub create_user :POST Args(0) { | |
my ( $self, $ctx ) = @_; | |
my $data = $ctx->req->body_data; | |
my $user = $ctx->model->resultset('Users')->create($data); | |
$ctx->detach('error', [400, "Couldn't create new user"]) unless $user; | |
$ctx->log->info('Hello, this is Log4Perl, you just made a new user!'); | |
$ctx->stash->{data} = $user; | |
$ctx->res->location("/users/$user"); | |
} | |
# update a specific user | |
sub update_user :POST Args(1) { | |
my ( $self, $ctx, $existing_user ) = @_; | |
my $data = $ctx->req->body_data; | |
my $user = $ctx->model->resultset('Users')->find($existing_user); | |
$ctx->detach('error', [400, "Can't find user: $existing_user"]) unless $existing_user; | |
my $updated_user = $user->update($data); | |
$ctx->detach('error', [400, "Fail to update user: $existing_user"]) unless $updated_user; | |
$ctx->stash->{data} = $updated_user; | |
} | |
# delete a specific user | |
sub delete_user :DELETE Args(1) { | |
my ( $self, $ctx, $username ) = @_; | |
my $user = $ctx->model->resultset('Users')->find($username); | |
my $ok = $user->delete(); | |
$ctx->detach('error', [400, "Invalid user: $user"]) unless $ok; | |
$ctx->stash->{data} = $ok; | |
} | |
# useful filters - all subsets of "all users" | |
# generic filter "count" to limit returns &count= | |
# all users ordered by alphabet - filter by letter | |
# ?filter=alpha&range=B | |
# all users ordered by date of creation - filter by date, year etc | |
# ?filter=date&start_date=&end_date= | |
# all users by health criteria | |
# ?filter=health&criteria=diabetes | |
# all users by gender, city, age ... | |
# ?filter=gender&gender=f ?filter=city&city=Berlin | |
# no routes for: | |
# delete all users ;) | |
# bulk update lots of users (excel/csv import?) | |
# bulk create lots of users (excel/csv import?) | |
__PACKAGE__->meta->make_immutable; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment