-
Notifications
You must be signed in to change notification settings - Fork 0
/
memberdb.go
79 lines (64 loc) · 1.97 KB
/
memberdb.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
package gohoa
import (
"fmt"
"sort"
)
//Emulate a key/value store with a key lookup based on address
//This is a simple in-memory map, but could be extended to a database
//if the HOA has members in the thousands or more it would be wise to
func NewMemberDB() MemberDB {
outDb := new(memberMapDB)
outDb.mm = make(map[Mkey]*Member)
//byStreet currently just an aggreatation of all members on a street
outDb.byStreet = make(map[string][]*Member)
return outDb
}
func (mdb *memberMapDB) AddConvience(member *Member) {
key := MakeKey(member)
mdb.Add(key, member)
}
func (mdb *memberMapDB) Add(key Mkey, member *Member) {
mdb.byStreet[key.streetName] = append(mdb.byStreet[key.streetName], member)
mdb.mm[key] = member
}
func MakeKey(member *Member) Mkey {
mkey := Mkey{member.PAddress.StreetName, member.PAddress.Number}
return mkey
}
func MakeKeyV(street string, number int) Mkey {
mkey := Mkey{street, number}
return mkey
}
func (mk Mkey) String() string {
return fmt.Sprintf("Number:'%d' Street:'%s'", mk.streetNumber, mk.streetName)
}
func (mdb *memberMapDB) Fetch(key Mkey) (*Member, bool) {
member, found := mdb.mm[key]
return member, found
}
func (mdb *memberMapDB) Size() int {
return len(mdb.mm)
}
func (mdb *memberMapDB) AllMembers() []Member {
members := make([]Member, 0, len(mdb.mm))
for _, member := range mdb.mm {
members = append(members, *member)
}
return members
}
// getAllMKeys is a backdoor getting all the keys in the map
func (mdb *memberMapDB) getAllMKeys() []Mkey {
keys := make([]Mkey, 0, len(mdb.mm))
for key := range mdb.mm {
keys = append(keys, key)
}
return keys
}
// getAllMembersOnStreet is a backdoor getting all the members on a street
func (mdb *memberMapDB) getAllMembersOnStreet(street string) []*Member {
if streetMembers, found := mdb.byStreet[street]; found {
sort.Slice(streetMembers, func(i, j int) bool { return streetMembers[i].PAddress.Number < streetMembers[j].PAddress.Number })
return streetMembers
}
return []*Member{}
}