Skip to content
Snippets Groups Projects
Commit 7a150ed2 authored by Jonah Husson's avatar Jonah Husson
Browse files

Mapimpl should properly upsert to match db

parent 1c58c9bd
Branches
No related tags found
1 merge request!12Add new columns to db, info call to api
......@@ -85,8 +85,8 @@ func newDatabase(username, password, dbName, address,
defer jww.INFO.Println("Map backend initialized successfully!")
mapImpl := &MapImpl{
members: map[string]Member{},
commitments: map[string]Commitment{},
members: map[string]*Member{},
commitments: map[string]*Commitment{},
}
return database(mapImpl), nil
......
......@@ -16,8 +16,8 @@ import (
// MapImpl struct implements the database interface with an underlying Map
type MapImpl struct {
members map[string]Member
commitments map[string]Commitment
members map[string]*Member
commitments map[string]*Commitment
sync.RWMutex
}
......@@ -25,7 +25,7 @@ func (db *MapImpl) InsertMembers(members []Member) error {
db.Lock()
defer db.Unlock()
for _, m := range members {
db.members[base64.StdEncoding.EncodeToString(m.Id)] = m
db.members[base64.StdEncoding.EncodeToString(m.Id)] = &m
}
return nil
}
......@@ -33,7 +33,14 @@ func (db *MapImpl) InsertMembers(members []Member) error {
func (db *MapImpl) InsertCommitment(commitment Commitment) error {
db.Lock()
defer db.Unlock()
db.commitments[string(commitment.Id)] = commitment
if _, ok := db.commitments[string(commitment.Id)]; ok {
db.commitments[string(commitment.Id)].NominatorWallet = commitment.NominatorWallet
db.commitments[string(commitment.Id)].SelectedStake = commitment.SelectedStake
db.commitments[string(commitment.Id)].Email = commitment.Email
db.commitments[string(commitment.Id)].Wallet = commitment.Wallet
} else {
db.commitments[string(commitment.Id)] = &commitment
}
return nil
}
......@@ -51,7 +58,7 @@ func (db *MapImpl) GetMember(id string) (*Member, error) {
if !ok {
return nil, errors.Errorf("No member in MapImpl with id %+v", id)
}
return &m, nil
return m, nil
}
func (db *MapImpl) GetCommitment(id string) (*Commitment, error) {
......@@ -67,5 +74,5 @@ func (db *MapImpl) GetCommitment(id string) (*Commitment, error) {
if !ok {
return nil, errors.Errorf("No commitment in MapImpl with id %+v", id)
}
return &c, nil
return c, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment