Created
February 9, 2021 06:18
-
-
Save vedashree29296/2d4ad9a83d1c51503907138fc99510de to your computer and use it in GitHub Desktop.
Get all categories
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
//GetAllCategory : Get all Categories from DB | |
func (c Category) GetAllCategory() ([]*models.Category, error) { | |
var categories []*models.Category // this will hold final result | |
//execute the query | |
rows, err := client.DbClient.Query("SELECT * from category") | |
defer rows.Close() | |
// iterate through rows | |
for rows.Next() { | |
// Bind to model | |
err = rows.Scan(&c.ID, &c.CategoryName) | |
// type cast to models.Category | |
cm := models.Category(c) | |
//add to result | |
categories = append(categories, &cm) | |
if err != nil { | |
return nil, err | |
} | |
} | |
// handle any errors that are encountered during iteration | |
err = rows.Err() | |
if err != nil { | |
return nil, err | |
} | |
return categories, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment