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 (
|
||||
"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
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user