annotate Lib/IMPL/Web/Application/Resource.pm @ 374:6608db5dcb81

sync
author cin
date Thu, 09 Jan 2014 19:40:33 +0400
parents 3ca44e23fd1f
children 441e84031c7b
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};
374
cin
parents: 373
diff changeset
88 } elsif($this->_isInvokable($children)) {
cin
parents: 373
diff changeset
89 return $this->_invoke($children,$childId);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
90 } else {
374
cin
parents: 373
diff changeset
91 die OpException->new("Invalid resource description", $childId, $children);
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
92 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
93 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
94
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
95 sub FetchChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
96 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
97
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
98 my $info = $this->Fetch($childId);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
99
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
100 return $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
101 if (is($info,ResourceInterface));
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
102
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
103 return $this->CreateChildResource($info, $childId)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
104 if ref($info) eq 'HASH';
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
105
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
106 die OpException->new("Invalid resource description", $childId, $info);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
107 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
108
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
109 sub CreateChildResource {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
110 my ($this,$info, $childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
111
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
112 my $params = hashApply(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
113 {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
114 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
115 id => $childId,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
116 request => $this->request,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
117 class => ResourceClass
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
118 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
119 $info
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
120 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
121
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
122 $params->{model} = $this->_invoke($params->{model})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
123 if $this->_isInvokable($params->{model});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
124
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
125 my $factory = Loader->default->Require($params->{class});
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
126
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
127 return $factory->new(%$params);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
128 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
129
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
130 sub GetChildResources {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
131 return {};
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
132 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
133
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
134 1;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
135
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
136 __END__
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
137
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
138 =pod
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
139
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
140 =head1 NAME
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
141
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
142 C<IMPL::Web::Application::Resource> - Ресурс C<REST> веб приложения
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
143
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
144 =head1 SYNOPSIS
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
145
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
146 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
147
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
148 use IMPL::require {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
149 Resource => 'IMPL::Web::Application::Resource',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
150 Security => 'IMPL::Security',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
151 NotFoundException => 'IMPL::Web::NotFoundException',
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
152 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
153 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
154
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
155 my $model = Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
156 get => sub { },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
157 verbs => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
158 # non-standart verbs placed here
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
159 myverb => sub { }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
160 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
161 #child resources can be a hash
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
162 chidren => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
163 user => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
164 # a resource class may be specified optionally
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
165 # class => Resource,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
166 model => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
167 return Security->principal
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
168 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
169 # the default get implementation is implied
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
170 # get => sub { shift->model },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
171 access => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
172 my ($this,$verb) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
173 die ForbiddenException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
174 if Security->principal->isNobody
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
175 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
176 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
177 catalog => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
178 get => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
179 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
180
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
181 return $ctx->products->find_rs({ in_stock => 1 });
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
182 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
183 # chid resource may be created dynamically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
184 children => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
185 # binds model against the parent reource and id
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
186 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
187
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
188 ($id) = ($id =~ /^(\w+)$/)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
189 or die NotFoundException->new($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
190
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
191 my $ctx = shift->application->ConnectDb()->AutoPtr();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
192
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
193 my $item = $ctx->products->fetch($id);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
194
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
195 die NotFoundException->new()
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
196 unless $item;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
197
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
198 # return parameters for the new resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
199 return {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
200 model => $item,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
201 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
202 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
203 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
204 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
205 # dynamically binds whole child resource. The result of binding is
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
206 # the new resource or a hash with arguments to create one
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
207 posts => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
208 my ($this,$id) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
209
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
210 # this approach can be used to create a dynamic resource relaying
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
211 # on the type of the model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
212
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
213 return Resource->new(
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
214 id => $id,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
215 parent => $this,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
216 get => sub { shift->model }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
217 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
218
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
219 # ditto
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
220 # parent and id will be mixed in automagically
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
221 # return { get => sub { shift->model} }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
222 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
223 post_only => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
224 get => undef, # remove GET verb implicitly
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
225 post => sub {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
226 my ($this) = @_;
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 );
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
231
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
232 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
233
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
234 Альтернативный вариант для создания класса ресурса.
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
235
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
236 =begin code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
237
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
238 package MyResource;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
239
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
240 use IMPL::declare {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
241 require => {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
242 ForbiddenException => 'IMPL::Web::ForbiddenException'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
243 },
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
244 base => [
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
245 'IMPL::Web::Application::Resource' => '@_'
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
246 ]
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
247 };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
248
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
249 sub ds {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
250 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
251
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
252 $this->context->{ds} ||= $this->application->ConnectDb();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
253 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
254
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
255 sub InvokeHttpVerb {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
256 my $this = shift;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
257
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
258 $this->ds->Begin();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
259
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
260 my $result = $this->next::method(@_);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
261
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
262 # in case of error the data context will be disposed and the transaction
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
263 # will be reverted
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
264 $this->ds->Commit();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
265
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
266 return $result;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
267 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
268
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
269 # this method is inherited by default
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
270 # sub HttpGet {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
271 # shift->model
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
272 #
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
273 # }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
274
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
275 sub HttpPost {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
276 my ($this) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
277
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
278 my %data = map {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
279 $_,
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
280 $this->request->param($_)
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
281 } qw(name description value);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
282
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
283 die ForbiddenException->new("The item with the scpecified name can't be created'")
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
284 if(not $data{name} or $this->ds->items->find({ name => $data{name}))
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
285
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
286 $this->ds->items->insert(\%data);
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
287
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
288 return $this->NoContent();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
289 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
290
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
291 sub Fetch {
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
292 my ($this,$childId) = @_;
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
293
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
294 my $item = $this->ds->items->find({name => $childId})
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
295 or die NotFoundException->new();
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
296
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
297 # return parameters for the child resource
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
298 return { model => $item, role => "item food" };
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
299 }
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
300
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
301 =end code
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
302
374
cin
parents: 373
diff changeset
303 =head1 MEMBERS
cin
parents: 373
diff changeset
304
cin
parents: 373
diff changeset
305 =head2 C<[virtual]Fetch($childId)>
cin
parents: 373
diff changeset
306
cin
parents: 373
diff changeset
307 Метод для получения дочернего ресурса.
cin
parents: 373
diff changeset
308
cin
parents: 373
diff changeset
309 Возвращает параметры для создания дочернего ресурса, либо уже созданный ресурс.
cin
parents: 373
diff changeset
310 Создание дочернего ресурса происходит при помощи метода C<CreateChildResource()>
cin
parents: 373
diff changeset
311 который добавляет недостающие параметры к возвращенным в данным методом и
cin
parents: 373
diff changeset
312 создает новый ресурс
cin
parents: 373
diff changeset
313
cin
parents: 373
diff changeset
314 =head2 C<>
cin
parents: 373
diff changeset
315
373
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
316 =cut
3ca44e23fd1f implemented new web resource
cin
parents:
diff changeset
317