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