location.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Address1 dbr.NullString `db:"address_1"`
  12. Address2 dbr.NullString `db:"address_2"`
  13. City dbr.NullString `db:"city"`
  14. Country dbr.NullString `db:"country"`
  15. CountryCode dbr.NullString `db:"country_code"`
  16. Latitude dbr.NullFloat64 `db:"latitude"`
  17. Longitude dbr.NullFloat64 `db:"longitude"`
  18. Phone dbr.NullString `db:"phone"`
  19. Province dbr.NullString `db:"province"`
  20. ProvinceCode dbr.NullString `db:"province_code"`
  21. Zip dbr.NullString `db:"zip"`
  22. }
  23. func (l *Location) As() *generated.Location {
  24. var (
  25. fmtProvince = strings.Builder{}
  26. loc = &generated.Location{
  27. Address: &generated.LocationAddress{},
  28. ID: model.NewId(model.GidLocation, l.Id),
  29. Name: l.Name,
  30. }
  31. )
  32. if l.Address1.Valid {
  33. loc.Address.Address1 = &l.Address1.String
  34. loc.Address.Formatted = append(loc.Address.Formatted, l.Address1.String)
  35. }
  36. if l.Address2.Valid {
  37. loc.Address.Address2 = &l.Address2.String
  38. loc.Address.Formatted = append(loc.Address.Formatted, l.Address2.String)
  39. }
  40. if l.City.Valid {
  41. loc.Address.City = &l.City.String
  42. loc.Address.Formatted = append(loc.Address.Formatted, l.City.String)
  43. }
  44. if l.ProvinceCode.Valid {
  45. loc.Address.ProvinceCode = &l.ProvinceCode.String
  46. fmtProvince.WriteString(l.ProvinceCode.String)
  47. }
  48. if l.Province.Valid {
  49. loc.Address.Province = &l.Province.String
  50. if fmtProvince.Len() == 0 {
  51. fmtProvince.WriteString(l.Province.String)
  52. }
  53. }
  54. if l.Zip.Valid {
  55. loc.Address.Zip = &l.Zip.String
  56. if fmtProvince.Len() == 0 {
  57. fmtProvince.WriteString(" ")
  58. }
  59. fmtProvince.WriteString(l.Zip.String)
  60. }
  61. if fmtProvince.Len() > 0 {
  62. loc.Address.Formatted = append(loc.Address.Formatted, fmtProvince.String())
  63. }
  64. if l.Country.Valid {
  65. loc.Address.Country = &l.Country.String
  66. loc.Address.Formatted = append(loc.Address.Formatted, l.Country.String)
  67. }
  68. if code := model.CountryCode(l.CountryCode.String); code.IsValid() {
  69. s := code.String()
  70. loc.Address.CountryCode = &s
  71. }
  72. if l.Latitude.Valid {
  73. loc.Address.Latitude = &l.Latitude.Float64
  74. }
  75. if l.Longitude.Valid {
  76. loc.Address.Longitude = &l.Longitude.Float64
  77. }
  78. if l.Phone.Valid {
  79. loc.Address.Phone = &l.Phone.String
  80. }
  81. return loc
  82. }