mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
refac: renamed 'post' -> 'article'
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/useCases"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ArticleCreatePageError struct {
|
||||
IsError bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func GetArticleCreatePageTemplate(postName string, errorMessage string) ([]byte, error) {
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
return api.Container.GetPageUseCase.GetPage("articleCreate", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"PageError": ArticleCreatePageError{
|
||||
IsError: errorMessage != "",
|
||||
Message: errorMessage,
|
||||
},
|
||||
"Name": postName,
|
||||
})
|
||||
}
|
||||
|
||||
func PostArticleCreatePage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
postName := r.FormValue("name")
|
||||
pattern := regexp.MustCompile("^[a-zA-Z0-9À-ÖØ-öø-ÿĀ-ſḀ-ỿ ]{3,50}$")
|
||||
|
||||
if !pattern.MatchString(postName) {
|
||||
postsTmpl, _ := GetArticleCreatePageTemplate(postName, "Name should be alphanumeric, and between 3 and 50 characters.")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
return
|
||||
}
|
||||
|
||||
post, err := api.Container.CreateArticleUseCase.CreateArticle(useCases.CreateArticleCommand{
|
||||
Title: postName,
|
||||
Body: "",
|
||||
})
|
||||
if err != nil {
|
||||
postsTmpl, _ := GetArticleCreatePageTemplate(postName, "Something went wrong when creating the article, please contact admin.")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/article/"+strconv.Itoa(int(post.ID))+"/edit", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetArticleCreatePage(w http.ResponseWriter, _ *http.Request) {
|
||||
postsTmpl, _ := GetArticleCreatePageTemplate("", "")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/post"
|
||||
"GoCMS/api/controllers/article"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -10,21 +10,21 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
func GetArticleDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||
if err != nil {
|
||||
http.Error(w, post.IdUint32ErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, article.IdUint32ErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
localPost, err := api.Container.GetPostUseCase.GetPost(uint32(id))
|
||||
localArticle, err := api.Container.GetArticleUseCase.GetArticle(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(localPost.Images)
|
||||
for _, image := range localPost.Images {
|
||||
fmt.Println(localArticle.Images)
|
||||
for _, image := range localArticle.Images {
|
||||
err = api.Container.DeleteImageUseCase.DeleteImage(image.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
@@ -32,11 +32,11 @@ func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
err = api.Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
err = api.Container.DeleteArticleUseCase.DeleteArticle(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/post", http.StatusSeeOther)
|
||||
http.Redirect(w, r, "/article", http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/domain/article"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type ArticleEditPageAlert struct {
|
||||
IsError bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func getArticleEditPageTemplate(post article.Article, alert ArticleEditPageAlert) []byte {
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
articleTmpl, _ := api.Container.GetPageUseCase.GetPage("articleEdit", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Article": post,
|
||||
"Alert": alert,
|
||||
"Secured": os.Getenv("ENVIRONMENT") == "production",
|
||||
})
|
||||
return articleTmpl
|
||||
}
|
||||
|
||||
func PostArticleEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
articleID := chi.URLParam(r, "id")
|
||||
articleIDint, err := strconv.Atoi(articleID)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getArticleEditPageTemplate(article.Article{}, ArticleEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not find the requested article.",
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_ = r.ParseForm()
|
||||
articleBody := r.FormValue("articleBody")
|
||||
|
||||
getArticle, _ := api.Container.GetArticleUseCase.GetArticle(uint32(articleIDint))
|
||||
updatedArticle, err := api.Container.UpdateArticleUseCase.UpdateBody(getArticle.ID, articleBody)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getArticleEditPageTemplate(article.Article{Body: articleBody}, ArticleEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not save the article: " + err.Error(),
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write(getArticleEditPageTemplate(updatedArticle, ArticleEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "Article successfully edited!",
|
||||
}))
|
||||
}
|
||||
|
||||
func GetArticleEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
articleID := chi.URLParam(r, "id")
|
||||
articleIDint, err := strconv.Atoi(articleID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
getArticle, _ := api.Container.GetArticleUseCase.GetArticle(uint32(articleIDint))
|
||||
|
||||
_, _ = w.Write(getArticleEditPageTemplate(getArticle, ArticleEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "",
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func updateIsOnline(articleId string, isOnline bool) (error, int) {
|
||||
articleIdInt, err := strconv.Atoi(articleId)
|
||||
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 = api.Container.UpdateArticleUseCase.UpdateIsOnline(uint32(articleIdInt), 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 GetArticleUnpublishPage(w http.ResponseWriter, r *http.Request) {
|
||||
articleId := chi.URLParam(r, "id")
|
||||
err, statusCode := updateIsOnline(articleId, false)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), statusCode)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/article", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetArticlePublishPage(w http.ResponseWriter, r *http.Request) {
|
||||
articleId := chi.URLParam(r, "id")
|
||||
err, statusCode := updateIsOnline(articleId, true)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), statusCode)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/article", http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetArticlesPage(w http.ResponseWriter, _ *http.Request) {
|
||||
articles := api.Container.ListArticlesUseCase.ListArticles()
|
||||
|
||||
var formattedArticles []map[string]any
|
||||
for _, article := range articles {
|
||||
formattedArticles = append(formattedArticles, map[string]any{
|
||||
"ID": article.ID,
|
||||
"Title": article.Title,
|
||||
"IsOnline": article.IsOnline,
|
||||
"CreatedAt": article.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
"UpdatedAt": article.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
articlesTmpl, _ := api.Container.GetPageUseCase.GetPage("articles", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Articles": formattedArticles,
|
||||
})
|
||||
|
||||
_, _ = w.Write(articlesTmpl)
|
||||
}
|
||||
@@ -140,20 +140,20 @@ func NewPageRouter() http.Handler {
|
||||
|
||||
r.Get("/home", GetHomePage)
|
||||
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/article", GetArticlesPage)
|
||||
|
||||
r.Get("/post/{id}/edit", GetPostEditPage)
|
||||
r.Post("/post/{id}/edit", PostPostEditPage)
|
||||
r.Get("/article/{id}/edit", GetArticleEditPage)
|
||||
r.Post("/article/{id}/edit", PostArticleEditPage)
|
||||
|
||||
r.Get("/post/{id}/delete", GetPostDeletePage)
|
||||
r.Get("/article/{id}/delete", GetArticleDeletePage)
|
||||
|
||||
r.Get("/post/create", GetPostCreatePage)
|
||||
r.Post("/post/create", PostPostCreatePage)
|
||||
r.Get("/article/create", GetArticleCreatePage)
|
||||
r.Post("/article/create", PostArticleCreatePage)
|
||||
|
||||
r.Post("/post/{id}/image/create", image.PostImage)
|
||||
r.Post("/article/{id}/image/create", image.PostImage)
|
||||
|
||||
r.Get("/post/{id}/publish", GetPostPublishPage)
|
||||
r.Get("/post/{id}/unpublish", GetPostUnpublishPage)
|
||||
r.Get("/article/{id}/publish", GetArticlePublishPage)
|
||||
r.Get("/article/{id}/unpublish", GetArticleUnpublishPage)
|
||||
|
||||
r.Get("/integration", GetPageIntegration)
|
||||
})
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/useCases"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostCreatePageError struct {
|
||||
IsError bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func GetPostCreatePageTemplate(postName string, errorMessage string) ([]byte, error) {
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
return api.Container.GetPageUseCase.GetPage("postCreate", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"PageError": PostCreatePageError{
|
||||
IsError: errorMessage != "",
|
||||
Message: errorMessage,
|
||||
},
|
||||
"Name": postName,
|
||||
})
|
||||
}
|
||||
|
||||
func PostPostCreatePage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
postName := r.FormValue("name")
|
||||
pattern := regexp.MustCompile("^[a-zA-Z0-9À-ÖØ-öø-ÿĀ-ſḀ-ỿ ]{3,50}$")
|
||||
|
||||
if !pattern.MatchString(postName) {
|
||||
postsTmpl, _ := GetPostCreatePageTemplate(postName, "Name should be alphanumeric, and between 3 and 50 characters.")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
return
|
||||
}
|
||||
|
||||
post, err := api.Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
Title: postName,
|
||||
Body: "",
|
||||
})
|
||||
if err != nil {
|
||||
postsTmpl, _ := GetPostCreatePageTemplate(postName, "Something went wrong when creating the post, please contact admin.")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/post/"+strconv.Itoa(int(post.ID))+"/edit", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetPostCreatePage(w http.ResponseWriter, _ *http.Request) {
|
||||
postsTmpl, _ := GetPostCreatePageTemplate("", "")
|
||||
_, _ = w.Write(postsTmpl)
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/domain/post"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PostEditPageAlert struct {
|
||||
IsError bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func getPostEditPageTemplate(post post.Post, alert PostEditPageAlert) []byte {
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postTmpl, _ := api.Container.GetPageUseCase.GetPage("postEdit", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Post": post,
|
||||
"Alert": alert,
|
||||
"Secured": os.Getenv("ENVIRONMENT") == "production",
|
||||
})
|
||||
return postTmpl
|
||||
}
|
||||
|
||||
func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postID := chi.URLParam(r, "id")
|
||||
postIDint, err := strconv.Atoi(postID)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate(post.Post{}, PostEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not find the requested post.",
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_ = r.ParseForm()
|
||||
postBody := r.FormValue("postBody")
|
||||
|
||||
getPost, _ := api.Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
updatedPost, err := api.Container.UpdatePostUseCase.UpdateBody(getPost.ID, postBody)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate(post.Post{Body: postBody}, PostEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not save the post: " + err.Error(),
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write(getPostEditPageTemplate(updatedPost, PostEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "Post successfully edited!",
|
||||
}))
|
||||
}
|
||||
|
||||
func GetPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postID := chi.URLParam(r, "id")
|
||||
postIDint, err := strconv.Atoi(postID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
getPost, _ := api.Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
|
||||
_, _ = w.Write(getPostEditPageTemplate(getPost, PostEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "",
|
||||
}))
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
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 = api.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)
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
|
||||
posts := api.Container.ListPostsUseCase.ListPosts()
|
||||
|
||||
var formattedPosts []map[string]any
|
||||
for _, post := range posts {
|
||||
formattedPosts = append(formattedPosts, map[string]any{
|
||||
"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"),
|
||||
})
|
||||
}
|
||||
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postsTmpl, _ := api.Container.GetPageUseCase.GetPage("posts", map[string]any{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Posts": formattedPosts,
|
||||
})
|
||||
|
||||
_, _ = w.Write(postsTmpl)
|
||||
}
|
||||
Reference in New Issue
Block a user