52 lines
1020 B
Go
52 lines
1020 B
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
|
|
core "fr.latosa-escrima/api/core"
|
|
)
|
|
|
|
func HandleCreateBlog(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 blog core.Blog
|
|
if err = json.Unmarshal(body, &blog); err != nil {
|
|
core.JSONError{
|
|
Status: core.Error,
|
|
Message: err.Error(),
|
|
}.Respond(w, http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
_, err = core.DB.NewInsert().Model(blog).Exec(context.Background())
|
|
if 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)
|
|
}
|