Guest User

Untitled

a guest
Apr 19th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.28 KB | None | 0 0
  1. func GetAllCountries(w http.ResponseWriter, r *http.Request) {
  2.     // Open a database connection and defer its closure
  3.     db, err := database.OpenDBConnection()
  4.     if err != nil {
  5.         http.Error(w, err.Error(), http.StatusInternalServerError)
  6.         return
  7.     }
  8.     defer db.Close()
  9.  
  10.     // Try to get countries from the database
  11.     countries, err := db.GetCountries()
  12.     if err != nil {
  13.         log.Printf("error getting countries from database: %v", err)
  14.         http.Error(w, err.Error(), http.StatusInternalServerError)
  15.         return
  16.     }
  17.  
  18.     // If there are no countries in the database, fetch them from the API
  19.     if len(countries) == 0 {
  20.         body, err := api.GetAPICountries()
  21.         if err != nil {
  22.             log.Printf("error getting countries from API: %v", err)
  23.             http.Error(w, err.Error(), http.StatusInternalServerError)
  24.             return
  25.         }
  26.  
  27.         var countryResponse models.CountryResponse
  28.         err = json.Unmarshal(body, &countryResponse)
  29.         if err != nil {
  30.             log.Printf("error unmarshaling API response: %v", err)
  31.             http.Error(w, err.Error(), http.StatusInternalServerError)
  32.             return
  33.         }
  34.  
  35.         for _, c := range countryResponse.CountryList {
  36.             err := db.CreateCountry(&models.Country{
  37.                 CountryName:       c.CountryName,
  38.                 CountryIso2:       c.CountryIso2,
  39.                 CountryIso3:       c.CountryIso3,
  40.                 CountryIsoNumeric: c.CountryIsoNumeric,
  41.                 Population:        c.Population,
  42.                 Capital:           c.Capital,
  43.                 Continent:         c.Continent,
  44.                 CurrencyName:      c.CurrencyName,
  45.                 CurrencyCode:      c.CurrencyCode,
  46.                 FipsCode:          c.FipsCode,
  47.                 PhonePrefix:       c.PhonePrefix,
  48.             })
  49.             if err != nil {
  50.                 log.Printf("error creating country in database: %v", err)
  51.                 http.Error(w, err.Error(), http.StatusInternalServerError)
  52.                 return
  53.             }
  54.         }
  55.  
  56.         // Refresh the countries from the database after inserting them
  57.         countries, err = db.GetCountries()
  58.         if err != nil {
  59.             log.Printf("error getting countries from database: %v", err)
  60.             http.Error(w, err.Error(), http.StatusInternalServerError)
  61.             return
  62.         }
  63.     }
  64.  
  65.     // Return the countries as a JSON response
  66.     w.Header().Set("Content-Type", "application/json")
  67.     err = json.NewEncoder(w).Encode(countries)
  68.     if err != nil {
  69.         log.Printf("error encoding countries as JSON: %v", err)
  70.         http.Error(w, err.Error(), http.StatusInternalServerError)
  71.         return
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment