Skip to content

Commit

Permalink
tidy up, renaming stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kubegu committed Jun 16, 2024
1 parent dd58ebb commit fd876e7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
28 changes: 25 additions & 3 deletions library-le/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions library-le/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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, "<p>Es wurden keine Titel gefunden.</p>")
return
}

availableMovies := filterAvailable(movies)
availableMovies := filterAvailable(media)
byBranch := arrangeByBranch(availableMovies)
data := map[string][]MediaByBranch{
"Branches": byBranch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
<div class="container">
<form hx-post="/games-search/" hx-target="#display-games" hx-indicator="#spinner" class="d-flex flex-wrap justify-content-center">
<form hx-post="/games-index/" hx-target="#display-games" hx-indicator="#spinner" class="d-flex flex-wrap justify-content-center">
<div class="p-2">
<select id="branchSelect" name="branch" class="form-select" style="width:auto;">
<option>Stadtbibliothek</option>
Expand Down

0 comments on commit fd876e7

Please sign in to comment.