backend fertig gestellt
frontend angefangen
This commit is contained in:
8
planner/besucher.go
Normal file
8
planner/besucher.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package planner
|
||||
|
||||
type Besucher struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Come bool `json:"come"`
|
||||
}
|
||||
75
planner/planner.go
Normal file
75
planner/planner.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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(message string) string {
|
||||
t := newBesucher(message)
|
||||
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(message string) Besucher {
|
||||
return Besucher{
|
||||
ID: xid.New().String(),
|
||||
Message: message,
|
||||
Come: false,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user