blogs api start

This commit is contained in:
gom-by
2025-01-28 18:23:42 +01:00
parent ac7e97527f
commit d27c257f6c
3 changed files with 26 additions and 16 deletions

View File

@@ -3,32 +3,21 @@ package api
import (
"context"
"encoding/json"
"io"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleCreateBlog(w http.ResponseWriter, r *http.Request) {
body, 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.Unmarshal(body, &blog); err != nil {
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}.Respond(w, http.StatusBadRequest)
}
_, err = core.DB.NewInsert().Model(blog).Exec(context.Background())
if err != nil {
if _, err := core.DB.NewInsert().Model(&blog).Exec(context.Background()); err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),