From c61affb36efcaed6f19f2643027f43527d821184 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Fri, 23 Feb 2024 23:23:06 +0100 Subject: [PATCH] prevent overposting --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 56fd3af..cc260a8 100644 --- a/main.go +++ b/main.go @@ -49,12 +49,20 @@ func postAlbums(c *gin.Context) { // Call BindJSON to bind the received JSON to // newAlbum. if err := c.BindJSON(&newAlbum); err != nil { + c.Status(http.StatusBadRequest) return } + for _, a := range albums { + if a == newAlbum { + c.Status(http.StatusBadRequest) + return + } + } + // Add the new album to the slice. albums = append(albums, newAlbum) - c.IndentedJSON(http.StatusCreated, newAlbum) + c.JSON(http.StatusCreated, newAlbum) } // getAlbumByID locates the album whose ID value matches the id