Besucher werden in den Datenbank gespeichert
This commit is contained in:
12
app.go
12
app.go
@@ -31,10 +31,14 @@ func (a *App) start() {
|
||||
}
|
||||
}))
|
||||
|
||||
a.r.GET("/besucher", handlers.GetBesucherListHandler)
|
||||
a.r.POST("/besucher", handlers.AddBesucherHandler)
|
||||
a.r.DELETE("/besucher/:id", handlers.DeleteBesucherHandler)
|
||||
a.r.PUT("/besucher", handlers.ComeBesucherHandler)
|
||||
env := handlers.EnvHandler{
|
||||
DB: a.db,
|
||||
}
|
||||
|
||||
a.r.GET("/besucher", env.GetBesucherListHandler)
|
||||
a.r.POST("/besucher", env.AddBesucherHandler)
|
||||
//a.r.DELETE("/besucher/:id", env.DeleteBesucherHandler)
|
||||
//a.r.PUT("/besucher", env.ComeBesucherHandler)
|
||||
|
||||
err := a.r.Run(":3001")
|
||||
if err != nil {
|
||||
|
||||
@@ -8,27 +8,32 @@ import (
|
||||
|
||||
"git.cosysda.de/HuskyTeufel/Hochzeit/planner"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
func GetBesucherListHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, planner.Get())
|
||||
type EnvHandler struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func AddBesucherHandler(c *gin.Context) {
|
||||
func (e *EnvHandler) GetBesucherListHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, planner.Get(e.DB))
|
||||
}
|
||||
|
||||
func (e *EnvHandler) AddBesucherHandler(c *gin.Context) {
|
||||
besucherItem, statusCode, err := convertHTTPBodyToBesucher(c.Request.Body)
|
||||
if err != nil {
|
||||
c.JSON(statusCode, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(statusCode, gin.H{"id": planner.Add(besucherItem)})
|
||||
c.JSON(statusCode, gin.H{"id": planner.Add(e.DB, besucherItem)})
|
||||
}
|
||||
|
||||
func DeleteBesucherHandler(c *gin.Context) {
|
||||
func (e *EnvHandler) DeleteBesucherHandler(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func ComeBesucherHandler(c *gin.Context) {
|
||||
func (e *EnvHandler) ComeBesucherHandler(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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