This repository has been archived on 2025-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Hochzeit/handlers/handlers.go
HuskyTeufel 227ddd81aa Revert "Umgeschrieben auf gorilla"
This reverts commit 318442cfaf.
2022-01-17 16:08:13 +01:00

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
}