From 976c56b1294ed32965141e6cfdea5c45cea89908 Mon Sep 17 00:00:00 2001 From: Hugo Abonizio Date: Wed, 4 Oct 2017 14:44:54 -0300 Subject: [PATCH] I'm feeling lucky feature (#53) * feat: Add GET /announcements/random endpoint * test: Add controller spec for GET /announcements/random * New line * refact: Fixes based on feedback --- config/routes.cr | 1 + spec/controllers/announcement_controller_spec.cr | 10 ++++++++++ spec/models/announcement_spec.cr | 13 +++++++++++++ src/controllers/announcement_controller.cr | 8 ++++++++ src/models/announcement.cr | 4 ++++ src/views/layouts/_search.slang | 2 ++ 6 files changed, 38 insertions(+) diff --git a/config/routes.cr b/config/routes.cr index 2257cb5..7574dbc 100644 --- a/config/routes.cr +++ b/config/routes.cr @@ -23,6 +23,7 @@ Amber::Server.configure do |app| routes :web do resources "/announcements", AnnouncementController + get "/announcements/random", AnnouncementController, :random get "/=:hashid", AnnouncementController, :expand get "/rss", RSSController, :show get "/sessions/new", SessionsController, :new diff --git a/spec/controllers/announcement_controller_spec.cr b/spec/controllers/announcement_controller_spec.cr index e6113ab..6fb5f7d 100644 --- a/spec/controllers/announcement_controller_spec.cr +++ b/spec/controllers/announcement_controller_spec.cr @@ -357,4 +357,14 @@ describe AnnouncementController do end end end + + describe "GET random" do + before { announcement } + + it "redirects to a random announcement" do + get "/announcements/random" + expect(response.status_code).to eq 302 + expect(response).to redirect_to "/announcements/#{announcement.id}" + end + end end diff --git a/spec/models/announcement_spec.cr b/spec/models/announcement_spec.cr index 19294d8..6a9d768 100644 --- a/spec/models/announcement_spec.cr +++ b/spec/models/announcement_spec.cr @@ -112,4 +112,17 @@ describe Announcement do expect(hashid).to eq "D49Nz" end end + + describe ".random" do + let!(:ann) { announcement(user.tap &.save).tap &.save } + + before do + Announcement.clear + User.clear + end + + it "returns random announcements" do + expect(Announcement.random.not_nil!.id).to eq ann.id + end + end end diff --git a/src/controllers/announcement_controller.cr b/src/controllers/announcement_controller.cr index 1ea07e9..8fdde9d 100644 --- a/src/controllers/announcement_controller.cr +++ b/src/controllers/announcement_controller.cr @@ -83,6 +83,14 @@ class AnnouncementController < ApplicationController end end + def random + if announcement = Announcement.random + redirect_to "/announcements/#{announcement.id}" + else + redirect_to "/" + end + end + private def announcement_params params.to_h.select %w(title description type) end diff --git a/src/models/announcement.cr b/src/models/announcement.cr index 730dae1..d81e105 100644 --- a/src/models/announcement.cr +++ b/src/models/announcement.cr @@ -101,4 +101,8 @@ class Announcement < Granite::ORM::Base def content Autolink.auto_link(Markdown.to_html(description.not_nil!)) end + + def self.random + Announcement.all("ORDER BY RANDOM() LIMIT 1").first? + end end diff --git a/src/views/layouts/_search.slang b/src/views/layouts/_search.slang index d2f36d3..3afbd61 100644 --- a/src/views/layouts/_search.slang +++ b/src/views/layouts/_search.slang @@ -9,3 +9,5 @@ aside.widget.search input type="hidden" name="user" value="#{user}" - if type input type="hidden" name="type" value="#{type}" + a href="/announcements/random" + | I'm feeling lucky!