77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
package planner
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/rs/xid"
|
|
)
|
|
|
|
var (
|
|
list []Besucher
|
|
mtx sync.RWMutex
|
|
once sync.Once
|
|
)
|
|
|
|
func init() {
|
|
once.Do(initialiseList)
|
|
}
|
|
|
|
func initialiseList() {
|
|
list = []Besucher{}
|
|
}
|
|
|
|
// Get retrieves all elements from the Besucher list
|
|
func Get() []Besucher {
|
|
return list
|
|
}
|
|
|
|
// Add will add a new Besucher
|
|
func Add(newClient Besucher) string {
|
|
t := newBesucher(newClient)
|
|
mtx.Lock()
|
|
list = append(list, t)
|
|
mtx.Unlock()
|
|
return t.ID
|
|
}
|
|
|
|
// Delete will remove a Besucher from the Besucher list
|
|
func Delete(id string) error {
|
|
location, err := findBesucherLocation(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
removeElementByLocation(location)
|
|
return nil
|
|
}
|
|
|
|
func removeElementByLocation(i int) {
|
|
mtx.Lock()
|
|
list = append(list[:i], list[i+1:]...)
|
|
mtx.Unlock()
|
|
}
|
|
|
|
func findBesucherLocation(id string) (int, error) {
|
|
mtx.RLock()
|
|
defer mtx.RUnlock()
|
|
for i, t := range list {
|
|
if isMatchingID(t.ID, id) {
|
|
return i, nil
|
|
}
|
|
}
|
|
return 0, errors.New("Could not find Besucher based on id")
|
|
}
|
|
|
|
func isMatchingID(a, b string) bool {
|
|
return a == b
|
|
}
|
|
|
|
func newBesucher(newBesucher Besucher) Besucher {
|
|
return Besucher{
|
|
ID: xid.New().String(),
|
|
Name: newBesucher.Name,
|
|
Message: newBesucher.Message,
|
|
Come: newBesucher.Come,
|
|
}
|
|
}
|