Skip to content
Snippets Groups Projects
Commit 8c9886ab authored by Abdulsamad Sheikh's avatar Abdulsamad Sheikh :cat2:
Browse files

Added logs for ReadershipHandler

parent 8a8ec234
Branches
No related tags found
No related merge requests found
......@@ -2,12 +2,14 @@ package handlers
import (
"encoding/json"
"log"
"net/http"
"assignment1/models"
"assignment1/services"
"fmt"
)
// BookCountHandler handles requests to the /librarystats/v1/bookcount/ endpoint.
func BookCountHandler(w http.ResponseWriter, r *http.Request) {
// Extract query parameters for language
......@@ -40,37 +42,41 @@ func BookCountHandler(w http.ResponseWriter, r *http.Request) {
}
func ReadershipHandler(w http.ResponseWriter, r *http.Request) {
// Extract the language code from the URL path
language := r.URL.Path[len("/librarystats/v1/readership/"):]
log.Printf("ReadershipHandler: Processing language: %s", language)
// Optional: Parse query parameters for limit if present
query := r.URL.Query()
limitParam := query.Get("limit")
var limit int
if limitParam != "" {
fmt.Sscanf(limitParam, "%d", &limit)
log.Printf("ReadershipHandler: Limit set to %d", limit)
}
// Fetch book data from the Gutendex API
books, err := services.FetchBooksByLanguage(language)
if err != nil {
log.Printf("ReadershipHandler: Error fetching books: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("ReadershipHandler: Fetched %d books", len(books.Results))
// Fetch countries for the given language
countries, err := services.FetchCountriesByLanguage(language)
countries, err := services.FetchCountriesByLanguage(language)
if err != nil {
log.Printf("ReadershipHandler: Error fetching countries: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("ReadershipHandler: Fetched %d countries", len(countries))
// Fetch population data for each country and calculate total readership
var readershipDetails []models.ReadershipDetail
var totalReadership int64 = 0
for _, country := range countries { // Directly range over countries
for _, country := range countries {
population, err := services.FetchPopulationByCountryCode(country.ISO3166_1_Alpha_2)
if err != nil {
log.Printf("ReadershipHandler: Error fetching population for country %s: %v", country.ISO3166_1_Alpha_2, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
......@@ -78,25 +84,30 @@ func ReadershipHandler(w http.ResponseWriter, r *http.Request) {
readershipDetail := models.ReadershipDetail{
Country: country.Official_Name,
ISOCode: country.ISO3166_1_Alpha_2, // Corrected field access
ISOCode: country.ISO3166_1_Alpha_2,
Books: len(books.Results),
Authors: services.CalculateUniqueAuthors(books.Results),
Readership: population,
}
readershipDetails = append(readershipDetails, readershipDetail)
// Apply limit if specified
if limit > 0 && len(readershipDetails) >= limit {
log.Printf("ReadershipHandler: Limit reached, stopping processing")
break
}
}
// Create and send the response
log.Printf("ReadershipHandler: Total readership calculated: %d", totalReadership)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(readershipDetails)
if err := json.NewEncoder(w).Encode(readershipDetails); err != nil {
log.Printf("ReadershipHandler: Error encoding response: %v", err)
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
} else {
log.Printf("ReadershipHandler: Response successfully sent")
}
}
func StatusHandler(w http.ResponseWriter, r *http.Request) {
status := models.ServiceStatus{
GutendexAPI: services.CheckServiceAvailability("http://129.241.150.113:8000/books/"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment