From: Dobrica Pavlinusic Date: Fri, 30 Sep 2011 16:00:19 +0000 (+0200) Subject: use views to report number of submited reservations X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=commitdiff_plain;h=1b61608dd2d2fc90aef81350a00a48459c5413ec use views to report number of submited reservations --- diff --git a/angular-server.pl b/angular-server.pl index 9487599..71e5f69 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -240,7 +240,7 @@ get '/reservations/get/(*url)' => sub { my $c = iCal::Parser->new->parse_strings( $text ); - warn "# iCal::Parser = ",dump($c); +# warn "# iCal::Parser = ",dump($c); my $ical = { cal => $c->{cals}->[0], # FIXME assume single calendar @@ -264,6 +264,38 @@ get '/reservations/get/(*url)' => sub { _render_jsonp( $self, $ical ); }; +get '/reservations/events/:view_name' => sub { + my $self = shift; + + my $view = _couchdb_get('/reservations/_design/events/_view/' . $self->param('view_name') . '?group=true'); + my $hash; + + if ( exists $view->{error} ) { + _couchdb_put "/reservations/_design/events", { + _id => '_design/events', + language => 'javascript', + views => { + submited => { + map => q| + function(doc) { + if ( doc.event && doc.event.UID ) emit(doc.event.UID, 1) + } + |, + reduce => q|_sum|, + } + } + }; + } + + _render_jsonp( $self, {} ) unless ref $view->{rows} eq 'ARRAY'; + + foreach my $row ( @{ $view->{rows} } ) { + $hash->{ $row->{key} } = $row->{value}; + } + + _render_jsonp( $self, $hash ); +}; + app->start; __DATA__ diff --git a/public/app/reservations/calendar.html b/public/app/reservations/calendar.html index 0f5b2b2..3175451 100644 --- a/public/app/reservations/calendar.html +++ b/public/app/reservations/calendar.html @@ -17,10 +17,22 @@ function Calendar($xhr,$resource){ $xhr("GET" , "/reservations/get/www.google.com/calendar/ical/8tg8ecg285qshtp75813jktqa0%40group.calendar.google.com/private-b9d68b530fde2c6060979f8a05aa0865/basic.ics" , function(code, response){ - console.log('xhr JSON', code, response); + console.log('xhr calendar', code, response); self.data = response; } ); + + var load_submited = function() { + $xhr("GET" + , "/reservations/events/submited" + , function(code, response){ + console.log('xhr submited', code, response); + self.submited = response; + } + ); + } + load_submited(); + var Reservation = $resource('/data/reservations/prijava/:id'); this.reservation = new Reservation(); @@ -46,9 +58,10 @@ function Calendar($xhr,$resource){