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

56
backend/api/get_blog.go Normal file
View File

@@ -0,0 +1,56 @@
package api
import (
"context"
"fmt"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
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 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
}