use views to report number of submited reservations
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 30 Sep 2011 16:00:19 +0000 (18:00 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 30 Sep 2011 16:00:19 +0000 (18:00 +0200)
angular-server.pl
public/app/reservations/calendar.html

index 9487599..71e5f69 100755 (executable)
@@ -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__
 
index 0f5b2b2..3175451 100644 (file)
@@ -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){
 <ul>
 <li ng:repeat="c in data.events" ng:show="! reservation.UID || reservation.UID == c.UID">
 {{c.DTSTART}} - {{c.DTEND}} 
-<a href="" ng:click="reservation.UID = c.UID ; reservation.event = c">
-<b>{{c.SUMMARY}}</b>
-</a>
+<a href="" ng:click="reservation.UID = c.UID ; reservation.event = c">{{c.SUMMARY}}</a>
+<b ng:show="submited[c.UID]">
+broj prijava: {{submited[c.UID]}}
+</b>
 <div>{{c.LOCATION}}</div>
 <div ng:show="c.DESCRIPTION">
 {{c.DESCRIPTION}}
@@ -84,7 +97,7 @@ zvanje: <input name="reservation.zvanje">
 <br>
 podru─Źke/tema zaninimanja: <input name="reservation.porducje">
 <br>
-<input type=button ng:click="reservation.$save()" value="Prijavi me">
+<input type=button ng:click="reservation.$save(); load_reservations();" value="Prijavi me">
 <pre>{{reservation}}</pre>
 </form>