prevent overposting

This commit is contained in:
AustrianToast 2024-02-23 23:23:06 +01:00
parent 1517fdaa09
commit c61affb36e
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4

10
main.go
View File

@ -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