Implemented getting all categories.
This commit is contained in:
parent
38e5d9e4bf
commit
5095c7649d
@ -16,6 +16,8 @@ var (
|
|||||||
|
|
||||||
//go:embed queries/categoryById.sql
|
//go:embed queries/categoryById.sql
|
||||||
queryCategoryById string
|
queryCategoryById string
|
||||||
|
//go:embed queries/categoryGetAll.sql
|
||||||
|
queryCategoryGetAll string
|
||||||
//go:embed queries/categoryNew.sql
|
//go:embed queries/categoryNew.sql
|
||||||
queryCategoryNew string
|
queryCategoryNew string
|
||||||
//go:embed queries/postGetAll.sql
|
//go:embed queries/postGetAll.sql
|
||||||
@ -30,6 +32,7 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
stmtCategoryById *sql.Stmt
|
stmtCategoryById *sql.Stmt
|
||||||
|
stmtCategoryGetAll *sql.Stmt
|
||||||
stmtCategoryNew *sql.Stmt
|
stmtCategoryNew *sql.Stmt
|
||||||
stmtPostGetAll *sql.Stmt
|
stmtPostGetAll *sql.Stmt
|
||||||
stmtPostNew *sql.Stmt
|
stmtPostNew *sql.Stmt
|
||||||
@ -50,6 +53,11 @@ func initDBStatements(db *sql.DB) error {
|
|||||||
return errors.Wrap(err, "failed to prepare queryCategoryById")
|
return errors.Wrap(err, "failed to prepare queryCategoryById")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stmtCategoryGetAll, err = db.Prepare(queryCategoryGetAll)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to prepare queryCategoryGetAll")
|
||||||
|
}
|
||||||
|
|
||||||
stmtCategoryNew, err = db.Prepare(queryCategoryNew)
|
stmtCategoryNew, err = db.Prepare(queryCategoryNew)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to prepare queryCategoryNew")
|
return errors.Wrap(err, "failed to prepare queryCategoryNew")
|
||||||
@ -185,6 +193,34 @@ func (s *SQLiteMindflow) Posts() (posts []mindflow.Post, err error) {
|
|||||||
return posts, nil
|
return posts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLiteMindflow) Categories() (categories []mindflow.Category, err error) {
|
||||||
|
tx, err := s.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
rows, err := tx.Stmt(stmtCategoryGetAll).Query()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var category mindflow.Category
|
||||||
|
|
||||||
|
if err = rows.Scan(&category.ID, &category.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
categories = append(categories, category)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return categories, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SQLiteMindflow) NewCategory(name string) (int64, error) {
|
func (s *SQLiteMindflow) NewCategory(name string) (int64, error) {
|
||||||
tx, err := s.db.Begin()
|
tx, err := s.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
4
pkg/mindflow/database/sqlite/queries/categoryGetAll.sql
Normal file
4
pkg/mindflow/database/sqlite/queries/categoryGetAll.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
SELECT
|
||||||
|
`category_id`,
|
||||||
|
`name`
|
||||||
|
FROM `category`;
|
@ -5,6 +5,7 @@ type Mindflow interface {
|
|||||||
Edit(post *Post) error
|
Edit(post *Post) error
|
||||||
Delete(id int64) error
|
Delete(id int64) error
|
||||||
Posts() ([]Post, error)
|
Posts() ([]Post, error)
|
||||||
|
Categories() ([]Category, error)
|
||||||
NewCategory(name string) (int64, error)
|
NewCategory(name string) (int64, error)
|
||||||
GetCategoryByID(id int64) (string, error)
|
GetCategoryByID(id int64) (string, error)
|
||||||
Close() error
|
Close() error
|
||||||
|
Loading…
Reference in New Issue
Block a user