-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.go
206 lines (171 loc) · 4.62 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package hjem
import (
"errors"
"regexp"
"strconv"
"strings"
"gorm.io/gorm"
)
var (
ErrInvalidZipCodes = errors.New("invalid zip code format")
ErrInvalidPropertyType = errors.New("invalid property type format")
ErrUnknownAddr = errors.New("unknown address")
)
type Config struct {
PropertyTypes []string
ZipCodeFrom *int
ZipCodeTo *int
}
func NewConfig(zipcodes, properties string) (*Config, error) {
var conf Config
if zipcodes != "" {
zip, err := strconv.Atoi(zipcodes)
if err == nil {
return &Config{
ZipCodeFrom: &zip,
ZipCodeTo: &zip,
}, nil
}
splits := strings.Split(zipcodes, "-")
if len(splits) != 2 {
return nil, ErrInvalidZipCodes
}
from, err := strconv.Atoi(splits[0])
if err != nil {
return nil, ErrInvalidZipCodes
}
to, err := strconv.Atoi(splits[1])
if err != nil {
return nil, ErrInvalidZipCodes
}
conf.ZipCodeFrom = &from
conf.ZipCodeTo = &to
}
if properties != "" {
valid := map[string]bool{
"house": true,
"apartment": true,
"vacation": true,
}
splits := strings.Split(properties, ",")
if len(splits) == 0 {
if _, ok := valid[properties]; !ok {
return nil, ErrInvalidPropertyType
}
conf.PropertyTypes = []string{properties}
} else {
for _, p := range strings.Split(properties, ",") {
if _, ok := valid[p]; !ok {
return nil, ErrInvalidPropertyType
}
}
conf.PropertyTypes = splits
}
}
return &conf, nil
}
// type Address struct {
// ID uint `gorm:"primaryKey"`
// DawaID string `gorm:"not null,unique"`
// StreetName string `gorm:"index:addr_idx,not null"`
// StreetNumber string `gorm:"index:addr_idx,not null"`
// Floor sql.NullString `gorm:"index:addr_idx"`
// Door sql.NullString `gorm:"index:addr_idx"`
// PostalCode int `gorm:"index:addr_idx,not null"`
// MunicipalityCode string `gorm:"not null"`
// Longtitude float64 `gorm:"not null"`
// Latitude float64 `gorm:"not null"`
// }
// type Sale struct {
// AddressID uint `gorm:"primaryKey"`
// SoldDate time.Time `gorm:"primaryKey"`
// BoligaEstateId int `gorm:"not null"`
// AmountDKK int `gorm:"not null"`
// PropertyType int `gorm:"not null"`
// SqMeters int `gorm:"not null"`
// Rooms int
// BuildYear int `gorm:"not null"`
// PriceChange float64
// SaleType string `gorm:"not null"`
// }
type Store struct {
db *gorm.DB
}
func NewStore(db *gorm.DB) (*Store, error) {
db.AutoMigrate(&Address{})
// db.AutoMigrate(&Sale{})
return &Store{
db: db,
}, nil
}
func (s *Store) CountAddresses() int64 {
var count int64
s.db.Model(Address{}).Count(&count)
return count
}
func (s *Store) StreamAddrs(addrC <-chan Address) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var batch []Address
for addr := range addrC {
if len(batch) > 100 {
s.db.Create(&batch)
batch = []Address{}
}
batch = append(batch, addr)
}
if len(batch) > 0 {
s.db.Create(&batch)
batch = []Address{}
}
return nil
})
}
var (
addrRegexp = regexp.MustCompile(`([a-zA-Z\p{L}-. ]+) ([0-9][a-zA-Z0-9\p{L}]*)(, ([a-zA-Z0-9\p{L}]+)\.?)?( ([a-zA-Z0-9\p{L}]+)\.?)?`)
)
// func (s *Store) SaveSale(sale BoligaSale) error {
// matches := addrRegexp.FindAllStringSubmatch(sale.Addr, 1)
// if len(matches) == 0 {
// return fmt.Errorf("unable to parse address")
// }
// query := map[string]interface{}{
// "street_name": matches[0][1],
// "street_number": matches[0][2],
// "floor": nil,
// "door": nil,
// "postal_code": sale.ZipCode,
// }
// if floor := matches[0][4]; floor != "" {
// query["floor"] = floor
// }
// if door := matches[0][6]; door != "" {
// query["door"] = door
// }
// var addrs []Address
// s.db.Find(&addrs, query)
// if len(addrs) == 0 {
// results, _ := DawaFuzzySearch{fmt.Sprintf("%s, %d", sale.Addr, sale.ZipCode)}.Fetch()
// if len(results) != 1 {
// return ErrUnknownAddr
// }
// s.db.Find(&addrs, map[string]interface{}{"dawa_id": results[0].ID})
// if len(addrs) != 1 {
// return ErrUnknownAddr
// }
// }
// addr := addrs[0]
// saleR := Sale{
// AddressID: addr.ID,
// SoldDate: sale.SoldDate,
// BoligaEstateId: sale.EstateId,
// AmountDKK: sale.AmountDKK,
// PropertyType: sale.PropertyType,
// SqMeters: sale.SqMeters,
// Rooms: int(sale.Rooms),
// BuildYear: sale.BuildYear,
// PriceChange: sale.PriceChange,
// SaleType: sale.SaleType,
// }
// s.db.Create(&saleR)
// return nil
// }