mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
fix: standardized ID retrieval in routes with IDs
This commit is contained in:
@@ -7,10 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||||
postId := chi.URLParam(r, "id")
|
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||||
id, err := strconv.Atoi(postId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -13,10 +13,12 @@ type PostPost struct {
|
|||||||
Body string `json:"body" validate:"required,max=10000"`
|
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) {
|
func getPost(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32).", http.StatusBadRequest)
|
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,10 +67,9 @@ func listPosts(w http.ResponseWriter, _ *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deletePost(w http.ResponseWriter, r *http.Request) {
|
func deletePost(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||||
id, err := strconv.Atoi(idParam)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||||
|
|||||||
Reference in New Issue
Block a user