blogs api

This commit is contained in:
gom-by
2025-01-29 18:31:06 +01:00
parent 9bb72cd449
commit 107ff81e9e
7 changed files with 86 additions and 70 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)
}