Created
February 9, 2021 06:19
-
-
Save vedashree29296/f960d195c29fb969f076ce7094748a5a to your computer and use it in GitHub Desktop.
Create a new category and return an id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CreateCategory : create a new Category in DB | |
func (c *Category) CreateCategory() (int, error) { | |
//Create and execute the query | |
row := client.DbClient.QueryRow("INSERT INTO category (category_name) values ($1) RETURNING id;", c.CategoryName) | |
// save the data in an object | |
switch err := row.Scan(&c.ID); err { | |
// success, no errors | |
case nil: | |
return c.ID, nil | |
// In case of no output rows | |
case sql.ErrNoRows: | |
return c.ID, errors.New("Insert Failed") | |
// other errors occured | |
default: | |
return c.ID, err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment