in go-gin-react/go-gin-react-part1/main.go [135:159]
func createChannel(c *gin.Context, db *sql.DB) {
// Parse JSON request body into Channel struct
var channel Channel
if err := c.ShouldBindJSON(&channel); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Insert channel into database
result, err := db.Exec("INSERT INTO channels (name) VALUES (?)", channel.Name)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Get ID of newly inserted channel
id, err := result.LastInsertId()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Return ID of newly inserted channel
c.JSON(http.StatusOK, gin.H{"id": id})
}