Skip to content

Instantly share code, notes, and snippets.

@rymizuki
Created May 27, 2015 02:52
Show Gist options
  • Save rymizuki/74594316b9e603a4a49b to your computer and use it in GitHub Desktop.
Save rymizuki/74594316b9e603a4a49b to your computer and use it in GitHub Desktop.
app.psgi
```perl
use Amon2::Lite;
post '/api/user/', sub {
my ($c, $args) = @_;
my $params = $c->req->parameters;
my $user = $c->db->select_row('SELECT * FROM `user` FROM `name` = ?', [$params->{name}]);
return $c->create_response(409) if $user;
$c->db->query('INSERT INTO `user` (`name`, `email`) VALUES (?, ?)', [$params->{name}, $params->{email}]);
$user = $c->db->select_row('SELECT * FROM `user` FROM `name` = ?', [$params->{name}]);
return $c->render_json({
id => $user->{id},
name => $user->{name},
email => $user->{email},
});
};
__PACKAGE__->to_app;
```
commandline
```shell
mysqladmin -uroot create 'example'
mysql -uroot user -e 'CREATE table `user` (`id` interger unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(64) NOT NULL UNIQUE KEY, `email` varchar(64) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET utf8mb4';
plackup -a app.psgi
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment