Merging with dev/cedric
This commit is contained in:
58
backend/api/blogs/blogs.go
Normal file
58
backend/api/blogs/blogs.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
}
|
||||
0
backend/api/blogs/delete_blog.go
Normal file
0
backend/api/blogs/delete_blog.go
Normal file
40
backend/api/blogs/new_blog.go
Normal file
40
backend/api/blogs/new_blog.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"io"
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
_, 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.NewDecoder(r.Body).Decode(&blog); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if _, err := core.DB.NewInsert().Model(&blog).Exec(context.Background()); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Blog inserted",
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
Reference in New Issue
Block a user