From ba487680f7b6943fb75dad9df9d092485bd96d91 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sun, 17 Jun 2018 00:45:37 +0100 Subject: [PATCH 1/5] Begin ability to search for podcasts and people. --- controllers/search.go | 58 ++++++++++++++++++++++++++++++++----------- models/search.go | 26 +++++++++++++++++-- views/search.tmpl | 38 ++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 24 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index e2777b21..4b408029 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -25,34 +25,64 @@ func NewSearchController(s *myradio.Session, c *structs.Config) *SearchControlle func (sc *SearchController) Get(w http.ResponseWriter, r *http.Request) { // Check if they've landed or they've searched var term = r.URL.Query().Get("term") - var searching = (term != "") - var results []myradio.ShowMeta + var source = r.URL.Query().Get("source") + + var errorMsg string + var searching = false + + if len(term) < 3 { + errorMsg = "Your search term is too short. Try using more keywords." + } else { + searching = true + } + + var showResults []myradio.ShowMeta + var podcastResults []myradio.Podcast + var peopleResults []myradio.UserSearch + var err error if searching { // Contact the DB and get search results sm := models.NewSearchModel(sc.session) - results, err = sm.Get(term) + switch source { + case "show": + showResults, err = sm.GetShows(term) + case "podcast": + podcastResults, err = sm.GetPodcasts(term) + case "people": + peopleResults, err = sm.GetUsers(term) + default: + errorMsg = "You didn't select something to search for. Please select shows or podcasts." + } if err != nil { log.Println(err) return } } - + var numResults = len(showResults) + len(podcastResults) + len(peopleResults) data := struct { - Searching bool - Results []myradio.ShowMeta - NumResults int - BaseURL string - Term string + Searching bool + Source string + ShowResults []myradio.ShowMeta + PodcastResults []myradio.Podcast + PeopleResults []myradio.UserSearch + NumResults int + BaseURL string + Term string + ErrorMsg string }{ - Searching: searching, - Results: results, - NumResults: len(results), - BaseURL: r.URL.Path, - Term: term, + Searching: searching, + Source: source, + ShowResults: showResults, + PodcastResults: podcastResults, + PeopleResults: peopleResults, + NumResults: numResults, + BaseURL: r.URL.Path, + Term: term, + ErrorMsg: errorMsg, } err = utils.RenderTemplate(w, sc.config.PageContext, data, "search.tmpl") diff --git a/models/search.go b/models/search.go index 5d5d27ea..e63cbe73 100644 --- a/models/search.go +++ b/models/search.go @@ -15,13 +15,35 @@ func NewSearchModel(s *myradio.Session) *SearchModel { return &SearchModel{Model{session: s}} } -// Get gets the data required for the Search controller from MyRadio. +// GetShows gets the data required for the Search controller from MyRadio. // // term is the string term to search for. This is currently a show // search. // // On success, it returns the search results, and nil. // Otherwise, it returns undefined data and the error causing failure. -func (m *SearchModel) Get(term string) ([]myradio.ShowMeta, error) { +func (m *SearchModel) GetShows(term string) ([]myradio.ShowMeta, error) { return m.session.GetSearchMeta(term) } + +// GetPodcasts gets the data required for the Search controller from MyRadio. +// +// term is the string term to search for. This is currently a podcast +// search. +// +// On success, it returns the search results, and nil. +// Otherwise, it returns undefined data and the error causing failure. +func (m *SearchModel) GetPodcasts(term string) ([]myradio.Podcast, error) { + return m.session.GetPodcastMeta(term) +} + +// GetUsers gets the data required for the Search controller from MyRadio. +// +// term is the string term to search for. This is currently a user +// search. +// +// On success, it returns the search results, and nil. +// Otherwise, it returns undefined data and the error causing failure. +func (m *SearchModel) GetUsers(term string) ([]myradio.UserSearch, error) { + return m.session.GetUserMeta(term) +} diff --git a/views/search.tmpl b/views/search.tmpl index 50ba9b39..138979f5 100644 --- a/views/search.tmpl +++ b/views/search.tmpl @@ -7,23 +7,45 @@
+ + + + + +
+ {{if gt (len .ErrorMsg) 0}} +
{{.ErrorMsg}}
+ {{end}} {{if .Searching}}
{{.NumResults}} Results

- {{range .Results}} + {{range .ShowResults}}
- {{.Title}} + {{.Title}} +
+ {{html .Description}} +
+ {{end}} + {{range .PodcastResults}} +
+ {{.Title}}
{{html .Description}}
- {{else}} -

- No Results -

- {{end}} - + {{end}} + {{range .PeopleResults}} +
+ {{.Fname}} {{.Sname}} +
+
+ {{end}} + {{if eq .NumResults 0}} +

+ No search results. Please try a different search term. +

+ {{end}} {{else}} {{end}} From 30e8266141b8cbaec62d94c31bd6786e2a86f425 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sun, 17 Jun 2018 01:46:50 +0100 Subject: [PATCH 2/5] Nicer search source design. --- controllers/search.go | 7 ++++--- sass/base/_searchBox.scss | 23 +++++++++++++++++++++++ views/search.tmpl | 16 ++++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index 4b408029..b3517a0c 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -47,14 +47,15 @@ func (sc *SearchController) Get(w http.ResponseWriter, r *http.Request) { sm := models.NewSearchModel(sc.session) switch source { - case "show": + default: + source = "show" showResults, err = sm.GetShows(term) case "podcast": podcastResults, err = sm.GetPodcasts(term) case "people": peopleResults, err = sm.GetUsers(term) - default: - errorMsg = "You didn't select something to search for. Please select shows or podcasts." + case "show": + showResults, err = sm.GetShows(term) } if err != nil { diff --git a/sass/base/_searchBox.scss b/sass/base/_searchBox.scss index 8a32810b..4ffab927 100644 --- a/sass/base/_searchBox.scss +++ b/sass/base/_searchBox.scss @@ -13,6 +13,8 @@ $search-background: rgb(255, 255, 255); margin-top: auto; width: auto; + + input { background: none; border: 0; @@ -22,6 +24,7 @@ $search-background: rgb(255, 255, 255); padding-left: 0; padding-right: .5em; width: 100%; + float: left; &:focus { outline: none; @@ -30,6 +33,26 @@ $search-background: rgb(255, 255, 255); } } + input[type="radio"] { + display: none; + } + .source-group { + position: absolute; + top: 0; + right: 0; + background: $off-white-color; + border-radius: 45px; + padding: 0 15px; + label { + display: inline-block; + padding: 0 5px; + font-size: 0.8em; + } + input:checked + label { + color: $blue; + } + + } button { color: $search-button-color; border: 0; diff --git a/views/search.tmpl b/views/search.tmpl index 138979f5..ac184d93 100644 --- a/views/search.tmpl +++ b/views/search.tmpl @@ -7,12 +7,16 @@
- - - - - - + +
+ + + + + + + +
{{if gt (len .ErrorMsg) 0}} From f67bc38c73c7211a2b3fb82079868c6f781ca3d7 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 27 Feb 2021 13:19:36 +0000 Subject: [PATCH 3/5] Remove people because can't filter public. --- controllers/search.go | 25 +++++++++++++------------ views/search.tmpl | 39 ++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index b3517a0c..721ac637 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -52,10 +52,11 @@ func (sc *SearchController) Get(w http.ResponseWriter, r *http.Request) { showResults, err = sm.GetShows(term) case "podcast": podcastResults, err = sm.GetPodcasts(term) - case "people": - peopleResults, err = sm.GetUsers(term) case "show": showResults, err = sm.GetShows(term) + // Sadly the people endpoint doesn't return just public users yet. + //case "people": + // peopleResults, err = sm.GetUsers(term) } if err != nil { @@ -69,21 +70,21 @@ func (sc *SearchController) Get(w http.ResponseWriter, r *http.Request) { Source string ShowResults []myradio.ShowMeta PodcastResults []myradio.Podcast - PeopleResults []myradio.UserSearch - NumResults int - BaseURL string - Term string - ErrorMsg string + //PeopleResults []myradio.UserSearch + NumResults int + BaseURL string + Term string + ErrorMsg string }{ Searching: searching, Source: source, ShowResults: showResults, PodcastResults: podcastResults, - PeopleResults: peopleResults, - NumResults: numResults, - BaseURL: r.URL.Path, - Term: term, - ErrorMsg: errorMsg, + //PeopleResults: peopleResults, + NumResults: numResults, + BaseURL: r.URL.Path, + Term: term, + ErrorMsg: errorMsg, } err = utils.RenderTemplate(w, sc.config.PageContext, data, "search.tmpl") diff --git a/views/search.tmpl b/views/search.tmpl index ac184d93..c76b67be 100644 --- a/views/search.tmpl +++ b/views/search.tmpl @@ -14,8 +14,6 @@ - - @@ -25,25 +23,24 @@ {{if .Searching}}
{{.NumResults}} Results

- {{range .ShowResults}} -
- {{.Title}} -
- {{html .Description}} -
- {{end}} - {{range .PodcastResults}} -
- {{.Title}} -
- {{html .Description}} -
- {{end}} - {{range .PeopleResults}} -
- {{.Fname}} {{.Sname}} -
-
+ {{if .ShowResults }} +

Shows

+ {{range .ShowResults}} +
+ {{.Title}} +
+ {{html .Description}} +
+ {{end}} + {{else if .PodcastResults}} +

Podcasts

+ {{range .PodcastResults}} +
+ {{.Title}} +
+ {{html .Description}} +
+ {{end}} {{end}} {{if eq .NumResults 0}}

From cbec0d122eaa0a03bf6724e2c3410d4c071aa343 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 27 Feb 2021 13:31:51 +0000 Subject: [PATCH 4/5] Allow search filters to submit search --- public/js/search.js | 7 +++++++ views/search.tmpl | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 public/js/search.js diff --git a/public/js/search.js b/public/js/search.js new file mode 100644 index 00000000..06a8d062 --- /dev/null +++ b/public/js/search.js @@ -0,0 +1,7 @@ +let form = document.getElementById("searchForm"); +let filters = document.getElementsByClassName("search-filter") +for (var i = 0; i < filters.length; i++) { + filters[i].addEventListener("click", function () { + form.submit(); + }); +}; diff --git a/views/search.tmpl b/views/search.tmpl index c76b67be..b0dbba24 100644 --- a/views/search.tmpl +++ b/views/search.tmpl @@ -3,16 +3,15 @@ {{with .PageData}}
-