Besucher werden in den Datenbank gespeichert
This commit is contained in:
@@ -1,71 +1,29 @@
|
||||
package planner
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"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
|
||||
func Get(db *gorm.DB) []Besucher {
|
||||
var besucherlist []Besucher
|
||||
db.Find(&besucherlist)
|
||||
return besucherlist
|
||||
}
|
||||
|
||||
// Add will add a new Besucher
|
||||
func Add(newClient Besucher) string {
|
||||
func Add(db *gorm.DB, newClient Besucher) string {
|
||||
t := newBesucher(newClient)
|
||||
mtx.Lock()
|
||||
list = append(list, t)
|
||||
mtx.Unlock()
|
||||
db.Create(&t)
|
||||
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(),
|
||||
|
||||
Reference in New Issue
Block a user