correct and cleanup in-memory schema
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 1 Nov 2010 13:24:53 +0000 (14:24 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 1 Nov 2010 13:24:53 +0000 (14:24 +0100)
angular-server.pl

index 46a80ad..0896eef 100755 (executable)
@@ -123,13 +123,24 @@ get '/data/:database/:entity/:id' => sub {
 any [ 'post' ] => '/data/:database/:entity' => sub {
        my $self = shift;
        my $json = $self->req->json;
-       my $id = $self->param('id');
-       $id = $json->{'$id'};
-       $id = Time::HiRes::time() if ! $id || $id eq '_new';
-       $json->{'$id'} = $id;
+       my $id = $json->{'$id'} || Time::HiRes::time(); # FIXME UUID?
        warn "## $id body ",dump($self->req->body, $json);
        die "no data" unless $data;
-       $data->{ $self->param('database') }->{ $self->param('entity') }->{ $id } = $json;
+
+       my $database = $self->param('database');
+       my $entity   = $self->param('entity');
+
+       my $nr = $id2nr->{$database}->{$entity}->{$id};
+       if ( defined $nr ) {
+               $data->{$database}->{$entity}->[$nr] = $json;
+               warn "# update $nr $id ",dump($json);
+       } else {
+               $json->{'$id'} ||= $id;
+               push @{ $data->{$database}->{$entity} }, $json;
+               my $nr = $#{ $data->{$database}->{$entity} };
+               $id2nr->{$id} = $nr;
+               warn "# added $nr $id ",dump($json);
+       }
        _render_jsonp( $self,  $json );
 };