blogs and event api : Get and Post methods
This commit is contained in:
@@ -2,57 +2,43 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Println("salut")
|
||||
emptyObject := make(map[string]interface{})
|
||||
|
||||
emptyJSON, json_err := json.Marshal(emptyObject)
|
||||
if json_err != nil {
|
||||
fmt.Println("Couldn't create the json object")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(emptyJSON))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Wrong method", http.StatusMethodNotAllowed)
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method not Allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
blog := &core.Blog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
BlogID: [16]byte{},
|
||||
Slug: "",
|
||||
Content: "",
|
||||
Label: "",
|
||||
AuthorID: [16]byte{},
|
||||
Published: time.Time{},
|
||||
Summary: "",
|
||||
Image: "",
|
||||
Href: "",
|
||||
Author: &core.User{},
|
||||
}
|
||||
|
||||
blog_uuid := r.PathValue("uuid")
|
||||
fmt.Println(blog_uuid)
|
||||
ctx := context.Background()
|
||||
err_db := core.DB.NewSelect().Model(blog).Where("uuid = ?", blog_uuid).Scan(ctx)
|
||||
if err_db != nil {
|
||||
log.Fatal(err_db)
|
||||
http.Error(w, "Can't use select", http.StatusNotFound)
|
||||
return
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
w.Write([]byte(`{message: "Successfuly responded to request}`))
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Status OK",
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user