mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: draft/online post field #26
This commit is contained in:
@@ -148,6 +148,9 @@ func NewPageRouter() http.Handler {
|
||||
|
||||
r.Post("/post/{id}/image/create", postImage)
|
||||
|
||||
r.Get("/post/{id}/publish", GetPostPublishPage)
|
||||
r.Get("/post/{id}/unpublish", GetPostUnpublishPage)
|
||||
|
||||
r.Get("/integration", GetPageIntegration)
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func updateIsOnline(postId string, isOnline bool) (error, int) {
|
||||
postIdInt, err := strconv.Atoi(postId)
|
||||
if err != nil {
|
||||
return errors.New("the server expects the ID to be in the format of an unsigned 32-bit integer (uint32)"), http.StatusBadRequest
|
||||
}
|
||||
|
||||
_, err = Container.UpdatePostUseCase.UpdateIsOnline(uint32(postIdInt), isOnline)
|
||||
if err != nil {
|
||||
return errors.New("the requested resource, identified by its unique ID, could not be found on the server"), http.StatusNotFound
|
||||
}
|
||||
|
||||
return nil, http.StatusOK
|
||||
}
|
||||
|
||||
func GetPostUnpublishPage(w http.ResponseWriter, r *http.Request) {
|
||||
postId := chi.URLParam(r, "id")
|
||||
err, statusCode := updateIsOnline(postId, false)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), statusCode)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/post", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetPostPublishPage(w http.ResponseWriter, r *http.Request) {
|
||||
postId := chi.URLParam(r, "id")
|
||||
err, statusCode := updateIsOnline(postId, true)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), statusCode)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/post", http.StatusSeeOther)
|
||||
}
|
||||
@@ -13,6 +13,7 @@ func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
|
||||
formattedPosts = append(formattedPosts, map[string]interface{}{
|
||||
"ID": post.ID,
|
||||
"Title": post.Title,
|
||||
"IsOnline": post.IsOnline,
|
||||
"CreatedAt": post.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
"UpdatedAt": post.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
|
||||
+15
-9
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"GoCMS/domain/post"
|
||||
"GoCMS/useCases"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -22,33 +23,33 @@ func getPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
post, err := Container.GetPostUseCase.GetPost(uint32(id))
|
||||
localPost, err := Container.GetPostUseCase.GetPost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
postJson, _ := json.Marshal(post)
|
||||
postJson, _ := json.Marshal(localPost)
|
||||
_, _ = w.Write(postJson)
|
||||
}
|
||||
|
||||
func postPost(w http.ResponseWriter, r *http.Request) {
|
||||
var post PostPost
|
||||
err := json.NewDecoder(r.Body).Decode(&post)
|
||||
var localPost PostPost
|
||||
err := json.NewDecoder(r.Body).Decode(&localPost)
|
||||
if err != nil {
|
||||
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = validate.Struct(post)
|
||||
err = validate.Struct(localPost)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
createdPost, err := Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
Title: post.Title,
|
||||
Body: post.Body,
|
||||
Title: localPost.Title,
|
||||
Body: localPost.Body,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -61,8 +62,13 @@ func postPost(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func listPosts(w http.ResponseWriter, _ *http.Request) {
|
||||
posts := Container.ListPostsUseCase.ListPosts()
|
||||
postsJson, _ := json.Marshal(posts)
|
||||
|
||||
onlinePosts := make([]post.Post, 0)
|
||||
for _, localPost := range posts {
|
||||
if localPost.IsOnline {
|
||||
onlinePosts = append(onlinePosts, localPost)
|
||||
}
|
||||
}
|
||||
postsJson, _ := json.Marshal(onlinePosts)
|
||||
_, _ = w.Write(postsJson)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user