#Show all countries

3 messages · Page 1 of 1 (latest)

hot breach
#

So the scope of what im trying to do is something like:

  • Query Fake API endpoint
  • Save Data
  • Insert Data into my Database
  • Create my own endpoint
func PublicRoutes(router *chi.Mux) {
    // Routes for GET method:

    router.Get("/api/v1/user/{id}", controllers.GetUser)
    router.Get("/api/v1/users", controllers.GetUsers)
    router.Get("/api/v1", controllers.Index)
    router.Get("/api/v1/countries", controllers.GetAllCountries)
}

/api/v1/countries returns a 404 page not found, the other endpoints work properly.
the code for my controller is as it follows:
https://pastebin.com/f0Zw6EFX

I should probably start by making it smaller.
My api call is:

    httpClient := http.Client{}

    req, err := http.NewRequest("GET", "http://localhost:3000/data", nil)
    if err != nil {
        panic(err)
    }

    //q := req.URL.Query()
    //q.Add("access_key", "YOUR_ACCESS_KEY")
    //req.URL.RawQuery = q.Encode()

    res, err := httpClient.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        return nil, err
    }

    return body, nil
}```

And my queries used on the pastbin controller:
```go
func (q *CountryQueries) GetCountries() ([]models.Country, error) {

    // Define countries variable.
    var countries []models.Country

    // Send query to database.
    if err := q.Select(&countries, `SELECT * FROM country`); err != nil {
        return []models.Country{}, err
    }

    return countries, nil
}

Any new eyes could guide me on what I am doing wrong please?

#

the other query:

func (q *CountryQueries) CreateCountry(c *models.Country) error {
    // Send query to database.
    if _, err := q.Exec(
        `INSERT INTO country VALUES ($1, $2, $3, $4, $5, $6)`,
        c.CountryName,
        c.CountryIso2,
        c.CountryIso3,
        c.CountryIsoNumeric,
        c.Population,
        c.Capital,
        c.Continent,
        c.CurrencyName,
        c.CurrencyCode,
        c.FipsCode,
        c.PhonePrefix,
    ); err != nil {
        return fmt.Errorf("error inserting values: %w", err)
    }

    return nil
}
#

Controller is probably messed up but I can't debug the problem.