20. Mojoliciousでアプリを作る
lib/Sojorsojor.pm
package Sojorsojor;
use Mojo::Base 'Mojolicious';
# This method will run once at server start
sub startup { lib/Sojorsojor/Api/
my $self = shift; Friend.pm
の
# Router list
my $r = $self-‐‑‒>routes; を実⾏行行する
# Normal route to controller
$r-‐‑‒>get('/api/friend')-‐‑‒>to(ʼ’api-‐‑‒friend#list');
}
1;
29. Mojoliciousでアプリを作る
lib/Api/Base.pm
package Itemloupe::Api::Base;
use strict;
use warnings;
use base 'Mojolicious::Controller';
use Itemloupe::Session;
# APIのアウトプットとなるJSONのパターンを定義
sub api_̲output {
my ($self, $data, $error, $type) = @_̲;
〜~〜~処理理を書く〜~〜~
$self-‐‑‒>render('json' => $output);
}
1;
30. Mojoliciousでアプリを作る
lib/Api/Activity.pm
package Itemloupe::Api::Activity;
use strict;
use warnings;
コピペが減って
use base 'Itemloupe::Api::Base';
use Itemloupe::Model::Activity; すっきり
sub get {
〜~〜~〜~〜~〜~中略略〜~〜~〜~〜~〜~
# activity get
my $activity = new Itemloupe::Model::Activity;
my $result = $activity-‐‑‒>get($self-‐‑‒>uid, $uid, $p-‐‑‒
>{'type'});
# output
$self-‐‑‒>api_̲output($result, $activity-‐‑‒>get_̲error);
}