probo/store/memory/participant.go

108 lines
3 KiB
Go

package memory
import (
"errors"
"fmt"
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/models"
"github.com/google/uuid"
)
func (s *MemoryProboCollectorStore) ReadAllParticipants() ([]*models.Participant, error) {
result := make([]*models.Participant, 0)
for id := range s.participants {
if participant := s.getParticipantFromID(id); participant != nil {
result = append(result, participant)
}
}
return result, nil
}
func (s *MemoryProboCollectorStore) getParticipantFromID(id string) *models.Participant {
s.lock.RLock()
defer s.lock.RUnlock()
participant, ok := s.participants[id]
if ok {
return participant
}
return nil
}
func (s *MemoryProboCollectorStore) CreateParticipant(r *client.CreateUpdateParticipantRequest) (*models.Participant, error) {
q, _, err := s.createOrUpdateParticipant(r, "")
return q, err
}
func (s *MemoryProboCollectorStore) UpdateParticipant(r *client.CreateUpdateParticipantRequest, id string) (*models.Participant, bool, error) {
return s.createOrUpdateParticipant(r, id)
}
func (s *MemoryProboCollectorStore) ReadParticipantByID(id string) (*models.Participant, error) {
if id == "" {
return nil, errors.New("ID should not be an empty string!")
}
participant := s.getParticipantFromID(id)
if participant == nil {
return nil, fmt.Errorf("Participant ID %v not found in the store", id)
}
return participant, nil
}
func (s *MemoryProboCollectorStore) DeleteParticipant(r *client.DeleteParticipantRequest) (*models.Participant, error) {
return s.deleteParticipant(r.ID)
}
func (s *MemoryProboCollectorStore) deleteParticipant(id string) (*models.Participant, error) {
s.lock.Lock()
defer s.lock.Unlock()
participant := s.participants[id]
if participant == nil {
return nil, fmt.Errorf("Trying to delete a participant that doesn't exist in memory (ID: %v)", id)
}
delete(s.participants, id)
return participant, nil
}
func (s *MemoryProboCollectorStore) createOrUpdateParticipant(r *client.CreateUpdateParticipantRequest, id string) (*models.Participant, bool, error) {
var participant *models.Participant
if r.Participant == nil {
return nil, false, errors.New("A request was made passing a nil participant object")
}
if id != "" { // we're updating a participant
participant = s.getParticipantFromID(id)
if participant == nil { // Participant is not present in the store
return nil, false, fmt.Errorf("Participant ID %v doesn't exist in the store!", id)
}
} else {
id = uuid.New().String()
participant = new(models.Participant)
}
participant.Attributes = make(map[string]string)
participant.Firstname = r.Participant.Firstname
participant.Lastname = r.Participant.Lastname
participant.Token = r.Participant.Token
participant.Attributes = r.Participant.Attributes
return s.createParticipantFromID(id, participant), true, nil
}
func (s *MemoryProboCollectorStore) createParticipantFromID(id string, participant *models.Participant) *models.Participant {
s.lock.Lock()
defer s.lock.Unlock()
participant.ID = id
s.participants[id] = participant
return participant
}