2019-08-01 11:34:03 -08:00
|
|
|
package geonames
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-17 11:03:48 +07:00
|
|
|
|
2019-08-01 11:34:03 -08:00
|
|
|
"github.com/huandu/go-sqlbuilder"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// The database table for Country
|
|
|
|
countriesTableName = "countries"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindCountries ....
|
2019-08-17 11:03:48 +07:00
|
|
|
func (repo *Repository) FindCountries(ctx context.Context, orderBy, where string, args ...interface{}) ([]*Country, error) {
|
2019-08-01 11:34:03 -08:00
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.geonames.FindCountries")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
query := sqlbuilder.NewSelectBuilder()
|
|
|
|
query.Select("code,iso_alpha3,name,capital,currency_code,currency_name,phone,postal_code_format,postal_code_regex")
|
|
|
|
query.From(countriesTableName)
|
|
|
|
|
|
|
|
if orderBy == "" {
|
|
|
|
orderBy = "name"
|
|
|
|
}
|
|
|
|
query.OrderBy(orderBy)
|
|
|
|
|
|
|
|
if where != "" {
|
|
|
|
query.Where(where)
|
|
|
|
}
|
|
|
|
|
|
|
|
queryStr, queryArgs := query.Build()
|
2019-08-17 11:03:48 +07:00
|
|
|
queryStr = repo.DbConn.Rebind(queryStr)
|
2019-08-01 11:34:03 -08:00
|
|
|
args = append(args, queryArgs...)
|
|
|
|
|
|
|
|
// fetch all places from the db
|
2019-08-17 11:03:48 +07:00
|
|
|
rows, err := repo.DbConn.QueryContext(ctx, queryStr, args...)
|
2019-08-01 11:34:03 -08:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrapf(err, "query - %s", query.String())
|
|
|
|
err = errors.WithMessage(err, "find countries failed")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over each row
|
|
|
|
resp := []*Country{}
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
v Country
|
|
|
|
err error
|
|
|
|
)
|
2019-08-01 13:45:38 -08:00
|
|
|
err = rows.Scan(&v.Code, &v.IsoAlpha3, &v.Name, &v.Capital, &v.CurrencyCode, &v.CurrencyName, &v.Phone, &v.PostalCodeFormat, &v.PostalCodeRegex)
|
2019-08-01 11:34:03 -08:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrapf(err, "query - %s", query.String())
|
|
|
|
return nil, err
|
|
|
|
} else if v.Code == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = append(resp, &v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|