blogs api
This commit is contained in:
@@ -35,7 +35,7 @@ func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func HandleGetBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
var blog []core.Blog
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
32
backend/api/blogs/delete.go
Normal file
32
backend/api/blogs/delete.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("blog_uuid")
|
||||
var blog core.Blog
|
||||
res, err := core.DB.NewDelete().
|
||||
Model(&blog).
|
||||
Where("blog_id = ?", uuid).
|
||||
Returning("*").
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Println(res)
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Blog deleted.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
53
backend/api/blogs/update.go
Normal file
53
backend/api/blogs/update.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var blog core.Blog
|
||||
err := json.NewDecoder(r.Body).Decode(&blog)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
blog_uuid := r.PathValue("blog_uuid")
|
||||
blog.BlogID, err = uuid.Parse(blog_uuid)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(blog)
|
||||
_, err = core.DB.NewUpdate().
|
||||
Model(&blog).
|
||||
OmitZero().
|
||||
WherePK().
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Blog not found.",
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Blog updated",
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
@@ -32,17 +32,6 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
log.Println(event)
|
||||
|
||||
// val := reflect.ValueOf(event)
|
||||
// typ := val.Type()
|
||||
//
|
||||
// for i := 0; i < val.NumField(); i++ {
|
||||
// field := val.Field(i)
|
||||
// fieldType := typ.Field(i)
|
||||
// fmt.Printf("Field Name: %s, Field Value: %v\n", fieldType.Name, field.Interface())
|
||||
// if fiel
|
||||
// }
|
||||
|
||||
_, err = core.DB.NewUpdate().
|
||||
Model(&event).
|
||||
OmitZero().
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
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 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
|
||||
}
|
||||
Reference in New Issue
Block a user