This commit is contained in:
cdricms
2025-01-29 09:17:09 +01:00
parent 9bb72cd449
commit 7c66353e63
4 changed files with 5 additions and 54 deletions

40
backend/api/blogs/new.go Normal file
View File

@@ -0,0 +1,40 @@
package blogs
import (
"context"
"encoding/json"
"net/http"
"io"
core "fr.latosa-escrima/api/core"
)
func HandleNew(w http.ResponseWriter, r *http.Request) {
_, err := io.ReadAll(r.Body)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
var blog core.Blog
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
}
if _, err := core.DB.NewInsert().Model(&blog).Exec(context.Background()); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable)
}
core.JSONSuccess{
Status: core.Success,
Message: "Blog inserted",
Data: blog,
}.Respond(w, http.StatusCreated)
}