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

View File

@@ -2,14 +2,13 @@ package api
import (
"context"
"fmt"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
blog_uuid := r.PathValue("uuid")
var blog core.Blog
@@ -33,3 +32,25 @@ func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
}.Respond(w, http.StatusOK)
return
}
func HandleGetBlogs(w http.ResponseWriter, r *http.Request) {
var blog []core.Blog
count, err := core.DB.NewSelect().
Model(&blog).
Relation("Author").
ScanAndCount(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable)
return
}
core.JSONSuccess{
Status: core.Success,
Message: fmt.Sprint("%d blogs objects sent", count),
Data: blog,
}.Respond(w, http.StatusOK)
return
}

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(),