Permalink
Browse files

started Mojolicious port

  • Loading branch information...
1 parent 130e65a commit b7ee1f8548284b24da25e79de92eb62f40747894 @uwe committed with Jan 28, 2012
View
@@ -0,0 +1,37 @@
+package Mittag::Web;
+
+use Mojo::Base 'Mojolicious';
+
+use Mittag::DB::Schema;
+
+
+has schema => sub {
+ my $config = {
+ db_name => 'mittag',
+ db_user => 'root',
+ db_pass => 'root',
+ };
+ Mittag::DB::Schema->connect_with_config($config);
+};
+
+
+sub rs {
+ my ($self, $model) = @_;
+
+ return $self->schema->resultset('Mittag::DB::Schema::' . $model);
+}
+
+sub startup {
+ my ($self) = @_;
+
+ $self->plugin('tt_renderer');
+
+ my $r = $self->routes;
+
+ $r->route('/' )->to('day#date');
+ $r->route('/day' )->to('day#date');
+ $r->route('/day/:date')->to('day#date');
+}
+
+
+1;
View
@@ -0,0 +1,126 @@
+package Mittag::Web::Day;
+
+use Mojo::Base 'Mojolicious::Controller';
+
+use DateTime;
+
+
+sub date {
+ my ($self) = @_;
+
+ my $date = eval {
+ my @date = split /-/, $self->param('date') || '';
+ DateTime->new(
+ year => $date[0],
+ month => $date[1],
+ day => $date[2],
+ );
+ };
+ unless ($date) {
+ my $today = DateTime->today;
+ if ($today->dow > 5) {
+ $today = $self->_next_date($today);
+
+ # go back if no data
+ $today = $self->_prev_date(DateTime->today) unless $today;
+ }
+
+ $date = $today;
+ }
+
+ my @offers = $self->_get_offers($date);
+
+ unless (@offers) {
+ my $next_date = $self->_next_date($date, 1);
+ # if there is no future date, we try backwards
+ $next_date ||= $self->_prev_date($date, 1);
+
+ my $res = $self->req->new_response;
+ $res->redirect('/day/' . $next_date->ymd('-'));
+ return $res;
+ }
+
+ $self->stash(
+ OFFERS => \@offers,
+ date => $date,
+ prev_date => $self->_prev_date($date->clone->subtract(days => 1)) || undef,
+ next_date => $self->_next_date($date->clone->add( days => 1)) || undef,
+ );
+}
+
+# get daily and weekly offers (sorted by place and price)
+sub _get_offers {
+ my ($self, $date) = @_;
+
+ my @daily = $self->app->rs('DailyOffer')->search({date => $date->ymd('-')});
+
+ return unless @daily;
+
+ my @weekly = $self->app->rs('WeeklyOffer')->search({
+ from_date => {'<=' => $date->ymd('-')},
+ to_date => {'>=' => $date->ymd('-')},
+ });
+
+ my @offers = sort { $a->place->name cmp $b->place->name or $a->price <=> $b->price } (@daily, @weekly);
+
+ return @offers;
+}
+
+# same date or before
+sub _prev_date {
+ my ($self, $date, $seek) = @_;
+
+ my $daily = $self->app->rs('DailyOffer')->search(
+ {date => {'<=' => $date->ymd('-')}},
+ {order_by => {-desc => 'date'}, rows => 1},
+ )->single;
+
+ if ($daily) {
+ return $daily->date;
+ }
+
+ return unless $seek;
+
+ $daily = $self->app->rs('DailyOffer')->search(
+ {},
+ {order_by => {-asc => 'date'}, rows => 1},
+ )->single;
+
+ if ($daily) {
+ return $daily->date;
+ }
+
+ # database empty?
+ return;
+}
+
+# same date or after
+sub _next_date {
+ my ($self, $date, $seek) = @_;
+
+ my $daily = $self->app->rs('DailyOffer')->search(
+ {date => {'>=' => $date->ymd('-')}},
+ {order_by => {-asc => 'date'}, rows => 1},
+ )->single;
+
+ if ($daily) {
+ return $daily->date;
+ }
+
+ return unless $seek;
+
+ $daily = $self->app->rs('DailyOffer')->search(
+ {},
+ {order_by => {-desc => 'date'}, rows => 1},
+ )->single;
+
+ if ($daily) {
+ return $daily->date;
+ }
+
+ # database empty?
+ return;
+}
+
+
+1;
View
@@ -0,0 +1,13 @@
+package Mittag::Web::Example;
+use Mojo::Base 'Mojolicious::Controller';
+
+# This action will render a template
+sub welcome {
+ my $self = shift;
+
+ # Render template "example/welcome.html.ep" with message
+ $self->render(
+ message => 'Welcome to the Mojolicious real-time web framework!');
+}
+
+1;
View
Oops, something went wrong.
View
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Welcome to the Mojolicious real-time web framework!</title>
+ </head>
+ <body>
+ <h2>Welcome to the Mojolicious real-time web framework!</h2>
+ This is the static document "public/index.html",
+ <a href="/">click here</a> to get back to the start.
+ </body>
+</html>
View
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use Mojo::Base -strict;
+
+use File::Basename 'dirname';
+use File::Spec;
+
+use lib join '/', File::Spec->splitdir(dirname(__FILE__)), 'lib';
+use lib join '/', File::Spec->splitdir(dirname(__FILE__)), '..', 'lib';
+
+# Check if Mojolicious is installed;
+die <<EOF unless eval 'use Mojolicious::Commands; 1';
+It looks like you don't have the Mojolicious framework installed.
+Please visit http://mojolicio.us for detailed installation instructions.
+
+EOF
+
+# Application
+$ENV{MOJO_APP} ||= 'Mittag::Web';
+
+# Start commands
+Mojolicious::Commands->start;
View
@@ -0,0 +1,9 @@
+use Mojo::Base -strict;
+
+use Test::More tests => 4;
+use Test::Mojo;
+
+use_ok 'Mittag::Web';
+
+my $t = Test::Mojo->new('Mittag::Web');
+$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
View
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+[% USE Komma %]
+[% dow = ['', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag'] %]
+<html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
+ <title>Tagesangebote: [% date.dmy('.') %]</title>
+
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+
+ <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>
+ </head>
+
+ <body>
+ <div data-role="page">
+ <div data-role="header">
+ [% IF prev_date %]<a href="/day/[% prev_date.ymd('-') %]" data-role="button" data-icon="arrow-l" data-iconpos="notext" data-transition="slide" data-direction="reverse">[&lt;&lt;]</a>[% END %]
+ <h1>[% dow.${date.dow} | html %], [% date.dmy('.') %]</h1>
+ [% IF next_date %]<a href="/day/[% next_date.ymd('-') %]" data-role="button" data-icon="arrow-r" data-iconpos="notext" data-transition="slide">[&gt;&gt;]</a>[% END %]
+ </div>
+
+ <div data-role="content">
+
+ <ul data-role="listview" data-filter="true">
+[% last_place = '' %]
+[% FOREACH offer = OFFERS %]
+[% IF offer.place.name != last_place %]
+[% last_place = offer.place.name %]
+ <li data-role="list-divider">[% offer.place.name | html %]</li>
+[% END %]
+ <li>[% offer.name | html %] <span class="ui-li-count">[% offer.price | komma2 %] &euro;</span></li>
+[% END %]
+ </ul>
+ </div>
+
+ <div data-role="footer">
+ <p>Alle Angaben ohne Gew&auml;hr!</p>
+ <p>Fehler, Kommentare, neue Restaurants und Mithilfe gern an <a href="mailto:uwe@uwevoelker.de">uwe@uwevoelker.de</a></p>
+ </div>
+ </div>
+
+ </body>
+</html>
@@ -0,0 +1,10 @@
+% layout 'default';
+% title 'Welcome';
+<h2><%= $message %></h2>
+This page was generated from the template
+"templates/example/welcome.html.ep" and the layout
+"templates/layouts/default.html.ep",
+<a href="<%== url_for %>">click here</a>
+to reload the page or
+<a href="/index.html">here</a>
+to move forward to a static page.
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<html>
+ <head><title><%= title %></title></head>
+ <body><%= content %></body>
+</html>

0 comments on commit b7ee1f8

Please sign in to comment.