c52e234d79e72f76b26c7010f0b0015a12e7df8b
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / RPC.pm
1 package Catalyst::Controller::DBIC::API::RPC;
2 #ABSTRACT: Provides an RPC interface to DBIx::Class
3
4 use Moose;
5 BEGIN { extends 'Catalyst::Controller::DBIC::API'; }
6
7 __PACKAGE__->config(
8     'action'    => { object_with_id => { PathPart => 'id' } },
9     'default'   => 'application/json',
10     'stash_key' => 'response',
11     'map'       => {
12         'application/x-www-form-urlencoded' => 'JSON',
13         'application/json'                  => 'JSON',
14     },
15 );
16
17 =head1 DESCRIPTION
18
19 Provides an RPC API interface to the functionality described in L<Catalyst::Controller::DBIC::API>.
20
21 By default provides the following endpoints:
22
23   $base/create
24   $base/list
25   $base/id/[identifier]
26   $base/id/[identifier]/delete
27   $base/id/[identifier]/update
28
29 Where $base is the URI described by L</setup>, the chain root of the controller.
30
31 =method_protected setup
32
33 Chained: override
34 PathPart: override
35 CaptureArgs: 0
36
37 As described in L<Catalyst::Controller::DBIC::API/setup>, this action is the chain root of the controller but has no pathpart or chain parent defined by default, so these must be defined in order for the controller to function. The neatest way is normally to define these using the controller's config.
38
39   __PACKAGE__->config
40     ( action => { setup => { PathPart => 'track', Chained => '/api/rpc/rpc_base' } },
41         ...
42   );
43
44 =cut
45
46 =method_protected index
47
48 Chained: L</setup>
49 PathPart: ''
50 Args: 0
51
52 Returns http status code 404 by default.
53
54 =cut
55
56 sub index : Chained('setup') PathPart('') Args(0) {
57         my ( $self, $c ) = @_;
58
59         $self->push_error($c, { message => 'Not implemented' });
60         $c->res->status( '404' );
61 }
62
63 =method_protected create
64
65 Chained: L</objects_no_id>
66 PathPart: create
67 CaptureArgs: 0
68
69 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/update_or_create>.
70
71 =cut
72
73 sub create :Chained('objects_no_id') :PathPart('create') :Args(0)
74 {
75         my ($self, $c) = @_;
76     $self->update_or_create($c);
77 }
78
79 =method_protected list
80
81 Chained: L</deserialize>
82 PathPart: list
83 CaptureArgs: 0
84
85 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/list>.
86
87 =cut
88
89 sub list :Chained('deserialize') :PathPart('list') :Args(0)
90 {
91         my ($self, $c) = @_;
92     $self->next::method($c);
93 }
94
95 =method_protected item
96
97 Chained: L</object_with_id>
98 PathPart: ''
99 Args: 0
100
101 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/item>.
102
103 =cut
104
105 sub item :Chained('object_with_id') :PathPart('') :Args(0)
106 {
107     my ($self, $c) = @_;
108     $self->next::method($c);
109 }
110
111 =method_protected update
112
113 Chained: L</object_with_id>
114 PathPart: update
115 Args: 0
116
117 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/update_or_create>.
118
119 =cut
120
121 sub update :Chained('object_with_id') :PathPart('update') :Args(0)
122 {
123     my ($self, $c) = @_;
124     $self->update_or_create($c);
125 }
126
127 =method_protected delete
128
129 Chained: L</object_with_id>
130 PathPart: delete
131 Args: 0
132
133 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/delete>.
134
135 =cut
136
137 sub delete :Chained('object_with_id') :PathPart('delete') :Args(0)
138 {
139     my ($self, $c) = @_;
140     $self->next::method($c);
141 }
142
143 =method_protected update_bulk
144
145 Chained: L</objects_no_id>
146 PathPart: update
147 Args: 0
148
149 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/update_or_create> for multiple objects.
150
151 =cut
152
153 sub update_bulk :Chained('objects_no_id') :PathPart('update') :Args(0)
154 {
155     my ($self, $c) = @_;
156     $self->update_or_create($c);
157 }
158
159 =method_protected delete_bulk
160
161 Chained: L</objects_no_id>
162 PathPart: delete
163 Args: 0
164
165 Provides an endpoint to the functionality described in L<Catalyst::Controller::DBIC::API/delete> for multiple objects.
166
167 =cut
168
169 sub delete_bulk :Chained('objects_no_id') :PathPart('delete') :Args(0)
170 {
171     my ($self, $c) = @_;
172     $self->delete($c);
173 }
174
175 1;