From fd876e758008f337eee141824db6d019075abe26 Mon Sep 17 00:00:00 2001 From: kubegu Date: Sun, 16 Jun 2024 21:37:15 +0200 Subject: [PATCH] tidy up, renaming stuff --- library-le/search.go | 28 +++++++++++++++++-- library-le/search_test.go | 4 +-- web/server.go | 11 +++++--- .../{games.html => games-index.html} | 2 +- 4 files changed, 35 insertions(+), 10 deletions(-) rename web/templates/{games.html => games-index.html} (94%) diff --git a/library-le/search.go b/library-le/search.go index 536c39b..3027420 100644 --- a/library-le/search.go +++ b/library-le/search.go @@ -32,10 +32,10 @@ func (libClient Client) FindMovies(title string) []domain.Media { httpClient := http.Client{} searchResponse, err := httpClient.Do(searchRequest) if err != nil { - log.Println("error during search") + log.Println(err) return nil } - resultTitles := parseMediaSearch(searchResponse.Body) + resultTitles := extractTitles(searchResponse.Body) movies := make([]domain.Media, 0) //TODO: Parallel Ergbnislinks folgen und Details sammeln @@ -45,6 +45,28 @@ func (libClient Client) FindMovies(title string) []domain.Media { return movies } +// Search for a specific game title in all library branches +func (libClient Client) FindGames(title string, platform string) []domain.Media { + sessionErr := libClient.openSession() + if sessionErr != nil { + fmt.Println(sessionErr) + return nil + } + searchRequest := NewGameSearchRequest(title, platform, libClient.session) + httpClient := http.Client{} + searchResponse, err := httpClient.Do(searchRequest) + if err != nil { + log.Println(err) + return nil + } + resultTitles := extractTitles(searchResponse.Body) + games := make([]domain.Media, 0) + for _, resultTitle := range resultTitles { + games = append(games, resultTitle.loadMediaCopies(libClient.session)...) + } + return games +} + // Load all existing copys of a result title over all library branches func (result searchResult) loadMediaCopies(libSession webOpacSession) []domain.Media { request := createRequest(libSession, result.resultUrl) @@ -60,7 +82,7 @@ func (result searchResult) loadMediaCopies(libSession webOpacSession) []domain.M // Go through the search overview page and create a result object for each title found. // The result contain details of each copie availabile of the media. -func parseMediaSearch(searchResponse io.Reader) []searchResult { +func extractTitles(searchResponse io.Reader) []searchResult { doc, docErr := goquery.NewDocumentFromReader(searchResponse) if docErr != nil { log.Println("Could not create document from response.") diff --git a/library-le/search_test.go b/library-le/search_test.go index c2f9736..7333f84 100644 --- a/library-le/search_test.go +++ b/library-le/search_test.go @@ -40,7 +40,7 @@ func TestParseMovieCopiesResult(t *testing.T) { func TestParseSearchResultMovies(t *testing.T) { testResponse := loadTestData("testdata/movie_search_result.html") - results := parseMediaSearch(testResponse) + results := extractTitles(testResponse) Equal(t, 3, len(results)) Equal(t, "Der Clou", results[0].title) @@ -56,7 +56,7 @@ func TestParseSearchResultMovies(t *testing.T) { func TestParseSearchResultGames(t *testing.T) { testResponse := loadTestData("testdata/game_search_result.html") - results := parseMediaSearch(testResponse) + results := extractTitles(testResponse) Equal(t, 3, len(results)) Equal(t, "Monster hunter generations ultimate", results[0].title) diff --git a/web/server.go b/web/server.go index cf6b053..96d5bc8 100644 --- a/web/server.go +++ b/web/server.go @@ -21,7 +21,7 @@ func InitMux() *http.ServeMux { mux.HandleFunc("/games/", gamesIndexPageHandler) mux.HandleFunc("/movies/", moviePageHandler) - mux.HandleFunc("/games-search/", gameIndexHandler) + mux.HandleFunc("/games-index/", gameIndexHandler) mux.HandleFunc("/movies-search/", movieSearchHandler) return mux } @@ -32,7 +32,7 @@ type MediaByBranch struct { } func gamesIndexPageHandler(respWriter http.ResponseWriter, request *http.Request) { - templ := template.Must(template.ParseFS(htmlTemplates, "templates/games.html")) + templ := template.Must(template.ParseFS(htmlTemplates, "templates/games-index.html")) templ.Execute(respWriter, nil) } @@ -49,13 +49,16 @@ func movieSearchHandler(respWriter http.ResponseWriter, request *http.Request) { title := strings.ToLower(request.PostFormValue("movie-title")) client := libClient.Client{} movies := client.FindMovies(title) + renderMediaResults(movies, respWriter) +} - if len(movies) == 0 { +func renderMediaResults(media []domain.Media, respWriter http.ResponseWriter) { + if len(media) == 0 { fmt.Fprint(respWriter, "

Es wurden keine Titel gefunden.

") return } - availableMovies := filterAvailable(movies) + availableMovies := filterAvailable(media) byBranch := arrangeByBranch(availableMovies) data := map[string][]MediaByBranch{ "Branches": byBranch, diff --git a/web/templates/games.html b/web/templates/games-index.html similarity index 94% rename from web/templates/games.html rename to web/templates/games-index.html index fbe4ccf..50cfcbc 100644 --- a/web/templates/games.html +++ b/web/templates/games-index.html @@ -6,7 +6,7 @@
-
+