December 2018
Intermediate to advanced
500 pages
12h 19m
English
We are using the standard of having the PUT method perform an update for an object (well, some people may contest this). The updateHotel() handler is defined for the exact same path as GET for read, but for the PUT method. The code for the handler is as follows:
func updateHotel(c *gin.Context) {
// get hotel object from repository
hotelId:= c.Param("id")
hotel, found:= repository[hotelId]
if !found {
c.JSON(http.StatusNotFound, gin.H{"status": "hotel with id not found"})
} else {
//update
if err:= c.ShouldBindJSON(&hotel); err == nil {
repository[hotel.Id] = hotel
//return OK
c.JSON(http.StatusOK, gin.H{"status": "ok"})
} else {
// some params not correct
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
}
If a hotel ...
Read now
Unlock full access