From 098bff90b332c56a3a9ec3bef14eb50ae1656539 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Wed, 8 May 2024 11:59:38 +0200 Subject: [PATCH] Adding json api to see all offers. --- lib/GTSRSSApi.pm | 1 + lib/GTSRSSApi/Controller/Page.pm | 37 +++++++++++++++++++++----------- templates/page/index.html.ep | 13 +++++------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/GTSRSSApi.pm b/lib/GTSRSSApi.pm index f159aef..01d1acc 100644 --- a/lib/GTSRSSApi.pm +++ b/lib/GTSRSSApi.pm @@ -49,6 +49,7 @@ sub startup ($self) { # Normal route to controller $r->get('/')->to('Page#index'); + $r->get('/offers.json')->to('Page#active_offers_json'); $r->get('/all.rss')->to('Page#all_rss'); $r->get('/shiny.rss')->to('Page#shiny_rss'); $r->get('/pokerus.rss')->to('Page#pokerus_rss'); diff --git a/lib/GTSRSSApi/Controller/Page.pm b/lib/GTSRSSApi/Controller/Page.pm index a6fa542..9caff24 100644 --- a/lib/GTSRSSApi/Controller/Page.pm +++ b/lib/GTSRSSApi/Controller/Page.pm @@ -7,11 +7,11 @@ use warnings; use DateTime::Format::Pg; use DateTime::Format::Mail; use Mojo::DOM; +use Mojo::JSON qw/to_json/; use Mojo::Base 'Mojolicious::Controller', -signatures; use GTSRSSApi::DB; - # This action will render a template sub index ($self) { @@ -19,8 +19,19 @@ sub index ($self) { $self->render; } +sub active_offers_json($self) { + my $db = GTSRSSApi::DB->connect; + my @offers = $db->selectall_array( <<'EOF', { Slice => {} } ); +SELECT * FROM offers WHERE is_available ORDER BY date DESC LIMIT 100; +EOF + @offers = + map { $_->{date} = DateTime::Format::Pg->parse_datetime( $_->{date} ); $_ } + @offers; + return $self->render( json => \@offers ); +} + sub all_rss ($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news ORDER BY date DESC LIMIT 100; EOF @@ -36,7 +47,7 @@ EOF } sub computer_all_rss ($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news_computer ORDER BY date DESC LIMIT 100; EOF @@ -52,7 +63,7 @@ EOF } sub computer_shiny_rss ($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news_computer WHERE is_shiny ORDER BY date DESC LIMIT 100; EOF @@ -68,7 +79,7 @@ EOF } sub shiny_rss ($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news WHERE is_shiny ORDER BY date DESC LIMIT 100; EOF @@ -84,7 +95,7 @@ EOF } sub computer_pokerus_rss($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news_computer WHERE has_pokerus ORDER BY date DESC LIMIT 100; EOF @@ -100,7 +111,7 @@ EOF } sub pokerus_rss($self) { - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news WHERE has_pokerus ORDER BY date DESC LIMIT 100; EOF @@ -125,8 +136,9 @@ sub _feed_from_news_list ( $self, $news, $title, $link ) { $channel_tag->child_nodes->first->append_content($link_tag); $channel_tag->child_nodes->first->append_content($description_tag); - for my $new ( @$news ) { - $channel_tag->child_nodes->first->append_content( $self->_new_to_rss($new) ); + for my $new (@$news) { + $channel_tag->child_nodes->first->append_content( + $self->_new_to_rss($new) ); } $dom->child_nodes->first->append_content($channel_tag); return $dom; @@ -136,10 +148,9 @@ sub _new_to_rss ( $self, $new ) { my $item_tag = Mojo::DOM->new_tag('item'); my $title_tag = Mojo::DOM->new_tag( 'title', $new->{species} ); my $link = Mojo::DOM->new_tag( 'link', 'https://pkmnclassic.net/gts/' ); - my $description = Mojo::DOM->new_tag( 'description', - $new->{news_text}); - my $guid = Mojo::DOM->new_tag( 'guid', $new->{uuid} ); - my $date = Mojo::DOM->new_tag( + my $description = Mojo::DOM->new_tag( 'description', $new->{news_text} ); + my $guid = Mojo::DOM->new_tag( 'guid', $new->{uuid} ); + my $date = Mojo::DOM->new_tag( 'pubDate', '' . DateTime::Format::Mail->format_datetime( diff --git a/templates/page/index.html.ep b/templates/page/index.html.ep index f9970da..711dcbe 100644 --- a/templates/page/index.html.ep +++ b/templates/page/index.html.ep @@ -4,12 +4,13 @@

List of RSS Feeds for different kinds of GTS trades in pkmn-classic network.