refac: renamed 'post' -> 'article'

This commit is contained in:
Florian Sylvain
2025-11-17 19:27:48 +01:00
parent cb9a431488
commit b470bfcc67
44 changed files with 701 additions and 967 deletions
+99
View File
@@ -0,0 +1,99 @@
package article
import (
"GoCMS/api"
"GoCMS/api/controllers/auth"
"GoCMS/domain/article"
"GoCMS/useCases"
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
type PostArticle struct {
Title string `json:"title" validate:"required,min=3,max=50"`
Body string `json:"body" validate:"required,max=10000"`
}
const IdUint32ErrorMessage = "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32)."
func getArticle(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
return
}
localArticle, err := api.Container.GetArticleUseCase.GetArticle(uint32(id))
if err != nil || !localArticle.IsOnline {
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound)
return
}
articleJson, _ := json.Marshal(localArticle)
_, _ = w.Write(articleJson)
}
func postArticle(w http.ResponseWriter, r *http.Request) {
var localArticle PostArticle
err := json.NewDecoder(r.Body).Decode(&localArticle)
if err != nil {
http.Error(w, auth.BodyErrorMessage, http.StatusBadRequest)
return
}
err = api.Validate.Struct(localArticle)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
createdArticle, err := api.Container.CreateArticleUseCase.CreateArticle(useCases.CreateArticleCommand{
Title: localArticle.Title,
Body: localArticle.Body,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
articleJson, _ := json.Marshal(createdArticle)
_, _ = w.Write(articleJson)
}
func listArticles(w http.ResponseWriter, _ *http.Request) {
articles := api.Container.ListArticlesUseCase.ListArticles()
onlineArticles := make([]article.Article, 0)
for _, localArticle := range articles {
if localArticle.IsOnline {
onlineArticles = append(onlineArticles, localArticle)
}
}
articlesJson, _ := json.Marshal(onlineArticles)
_, _ = w.Write(articlesJson)
}
func deleteArticle(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
}
err = api.Container.DeleteArticleUseCase.DeleteArticle(uint32(id))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
}
_, _ = w.Write([]byte("article deleted"))
}
func NewArticleRouter() http.Handler {
r := chi.NewRouter()
r.Get("/{id}", getArticle)
r.Post("/", postArticle)
r.Get("/", listArticles)
r.Delete("/{id}", deleteArticle)
return r
}
-1
View File
@@ -29,7 +29,6 @@ type LoginCredentials struct {
var shouldCookieBeSecure = os.Getenv("ENVIRONMENT") == "production"
var Token *jwtauth.JWTAuth
// TODO Move into its own file or package that handles api errors
const LogsErrorMessage = "Access to the requested resource is forbidden due to incorrect password and/or username."
const BodyErrorMessage = "The request cannot be processed due to a mismatch in the format of the body."
+1 -1
View File
@@ -27,7 +27,7 @@ func PostImage(w http.ResponseWriter, r *http.Request) {
return
}
err = api.Container.UpdatePostUseCase.AddImage(uint32(idInt), newImage.ID)
err = api.Container.UpdateArticleUseCase.AddImage(uint32(idInt), newImage.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
+58
View File
@@ -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)
}
+75
View File
@@ -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)
}
+31
View File
@@ -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)
}
+9 -9
View File
@@ -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)
})
-58
View File
@@ -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)
}
-75
View File
@@ -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)
}
-31
View File
@@ -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)
}
-99
View File
@@ -1,99 +0,0 @@
package post
import (
"GoCMS/api"
"GoCMS/api/controllers/auth"
"GoCMS/domain/post"
"GoCMS/useCases"
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
type PostPost struct {
Title string `json:"title" validate:"required,min=3,max=50"`
Body string `json:"body" validate:"required,max=10000"`
}
const IdUint32ErrorMessage = "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32)."
func getPost(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
return
}
localPost, err := api.Container.GetPostUseCase.GetPost(uint32(id))
if err != nil || !localPost.IsOnline {
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound)
return
}
postJson, _ := json.Marshal(localPost)
_, _ = w.Write(postJson)
}
func postPost(w http.ResponseWriter, r *http.Request) {
var localPost PostPost
err := json.NewDecoder(r.Body).Decode(&localPost)
if err != nil {
http.Error(w, auth.BodyErrorMessage, http.StatusBadRequest)
return
}
err = api.Validate.Struct(localPost)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
createdPost, err := api.Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
Title: localPost.Title,
Body: localPost.Body,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
postJson, _ := json.Marshal(createdPost)
_, _ = w.Write(postJson)
}
func listPosts(w http.ResponseWriter, _ *http.Request) {
posts := api.Container.ListPostsUseCase.ListPosts()
onlinePosts := make([]post.Post, 0)
for _, localPost := range posts {
if localPost.IsOnline {
onlinePosts = append(onlinePosts, localPost)
}
}
postsJson, _ := json.Marshal(onlinePosts)
_, _ = w.Write(postsJson)
}
func deletePost(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
}
err = api.Container.DeletePostUseCase.DeletePost(uint32(id))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
}
_, _ = w.Write([]byte("post deleted"))
}
func NewPostRouter() http.Handler {
r := chi.NewRouter()
r.Get("/{id}", getPost)
r.Post("/", postPost)
r.Get("/", listPosts)
r.Delete("/{id}", deletePost)
return r
}