Umgeschrieben auf gorilla
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
package planner
|
||||
|
||||
type Besucher struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Come bool `json:"come"`
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user