| 1 | package Magpie::Resource::DBIC; | ||
| 2 | $Magpie::Resource::DBIC::VERSION = '1.163200'; | ||
| 3 | # ABSTRACT: Resource implementation for DBIx::Class ResultSources. | ||
| 4 | |||
| 5 | use Moose; | ||
| 6 | extends 'Magpie::Resource'; | ||
| 7 | with 'Magpie::Plugin::DBI'; | ||
| 8 | use Class::Load; | ||
| 9 | use Magpie::Constants; | ||
| 10 | use Try::Tiny; | ||
| 11 | use Data::Printer; | ||
| 12 | |||
| 13 | has data_source => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'DBIx::Class::Schema', | ||
| 16 | lazy_build => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has result_class => ( | ||
| 20 | isa => "Str", | ||
| 21 | is => "ro", | ||
| 22 | #required => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has schema_class => ( | ||
| 26 | isa => "Str", | ||
| 27 | is => "ro", | ||
| 28 | ); | ||
| 29 | |||
| 30 | has typemap => ( | ||
| 31 | isa => "KiokuDB::TypeMap", | ||
| 32 | is => "ro", | ||
| 33 | predicate => "has_typemap", | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub _build_data_source { | ||
| 38 | my $self = shift; | ||
| 39 | my $k = undef; | ||
| 40 | |||
| 41 | try { | ||
| 42 | $k = $self->resolve_asset( service => 'dbic_schema' ); | ||
| 43 | } | ||
| 44 | catch { | ||
| 45 | warn "NOPE " . $_; | ||
| 46 | try { | ||
| 47 | my $schema_class = $self->schema_class; | ||
| 48 | $k = $schema_class->connect( @{ $self->_connect_args } ); | ||
| 49 | } | ||
| 50 | catch { | ||
| 51 | my $error = "Could not connect to DBIC data source: $_\n"; | ||
| 52 | warn $error; | ||
| 53 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 54 | }; | ||
| 55 | }; | ||
| 56 | |||
| 57 | return undef if $self->has_error; | ||
| 58 | return $k; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub GET { | ||
| 62 | my $self = shift; | ||
| 63 | $self->parent_handler->resource($self); | ||
| 64 | my $req = $self->request; | ||
| 65 | |||
| 66 | my $path = $req->path_info; | ||
| 67 | my $id = $self->get_entity_id; | ||
| 68 | |||
| 69 | if ($path =~ /\/$/ && !$id ) { | ||
| 70 | $self->state('prompt'); | ||
| 71 | return OK; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $data = undef; | ||
| 75 | |||
| 76 | try { | ||
| 77 | ($data) = $self->data_source->resultset($self->result_class)->find($id); | ||
| 78 | } | ||
| 79 | catch { | ||
| 80 | my $error = "Could not GET data from DBIC data source: $_\n"; | ||
| 81 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 82 | }; | ||
| 83 | |||
| 84 | return OK if $self->has_error; | ||
| 85 | |||
| 86 | unless ($data) { | ||
| 87 | $self->set_error({ status_code => 404, reason => 'Resource not found.'}); | ||
| 88 | return OK; | ||
| 89 | } | ||
| 90 | |||
| 91 | #warn "got data " . p($data); | ||
| 92 | |||
| 93 | $self->data($data); | ||
| 94 | return OK; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub POST { | ||
| 98 | my $self = shift; | ||
| 99 | my $req = $self->request; | ||
| 100 | |||
| 101 | my $to_store = undef; | ||
| 102 | |||
| 103 | my $result_class = $self->result_class; | ||
| 104 | |||
| 105 | # XXX should check for a content body first. | ||
| 106 | my $args = {}; | ||
| 107 | |||
| 108 | if ( $self->has_data ) { | ||
| 109 | $args = $self->data; | ||
| 110 | #warn "HAS DATA " . p($args); | ||
| 111 | $self->clear_data; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | for ( $req->param ) { | ||
| 115 | $args->{$_} = $req->param($_); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | # permit POST to update if there's an entity ID. | ||
| 120 | # XXX: Should this go in an optional Role? | ||
| 121 | if (my $existing_id = $self->get_entity_id) { | ||
| 122 | my $existing = undef; | ||
| 123 | try { | ||
| 124 | ($existing) = $self->data_source->resultset($result_class)->find($existing_id); | ||
| 125 | } | ||
| 126 | catch { | ||
| 127 | my $error = "Could not fetch data from DBIC data source for POST editing with entity with ID '$existing_id': $_\n"; | ||
| 128 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 129 | }; | ||
| 130 | |||
| 131 | return OK if $self->has_error; | ||
| 132 | |||
| 133 | if ($existing) { | ||
| 134 | foreach my $key (keys(%{$args})) { | ||
| 135 | $existing->$key( $args->{$key} ); | ||
| 136 | } | ||
| 137 | |||
| 138 | try { | ||
| 139 | $existing->update; | ||
| 140 | } | ||
| 141 | catch { | ||
| 142 | my $error = "Error updating data entity with ID $existing_id: $_\n"; | ||
| 143 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 144 | }; | ||
| 145 | |||
| 146 | return OK if $self->has_error; | ||
| 147 | |||
| 148 | # finally, if it all went OK, say so. | ||
| 149 | $self->state('updated'); | ||
| 150 | $self->response->status(204); | ||
| 151 | return OK; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # if we make it here there is no existing record, so make a new one. | ||
| 156 | my $id = undef; | ||
| 157 | |||
| 158 | try { | ||
| 159 | $to_store = $self->data_source->resultset($result_class)->create($args); | ||
| 160 | $id = $to_store->id; | ||
| 161 | } | ||
| 162 | catch { | ||
| 163 | my $error = "Could not store POST data in DBIC data source: $_\n"; | ||
| 164 | warn $error; | ||
| 165 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 166 | }; | ||
| 167 | |||
| 168 | return DECLINED if $self->has_error; | ||
| 169 | |||
| 170 | # XXX: all of this needs to go in an abstract object downstream serializer | ||
| 171 | # can figure stuff out | ||
| 172 | my $path = $req->path_info; | ||
| 173 | $path =~ s|^/||; | ||
| 174 | $path =~ s|/$||; | ||
| 175 | $self->state('created'); | ||
| 176 | $self->response->status(201); | ||
| 177 | $self->response->header( 'Location' => $req->base . $path . "/$id" ); | ||
| 178 | return OK; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub DELETE { | ||
| 182 | my $self = shift; | ||
| 183 | $self->parent_handler->resource( $self ); | ||
| 184 | my $req = $self->request; | ||
| 185 | |||
| 186 | my $ds = $self->data_source; | ||
| 187 | my $id = $self->get_entity_id; | ||
| 188 | my $existing = undef; | ||
| 189 | |||
| 190 | unless ($id) { | ||
| 191 | my $error = "DELETE request requires and entity ID.\n"; | ||
| 192 | $self->set_error({ status_code => 500, reason => $error }); | ||
| 193 | } | ||
| 194 | |||
| 195 | try { | ||
| 196 | ($existing) = $ds->resultset($self->result_class)->find($id); | ||
| 197 | }; | ||
| 198 | |||
| 199 | unless ($existing) { | ||
| 200 | my $error = "Resource not found for ID '$id'.\n"; | ||
| 201 | $self->set_error({ status_code => 404, reason => $error }); | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | try { | ||
| 206 | $existing->delete; | ||
| 207 | } | ||
| 208 | catch { | ||
| 209 | my $error = "Could not delete data from DBIC data source: $_\n"; | ||
| 210 | $self->set_error({ status_code => 500, reason => $error }); | ||
| 211 | }; | ||
| 212 | |||
| 213 | return OK if $self->has_error; | ||
| 214 | $self->state('deleted'); | ||
| 215 | $self->response->status(204); | ||
| 216 | return OK; | ||
| 217 | } | ||
| 218 | |||
| 219 | before [HTTP_METHODS] => sub { | ||
| 220 | my $self = shift; | ||
| 221 | $self->parent_handler->resource($self); | ||
| 222 | }; | ||
| 223 | |||
| 224 | sub PUT { | ||
| 225 | my $self = shift; | ||
| 226 | my $req = $self->request; | ||
| 227 | my $schema = $self->data_source; | ||
| 228 | my $to_store = undef; | ||
| 229 | |||
| 230 | my $wrapper_class = $self->wrapper_class; | ||
| 231 | |||
| 232 | # XXX should check for a content body first. | ||
| 233 | my %args = (); | ||
| 234 | |||
| 235 | if ( $self->has_data ) { | ||
| 236 | %args = %{ $self->data }; | ||
| 237 | $self->clear_data; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | for ( $req->param ) { | ||
| 241 | $args{$_} = $req->param($_); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | my $existing_id = $self->get_entity_id; | ||
| 246 | |||
| 247 | unless ($existing_id) { | ||
| 248 | $self->set_error({ | ||
| 249 | status_code => 400, | ||
| 250 | reason => "Attempt to PUT without a definable entity ID." | ||
| 251 | }); | ||
| 252 | return DONE; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | my $existing = undef; | ||
| 257 | try { | ||
| 258 | $existing = $schema->resultset($self->result_class)->find($existing_id); | ||
| 259 | } | ||
| 260 | catch { | ||
| 261 | my $error = "Could not fetch data from DBIC data source for PUT editing if entity with ID $existing_id: $_\n"; | ||
| 262 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 263 | }; | ||
| 264 | |||
| 265 | return OK if $self->has_error; | ||
| 266 | |||
| 267 | unless ($existing) { | ||
| 268 | $self->set_error(404); | ||
| 269 | return DONE; | ||
| 270 | } | ||
| 271 | |||
| 272 | my $existing_obj = $existing->as_obj; | ||
| 273 | foreach my $key (keys(%args)) { | ||
| 274 | try { | ||
| 275 | $existing_obj->$key( $args{$key} ); | ||
| 276 | } | ||
| 277 | catch { | ||
| 278 | my $error = "Error updating property '$key' of Resource ID $existing_id: $_\n"; | ||
| 279 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 280 | last; | ||
| 281 | }; | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | return OK if $self->has_error; | ||
| 286 | |||
| 287 | try { | ||
| 288 | $self->data_source->txn_do(sub { | ||
| 289 | $existing->update($existing_obj->to_storage); | ||
| 290 | }); | ||
| 291 | } | ||
| 292 | catch { | ||
| 293 | my $error = "Error updating data entity with ID $existing_id: $_\n"; | ||
| 294 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 295 | }; | ||
| 296 | |||
| 297 | return OK if $self->has_error; | ||
| 298 | |||
| 299 | # finally, if it all went OK, say so. | ||
| 300 | $self->state('updated'); | ||
| 301 | $self->response->status(204); | ||
| 302 | return OK; | ||
| 303 | } | ||
| 304 | |||
| 305 | 1; | ||
| 306 | |||
| 307 | # package MagpieGenericWrapper; | ||
| 308 | # | ||
| 309 | # sub new { | ||
| 310 | # my $proto = shift; | ||
| 311 | # my %args = @_; | ||
| 312 | # return bless \%args, $proto; | ||
| 313 | # } | ||
| 314 | |||
| 315 | 1; | ||
| 316 | |||
| 317 | __END__ |