location.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package relation
  2. import (
  3. "github.com/gshopify/service-wrapper/model"
  4. "github.com/mailru/dbr"
  5. "gshopper.com/gshopify/shop/graphql/generated"
  6. "strings"
  7. )
  8. type Location struct {
  9. Id string `db:"id"`
  10. Name string `db:"name"`
  11. Status string `db:"status"`
  12. Legacy bool `db:"legacy"`
  13. Address1 dbr.NullString `db:"address_1"`
  14. Address2 dbr.NullString `db:"address_2"`
  15. City dbr.NullString `db:"city"`
  16. Country dbr.NullString `db:"country"`
  17. CountryCode dbr.NullString `db:"country_code"`
  18. Latitude dbr.NullFloat64 `db:"latitude"`
  19. Longitude dbr.NullFloat64 `db:"longitude"`
  20. Phone dbr.NullString `db:"phone"`
  21. Province dbr.NullString `db:"province"`
  22. ProvinceCode dbr.NullString `db:"province_code"`
  23. Zip dbr.NullString `db:"zip"`
  24. LocalizedCountryName dbr.NullString `db:"localized_country_name"`
  25. LocalizedProvinceName dbr.NullString `db:"localized_province_name"`
  26. }
  27. func (l *Location) As() *generated.Location {
  28. var (
  29. fmtProvince = strings.Builder{}
  30. loc = &generated.Location{
  31. Address: &generated.LocationAddress{},
  32. ID: model.NewId(model.GidLocation, l.Id),
  33. Name: l.Name,
  34. }
  35. )
  36. if l.Address1.Valid {
  37. loc.Address.Address1 = &l.Address1.String
  38. loc.Address.Formatted = append(loc.Address.Formatted, l.Address1.String)
  39. }
  40. if l.Address2.Valid {
  41. loc.Address.Address2 = &l.Address2.String
  42. loc.Address.Formatted = append(loc.Address.Formatted, l.Address2.String)
  43. }
  44. if l.City.Valid {
  45. loc.Address.City = &l.City.String
  46. loc.Address.Formatted = append(loc.Address.Formatted, l.City.String)
  47. }
  48. if l.ProvinceCode.Valid {
  49. loc.Address.ProvinceCode = &l.ProvinceCode.String
  50. fmtProvince.WriteString(l.ProvinceCode.String)
  51. }
  52. if l.Province.Valid {
  53. loc.Address.Province = &l.Province.String
  54. if fmtProvince.Len() == 0 {
  55. fmtProvince.WriteString(l.Province.String)
  56. }
  57. }
  58. if l.Zip.Valid {
  59. loc.Address.Zip = &l.Zip.String
  60. if fmtProvince.Len() == 0 {
  61. fmtProvince.WriteString(" ")
  62. }
  63. fmtProvince.WriteString(l.Zip.String)
  64. }
  65. if fmtProvince.Len() > 0 {
  66. loc.Address.Formatted = append(loc.Address.Formatted, fmtProvince.String())
  67. }
  68. if l.Country.Valid {
  69. loc.Address.Country = &l.Country.String
  70. loc.Address.Formatted = append(loc.Address.Formatted, l.Country.String)
  71. }
  72. if code := model.CountryCode(l.CountryCode.String); code.IsValid() {
  73. s := code.String()
  74. loc.Address.CountryCode = &s
  75. }
  76. if l.Latitude.Valid {
  77. loc.Address.Latitude = &l.Latitude.Float64
  78. }
  79. if l.Longitude.Valid {
  80. loc.Address.Longitude = &l.Longitude.Float64
  81. }
  82. if l.Phone.Valid {
  83. loc.Address.Phone = &l.Phone.String
  84. }
  85. return loc
  86. }