feat: post list and edition redirect

This commit is contained in:
Florian Sylvain
2024-05-17 16:38:41 +02:00
parent 24eefb743b
commit 900ad3e8b8
2062 changed files with 7238 additions and 24 deletions
+37 -10
View File
@@ -1,29 +1,56 @@
package api
import (
"GoCMS/useCases"
"html/template"
"net/http"
"regexp"
)
type PostCreatePageError struct {
IsError bool
Message string
}
func GetPostCreatePageTemplate(postName string, errorMessage string) ([]byte, error) {
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
return Container.GetPageUseCase.GetPage("postCreate", map[string]interface{}{
"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}$")
isPostNameValid := pattern.MatchString(postName)
if !isPostNameValid {
r.Method = http.MethodGet
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 := 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/"+post.Title+"/edit", http.StatusSeeOther)
}
func GetPostCreatePage(w http.ResponseWriter, _ *http.Request) {
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
postsTmpl, _ := Container.GetPageUseCase.GetPage("postCreate", map[string]interface{}{
"Navbar": template.HTML(navbarTmpl),
"Head": headTmpl,
})
postsTmpl, _ := GetPostCreatePageTemplate("", "")
_, _ = w.Write(postsTmpl)
}