blogs and event api : Get and Post methods

This commit is contained in:
gom-by
2025-01-16 14:45:18 +01:00
parent 9a6e4a7565
commit 0c5a411ed1
5 changed files with 174 additions and 91 deletions

51
backend/api/new_event.go Normal file
View File

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