52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"git.cosysda.de/HuskyTeufel/Hochzeit/planner"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func GetBesucherListHandler(c *gin.Context) {
|
|
c.JSON(http.StatusOK, planner.Get())
|
|
}
|
|
|
|
func 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)})
|
|
}
|
|
|
|
func DeleteBesucherHandler(c *gin.Context) {
|
|
|
|
}
|
|
|
|
func ComeBesucherHandler(c *gin.Context) {
|
|
|
|
}
|
|
|
|
func convertHTTPBodyToBesucher(httpBody io.ReadCloser) (planner.Besucher, int, error) {
|
|
body, err := ioutil.ReadAll(httpBody)
|
|
if err != nil {
|
|
return planner.Besucher{}, http.StatusInternalServerError, err
|
|
}
|
|
defer httpBody.Close()
|
|
return convertJSONBodyToBesucher(body)
|
|
}
|
|
|
|
func convertJSONBodyToBesucher(jsonBody []byte) (planner.Besucher, int, error) {
|
|
var besucherItem planner.Besucher
|
|
err := json.Unmarshal(jsonBody, &besucherItem)
|
|
if err != nil {
|
|
return planner.Besucher{}, http.StatusBadRequest, err
|
|
}
|
|
return besucherItem, http.StatusOK, nil
|
|
}
|