annotate Lib/IMPL/Web/Application/Resource.pm @ 373:3ca44e23fd1f

implemented new web resource
author cin
date Wed, 25 Dec 2013 17:29:38 +0400
parents
children 6608db5dcb81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
1 package IMPL::Web::Application::Resource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
2 use strict;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
3
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
4 use constant {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
5 ResourceClass => __PACKAGE__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
6 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
7 use Scalar::Util qw(blessed);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
8
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
9 use IMPL::lang qw(:hash :base);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
10 use IMPL::Const qw(:prop);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
11 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
12 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
13 Exception => 'IMPL::Exception',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
14 OpException => '-IMPL::InvalidOperationException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
15 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
16 ResourceInterface => '-IMPL::Web::Application',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
17 Loader => 'IMPL::Code::Loader'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
18 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
19 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
20 'IMPL::Object' => undef,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
21 'IMPL::Web::Application::ResourceBase' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
22 ],
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
23 props => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
24 access => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
25 verbs => PROP_RW,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
26 children => PROP_RW
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
27 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
28 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
29
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
30 __PACKAGE__->static_accessor(verbNames => [qw(get post put delete options head)]);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
31 __PACKAGE__->static_accessor(httpMethodPrefix => 'Http');
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
32
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
33 sub CTOR {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
34 my ($this, %args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
35
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
36 my %verbs;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
37 my $httpPrefix = $this->httpMethodPrefix;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
38
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
39 foreach my $verb (@{$this->verbNames}) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
40 my $method = exists $args{$verb} ? $args{$verb} : $this->can($httpPrefix . ucfirst($verb));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
41 $verbs{$verb} = $method
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
42 if $method;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
43 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
44
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
45 hashApply(\%verbs,$args{verbs})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
46 if ref($args{verbs}) eq 'HASH' ;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
47
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
48 $this->children($args{children} || $this->GetChildResources());
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
49
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
50 $this->access($args{access})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
51 if $args{access};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
52
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
53 $this->verbs(\%verbs);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
54 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
55
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
56 sub _isInvokable {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
57 my ($this,$method) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
58
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
59 return
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
60 (blessed($method) and $method->can('Invoke')) ||
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
61 ref($method) eq 'CODE'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
62 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
63
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
64 sub _invoke {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
65 my ($this,$method,@args) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
66
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
67 if(blessed($method) and $method->can('Invoke')) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
68 return $method->Invoke($this,@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
69 } elsif(ref($method) eq 'CODE' || (not(ref($method)) and $this->can($method))) {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
70 return $this->$method(@args);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
71 } else {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
72 die OpException->new("Can't invoke the specified method: $method");
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
73 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
74 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
75
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
76 sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
77 shift->model;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
78 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
79
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
80 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
81 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
82
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
83 my $children = $this->children
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
84 or die NotFoundException->new( $this->location->url, $childId );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
85
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
86 if (ref($children) eq 'HASH') {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
87 return $children->{$childId};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
88 } else {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
89 return $this->_invoke($children,$childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
90 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
91 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
92
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
93 sub FetchChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
94 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
95
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
96 my $info = $this->Fetch($childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
97
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
98 return $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
99 if (is($info,ResourceInterface));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
100
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
101 return $this->CreateChildResource($info, $childId)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
102 if ref($info) eq 'HASH';
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
103
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
104 die OpException->new("Invalid resource description", $childId, $info);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
105 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
106
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
107 sub CreateChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
108 my ($this,$info, $childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
109
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
110 my $params = hashApply(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
111 {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
112 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
113 id => $childId,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
114 request => $this->request,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
115 class => ResourceClass
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
116 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
117 $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
118 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
119
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
120 $params->{model} = $this->_invoke($params->{model})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
121 if $this->_isInvokable($params->{model});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
122
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
123 my $factory = Loader->default->Require($params->{class});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
124
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
125 return $factory->new(%$params);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
126 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
127
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
128 sub GetChildResources {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
129 return {};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
130 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
131
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
132 1;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
133
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
134 __END__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
135
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
136 =pod
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
137
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
138 =head1 NAME
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
139
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
140 C<IMPL::Web::Application::Resource> - Ресурс C<REST> веб приложения
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
141
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
142 =head1 SYNOPSIS
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
143
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
144 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
145
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
146 use IMPL::require {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
147 Resource => 'IMPL::Web::Application::Resource',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
148 Security => 'IMPL::Security',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
149 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
150 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
151 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
152
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
153 my $model = Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
154 get => sub { },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
155 verbs => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
156 # non-standart verbs placed here
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
157 myverb => sub { }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
158 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
159 #child resources can be a hash
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
160 chidren => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
161 user => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
162 # a resource class may be specified optionally
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
163 # class => Resource,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
164 model => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
165 return Security->principal
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
166 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
167 # the default get implementation is implied
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
168 # get => sub { shift->model },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
169 access => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
170 my ($this,$verb) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
171 die ForbiddenException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
172 if Security->principal->isNobody
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
173 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
174 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
175 catalog => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
176 get => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
177 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
178
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
179 return $ctx->products->find_rs({ in_stock => 1 });
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
180 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
181 # chid resource may be created dynamically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
182 children => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
183 # binds model against the parent reource and id
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
184 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
185
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
186 ($id) = ($id =~ /^(\w+)$/)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
187 or die NotFoundException->new($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
188
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
189 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
190
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
191 my $item = $ctx->products->fetch($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
192
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
193 die NotFoundException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
194 unless $item;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
195
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
196 # return parameters for the new resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
197 return {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
198 model => $item,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
199 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
200 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
201 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
202 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
203 # dynamically binds whole child resource. The result of binding is
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
204 # the new resource or a hash with arguments to create one
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
205 posts => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
206 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
207
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
208 # this approach can be used to create a dynamic resource relaying
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
209 # on the type of the model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
210
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
211 return Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
212 id => $id,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
213 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
214 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
215 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
216
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
217 # ditto
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
218 # parent and id will be mixed in automagically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
219 # return { get => sub { shift->model} }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
220 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
221 post_only => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
222 get => undef, # remove GET verb implicitly
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
223 post => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
224 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
225 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
226 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
227 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
228 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
229
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
230 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
231
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
232 Альтернативный вариант для создания класса ресурса.
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
233
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
234 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
235
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
236 package MyResource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
237
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
238 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
239 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
240 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
241 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
242 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
243 'IMPL::Web::Application::Resource' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
244 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
245 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
246
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
247 sub ds {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
248 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
249
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
250 $this->context->{ds} ||= $this->application->ConnectDb();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
251 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
252
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
253 sub InvokeHttpVerb {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
254 my $this = shift;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
255
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
256 $this->ds->Begin();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
257
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
258 my $result = $this->next::method(@_);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
259
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
260 # in case of error the data context will be disposed and the transaction
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
261 # will be reverted
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
262 $this->ds->Commit();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
263
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
264 return $result;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
265 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
266
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
267 # this method is inherited by default
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
268 # sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
269 # shift->model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
270 #
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
271 # }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
272
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
273 sub HttpPost {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
274 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
275
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
276 my %data = map {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
277 $_,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
278 $this->request->param($_)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
279 } qw(name description value);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
280
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
281 die ForbiddenException->new("The item with the scpecified name can't be created'")
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
282 if(not $data{name} or $this->ds->items->find({ name => $data{name}))
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
283
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
284 $this->ds->items->insert(\%data);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
285
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
286 return $this->NoContent();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
287 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
288
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
289 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
290 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
291
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
292 my $item = $this->ds->items->find({name => $childId})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
293 or die NotFoundException->new();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
294
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
295 # return parameters for the child resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
296 return { model => $item, role => "item food" };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
297 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
298
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
299 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
300
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
301 =cut
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
302