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

58
backend/api/blogs/blog.go Normal file
View File

@@ -0,0 +1,58 @@
package blogs
import (
"context"
"fmt"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleBlog(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
blog_uuid := r.PathValue("uuid")
var blog core.Blog
_, err := core.DB.NewSelect().
Model(&blog).
Where("blog_id = ?", blog_uuid).
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: "Status OK",
Data: blog,
}.Respond(w, http.StatusOK)
return
}
func HandleBlogs(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
}