--- /dev/null
+package Newslash::Model::Journals;
+use Newslash::Model::Base -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 $dbh = $self->connect_db;
+
+ # get stories
+ my $where_clause = 'WHERE stories.time <= NOW()';;
+ if ($show_future) {
+ $where_clause = '';
+ }
+
+ my $sql = <<"EOSQL";
+SELECT latest.*, story_text.*, users.nickname as author
+ FROM (SELECT * from stories $where_clause ORDER BY time DESC LIMIT ?) AS latest
+ LEFT JOIN story_text ON latest.stoid = story_text.stoid
+ LEFT JOIN users ON latest.uid = users.uid
+EOSQL
+
+ my $sth = $dbh->prepare($sql);
+
+ $sth->execute($limit);
+ my $rs = $sth->fetchall_arrayref(+{});
+ $sth->finish;
+
+ if (@$rs == 0) {
+ $dbh->disconnect();
+ return [];
+ }
+
+ # get tags
+ $sql = <<"EOSQL";
+SELECT story_topics_rendered.*, story_topics_chosen.weight, topics.*
+ FROM (SELECT stoid FROM stories $where_clause ORDER BY time DESC LIMIT ?) AS latest
+ INNER JOIN story_topics_rendered ON latest.stoid = story_topics_rendered.stoid
+ LEFT JOIN story_topics_chosen ON story_topics_rendered.stoid = story_topics_chosen.stoid
+ AND story_topics_rendered.tid = story_topics_chosen.tid
+ LEFT JOIN topics ON story_topics_rendered.tid = topics.tid
+EOSQL
+
+ $sth = $dbh->prepare($sql);
+
+ $sth->execute($limit);
+ my $topics_table = $sth->fetchall_arrayref(+{});
+ $sth->finish;
+ $dbh->disconnect();
+
+ my $topics = {};
+ for my $topic (@$topics_table) {
+ if (!$topic->{stoid}) {
+ $topics->{$topic->{stoid}} = [];
+ }
+ push @{$topics->{$topic->{stoid}}}, $topic;
+ }
+
+ for my $story (@$rs) {
+ my $stoid = $story->{stoid};
+ $story->{topics} = $topics->{$stoid};
+ my $max_weight = 0;
+ for my $t (@{$story->{topics}}) {
+ if ($t->{weight} && $t->{weight} > $max_weight) {
+ $story->{primary_topic} = $t;
+ $max_weight = $t->{weight};
+ }
+ }
+ }
+
+ return $rs;
+}
+
+
+#========================================================================
+
+=head2 select($query_type, $value)
+
+get a story.
+
+=over 4
+
+=item Parameters
+
+=over 4
+
+=item $query_type
+
+query key, "sid" or "stoid"
+
+=item $value
+
+value for query
+
+=back
+
+=item Return value
+
+HASH of story contents
+
+=back
+
+=cut
+
+sub select {
+ my ($self, $query_type, $value) = @_;
+
+ if ($query_type !~ m/\A(sid|stoid)\z/) {
+ return undef;
+ }
+
+ my $dbh = $self->connect_db;
+ my $sql = <<"EOSQL";
+SELECT stories.*, story_text.*, users.nickname as author
+ FROM stories
+ LEFT JOIN story_text ON stories.stoid = story_text.stoid
+ LEFT JOIN users ON stories.uid = users.uid
+ WHERE stories.$query_type = ?
+EOSQL
+
+ my $sth = $dbh->prepare($sql);
+
+ $sth->execute($value);
+ my $story = $sth->fetchrow_hashref;
+ $sth->finish;
+
+ if (!$story) {
+ $dbh->disconnect();
+ return undef;
+ }
+
+ my $stoid = $story->{stoid};
+ if (!$stoid) {
+ $dbh->disconnect();
+ return undef;
+ }
+
+
+ # get tags
+ $sql = <<"EOSQL";
+SELECT story_topics_rendered.*, story_topics_chosen.weight, topics.*
+ FROM story_topics_rendered
+ LEFT JOIN story_topics_chosen ON story_topics_rendered.stoid = story_topics_chosen.stoid
+ AND story_topics_rendered.tid = story_topics_chosen.tid
+ LEFT JOIN topics ON story_topics_rendered.tid = topics.tid
+ WHERE story_topics_rendered.stoid = ?
+EOSQL
+
+ $sth = $dbh->prepare($sql);
+
+ $sth->execute($stoid);
+ my $topics = $sth->fetchall_arrayref(+{});
+ $sth->finish;
+ $dbh->disconnect();
+
+ $story->{topics} = $topics;
+ my $max_weight = 0;
+ for my $t (@$topics) {
+ if ($t->{weight} && $t->{weight} > $max_weight) {
+ $story->{primary_topic} = $t;
+ $max_weight = $t->{weight};
+ }
+ }
+
+ return $story;
+}
+
+1;
use Newslash::Model::Discussions;
use Newslash::Model::Journals;
+use Newslash::Model;
+
# This method will run once at server start
sub startup {
my $app = shift;
# add Model
my $db_opts = $app->config->{Database};
- $app->helper(users => sub { state $users = Newslash::Model::Users->new($db_opts) });
- $app->helper(stories => sub { state $stories = Newslash::Model::Stories->new($db_opts) });
- $app->helper(discussions => sub { state $discussions = Newslash::Model::Discussions->new($db_opts) });
- $app->helper(comments => sub { state $comments = Newslash::Model::Comments->new($db_opts) });
- $app->helper(journals => sub { state $journals = Newslash::Model::Journals->new($db_opts) });
+ $app->helper(model => Newslash::Model->loader($db_opts));
# use Template::Toolkit 2 render
$app->plugin('Newslash::Plugin::TT2Renderer');