diff --git a/controllers/search.go b/controllers/search.go index e2777b21..e074d0f5 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -25,15 +25,39 @@ 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 = len(term) != 0 + var validSearch = true + + if len(term) < 3 { + errorMsg = "Your search term is too short. Try using more keywords." + validSearch = false + } + + var showResults []myradio.ShowMeta + var podcastResults []myradio.Podcast + var peopleResults []myradio.UserSearch + var err error - if searching { + if validSearch { // Contact the DB and get search results sm := models.NewSearchModel(sc.session) - results, err = sm.Get(term) + switch source { + default: + source = "show" + showResults, err = sm.GetShows(term) + case "podcast": + podcastResults, err = sm.GetPodcasts(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 { log.Println(err) @@ -41,18 +65,28 @@ func (sc *SearchController) Get(w http.ResponseWriter, r *http.Request) { } } + // TODO Need to filter for unpublished podcasts/shows/people somehow! + var numResults = len(showResults) + len(podcastResults) + len(peopleResults) data := struct { - Searching bool - Results []myradio.ShowMeta + 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), + 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/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/sass/base/_searchBox.scss b/sass/base/_searchBox.scss index 8a32810b..9b71c908 100644 --- a/sass/base/_searchBox.scss +++ b/sass/base/_searchBox.scss @@ -13,6 +13,7 @@ $search-background: rgb(255, 255, 255); margin-top: auto; width: auto; + input { background: none; border: 0; @@ -22,6 +23,7 @@ $search-background: rgb(255, 255, 255); padding-left: 0; padding-right: .5em; width: 100%; + float: left; &:focus { outline: none; @@ -30,6 +32,27 @@ $search-background: rgb(255, 255, 255); } } + input[type="radio"] { + display: none; + } + .source-group { + position: absolute; + top: -4px; + right: 1px; + background: $off-white-color; + border-radius: 45px; + padding: 0 15px; + label { + display: inline-block; + padding: 0 5px; + font-size: 0.8em; + vertical-align: text-top; + } + input:checked + label { + color: $blue; + } + + } button { color: $search-button-color; border: 0; diff --git a/views/search.tmpl b/views/search.tmpl index 50ba9b39..094b4cbc 100644 --- a/views/search.tmpl +++ b/views/search.tmpl @@ -2,27 +2,130 @@ {{define "content"}} {{with .PageData}} -