Revert "Umgeschrieben auf gorilla"

This reverts commit 318442cfaf.
This commit is contained in:
HuskyTeufel
2022-01-17 16:08:13 +01:00
parent 318442cfaf
commit 227ddd81aa
7 changed files with 284 additions and 119 deletions

51
handlers/handlers.go Normal file
View File

@@ -0,0 +1,51 @@
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
}