blogs api start
This commit is contained in:
0
backend/api/delete_blog.go
Normal file
0
backend/api/delete_blog.go
Normal file
@@ -2,14 +2,13 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
blog_uuid := r.PathValue("uuid")
|
blog_uuid := r.PathValue("uuid")
|
||||||
|
|
||||||
var blog core.Blog
|
var blog core.Blog
|
||||||
@@ -33,3 +32,25 @@ func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
return
|
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
|
||||||
|
}
|
||||||
@@ -3,32 +3,21 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleCreateBlog(w http.ResponseWriter, r *http.Request) {
|
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
|
var blog core.Blog
|
||||||
if err = json.Unmarshal(body, &blog); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}.Respond(w, http.StatusNoContent)
|
}.Respond(w, http.StatusBadRequest)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = core.DB.NewInsert().Model(blog).Exec(context.Background())
|
if _, err := core.DB.NewInsert().Model(&blog).Exec(context.Background()); err != nil {
|
||||||
if err != nil {
|
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
|
|||||||
Reference in New Issue
Block a user