Files
latosa-escrima/backend/api/new_event.go
2025-01-16 14:45:18 +01:00

52 lines
1.0 KiB
Go

package api
import (
"context"
"net/http"
"io"
"encoding/json"
core "fr.latosa-escrima/api/core"
)
func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
core.JSONError{
Status: core.Error,
Message: "Method is not allowed",
}.Respond(w, http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
var event core.Event
if err = json.Unmarshal(body, &event); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
_, err = core.DB.NewInsert().Model(&event).Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable)
}
core.JSONSuccess{
Status: core.Success,
Message: "Event inserted",
Data: event,
}.Respond(w, http.StatusCreated)
}