OSDN Git Service

implement index page
authorhylom <hylom@users.sourceforge.jp>
Fri, 9 Sep 2016 12:57:51 +0000 (21:57 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 9 Sep 2016 12:57:51 +0000 (21:57 +0900)
dev/newslash_web/css/main.less
dev/newslash_web/lib/Newslash/Model/Stories.pm [new file with mode: 0644]
dev/newslash_web/lib/Newslash/Web.pm
dev/newslash_web/lib/Newslash/Web/Controller/Index.pm
dev/newslash_web/templates/common/layout.html.tt2
dev/newslash_web/templates/index/root.html.tt2

index bd32771..eb24cfd 100644 (file)
@@ -3,11 +3,14 @@
 @color: #1a150d;
 
 body {
-  background: @background;
-  color: @color;
+    background: @background;
+    color: @color;
 }
 
 #site-header h1 {
     text-align: center;
 }
 
+article h1 {
+    
+}
\ No newline at end of file
diff --git a/dev/newslash_web/lib/Newslash/Model/Stories.pm b/dev/newslash_web/lib/Newslash/Model/Stories.pm
new file mode 100644 (file)
index 0000000..55b3f93
--- /dev/null
@@ -0,0 +1,72 @@
+package Newslash::Model::Stories;
+use base qw(Newslash::Model::Base);
+
+use Data::Dumper;
+use Mojo::Log;
+
+#========================================================================
+
+=head2 latest(\%options)
+
+get latest stories.
+
+=over 4
+
+=item Parameters
+
+=over 4
+
+=item \%options
+
+options for query.
+
+$options->{show_future}: when 1, return feature stories. default is 0.
+$options->{limit}: number of stories. default is 10.
+
+=back
+
+=item Return value
+
+ARRAY of story contents
+
+=back
+
+=cut
+
+sub latest {
+  my ($self, $options) = @_;
+  $options ||= {};
+
+  my $show_future = $options->{show_future} || 0;
+  my $limit = $options->{limit} || 10;
+
+  my $where_clause = 'WHERE stories.time <= NOW()';;
+  if ($show_future) {
+    $where_clause = '';
+  }
+
+  my $dbh = $self->connect_db;
+  my $sql = <<"EOSQL";
+SELECT * 
+  FROM stories INNER JOIN story_text ON stories.stoid = story_text.stoid
+  $where_clause
+  ORDER BY time DESC
+  LIMIT ?
+EOSQL
+
+  my $sth = $dbh->prepare($sql);
+
+  $sth->execute($limit);
+  my $rs = $sth->fetchall_arrayref(+{});
+
+  $sth->finish;
+  $dbh->disconnect();
+
+  if (@$rs == 0) {
+    return [];
+  }
+
+  return $rs;
+}
+
+1;
index 251fb27..70065c8 100644 (file)
@@ -2,6 +2,7 @@ package Newslash::Web;
 use Mojo::Base 'Mojolicious';
 
 use Newslash::Model::Users;
+use Newslash::Model::Stories;
 
 # This method will run once at server start
 sub startup {
@@ -12,6 +13,7 @@ sub startup {
 
     # add Model
     $app->helper(users => sub { state $users = Newslash::Model::Users->new });
+    $app->helper(stories => sub { state $stories = Newslash::Model::Stories->new });
 
     # Documentation browser under "/perldoc"
     $app->plugin('PODRenderer');
index 366cac4..7859aeb 100644 (file)
@@ -5,7 +5,8 @@ sub root {
     my $c = shift;
     my $user = $c->session('user');
 
-    $c->render(user => $user);
+    my $stories = $c->stories->latest;
+    $c->render(user => $user, stories => $stories);
 }
 
 1;
index f31c20d..2414e05 100644 (file)
@@ -1,8 +1,9 @@
 <!DOCTYPE html>
 <html>
   <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <title>[% title %]</title>
-    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" />
+    <link rel="stylesheet" type="text/css" href="[% NS.css_path('bootstrap'); %]" />
     <script src="/jquery/jquery.min.js"></script>
     <script src="/bootstrap/js/bootstrap.min.js" ></script>
     <link rel="stylesheet" type="text/css" media="screen, projection" href="[% NS.css_path('main'); %]" />
index 04b054c..e95816e 100644 (file)
@@ -1,4 +1,28 @@
 [% WRAPPER common/layout %]
 
+<div class="index">
+  [%- FOREACH story IN stories -%]
+  <article>
+    <header>
+      <h1>[% story.title %]</h1>
+      <div class="property">
+       ストーリー by [% story.submitter %]
+       [% story.time %]
+       [% story.dept %] 部門より
+      </div>
+    </header>
+    <div class="body">[% story.introtext %]</div>
+    <footer>
+      <div class="link-to-story">記事を読む</div>
+      <div class="tag-bar">
+       <ul class="tags">
+         <li>tag1</li>
+         <li>tag2</li>
+       </ul>
+      </div>
+    </footer>
+  </article>
+  [%- END -%]
+</div><!-- .index -->
 
 [% END %]