bottin/handlers/read.go

62 lines
1.4 KiB
Go
Raw Normal View History

2023-05-25 02:21:09 -04:00
package handlers
import (
"fmt"
2023-05-25 02:21:09 -04:00
"net/http"
"git.agecem.com/agecem/bottin/v6/responses"
2023-05-25 02:21:09 -04:00
"github.com/labstack/echo/v4"
)
func (h *Handler) ReadMembre(c echo.Context) error {
2023-05-25 02:21:09 -04:00
membreID := c.Param("membre_id")
membre, err := h.DataClient.GetMembre(membreID)
2023-05-25 02:21:09 -04:00
if err != nil {
if err.Error() == "No membre by that id was found" {
return c.JSON(http.StatusNotFound, map[string]string{
"message": "Not Found",
})
}
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Unknown error during GetMembre",
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "Read successful",
"data": map[string]interface{}{
"membre": &membre,
},
})
}
func (h *Handler) ListMembres(c echo.Context) error {
var r responses.ListMembresResponse
membres, err := h.DataClient.GetMembres()
if err != nil {
r.StatusCode = http.StatusInternalServerError
r.Error = err.Error()
r.Message = "Error during (*handlers.Handler).DataClient.GetMembres"
return c.JSON(r.StatusCode, r)
}
r.StatusCode = http.StatusOK
switch membres := len(membres); membres {
case 0:
r.Message = "No membres returned from database"
case 1:
r.Message = "Membre returned from database"
default:
r.Message = fmt.Sprintf("%d membres returned from database", membres)
}
r.Data.Membres = membres
return c.JSON(r.StatusCode, r)
}