mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
fix: replaced post name with its id in URLs
This commit is contained in:
@@ -39,7 +39,7 @@
|
||||
<td class="date">{{ $post.CreatedAt }}</td>
|
||||
<td class="date">{{ $post.UpdatedAt }}</td>
|
||||
<td>
|
||||
<a href="/post/{{ $post.Title }}/edit" class="btn btn-secondary btn-sm">
|
||||
<a href="/post/{{ $post.ID }}/edit" class="btn btn-secondary btn-sm">
|
||||
<svg width="12px" height="12px" fill="currentColor">
|
||||
<use xlink:href="/static/bootstrap-icons.svg#pen"/>
|
||||
</svg>
|
||||
|
||||
+2
-2
@@ -123,9 +123,9 @@ func NewPageRouter() http.Handler {
|
||||
r.Use(IsVerifiedMiddleware)
|
||||
r.Get("/home", GetHomePage)
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/post/{name}/edit", GetPostEditPage)
|
||||
r.Get("/post/{id}/edit", GetPostEditPage)
|
||||
r.Post("/post/{id}/edit", PostPostEditPage)
|
||||
r.Get("/post/{id}/delete", GetPostDeletePage)
|
||||
r.Post("/post/{name}/edit", PostPostEditPage)
|
||||
r.Get("/post/create", GetPostCreatePage)
|
||||
r.Post("/post/create", PostPostCreatePage)
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostCreatePageError struct {
|
||||
@@ -47,7 +48,7 @@ func PostPostCreatePage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/post/"+post.Title+"/edit", http.StatusSeeOther)
|
||||
http.Redirect(w, r, "/post/"+strconv.Itoa(int(post.ID))+"/edit", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetPostCreatePage(w http.ResponseWriter, _ *http.Request) {
|
||||
|
||||
+12
-10
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostEditPageAlert struct {
|
||||
@@ -24,8 +24,9 @@ func getPostEditPageTemplate(body string, alert PostEditPageAlert) []byte {
|
||||
}
|
||||
|
||||
func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postName := chi.URLParam(r, "name")
|
||||
if len(postName) == 0 {
|
||||
postID := chi.URLParam(r, "id")
|
||||
postIDint, err := strconv.Atoi(postID)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate("", PostEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not find the requested post.",
|
||||
@@ -36,9 +37,8 @@ func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
postBody := r.FormValue("postBody")
|
||||
|
||||
parsedName, _ := url.PathUnescape(postName)
|
||||
post, _ := Container.GetPostUseCase.GetPostByName(parsedName)
|
||||
err := Container.UpdatePostUseCase.UpdateBody(post.ID, postBody)
|
||||
post, _ := Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
err = Container.UpdatePostUseCase.UpdateBody(post.ID, postBody)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate(postBody, PostEditPageAlert{
|
||||
IsError: true,
|
||||
@@ -54,13 +54,15 @@ func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func GetPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postName := chi.URLParam(r, "name")
|
||||
if len(postName) == 0 {
|
||||
postID := chi.URLParam(r, "id")
|
||||
postIDint, err := strconv.Atoi(postID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
parsedName, _ := url.PathUnescape(postName)
|
||||
post, _ := Container.GetPostUseCase.GetPostByName(parsedName)
|
||||
|
||||
post, _ := Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
|
||||
_, _ = w.Write(getPostEditPageTemplate(post.Body, PostEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "",
|
||||
|
||||
Reference in New Issue
Block a user