fix: standardized ID retrieval in routes with IDs

This commit is contained in:
Florian Sylvain
2024-05-20 12:42:12 +02:00
parent 5e9098aa20
commit ed5d52a506
2 changed files with 7 additions and 7 deletions
+2 -3
View File
@@ -7,10 +7,9 @@ import (
)
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
postId := chi.URLParam(r, "id")
id, err := strconv.Atoi(postId)
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, http.StatusText(400), http.StatusBadRequest)
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
return
}
+5 -4
View File
@@ -13,10 +13,12 @@ type PostPost struct {
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, "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
}
@@ -65,10 +67,9 @@ func listPosts(w http.ResponseWriter, _ *http.Request) {
}
func deletePost(w http.ResponseWriter, r *http.Request) {
idParam := chi.URLParam(r, "id")
id, err := strconv.Atoi(idParam)
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
}
err = Container.DeletePostUseCase.DeletePost(uint32(id))