1
0

Impl. category editing.

This commit is contained in:
Alexander Andreev 2023-05-23 00:22:12 +04:00
parent aeb481b2ba
commit 0e28f8a16b
Signed by: Arav
GPG Key ID: D22A817D95815393
3 changed files with 31 additions and 0 deletions

View File

@ -20,6 +20,8 @@ var (
queryCategoryGetAll string
//go:embed queries/categoryNew.sql
queryCategoryNew string
//go:embed queries/categoryEdit.sql
queryCategoryEdit string
//go:embed queries/categoryDelete.sql
queryCategoryDelete string
//go:embed queries/categoryDeleteUnused.sql
@ -38,6 +40,7 @@ var (
stmtCategoryById *sql.Stmt
stmtCategoryGetAll *sql.Stmt
stmtCategoryNew *sql.Stmt
stmtCategoryEdit *sql.Stmt
stmtCategoryDelete *sql.Stmt
stmtCategoryDeleteUnused *sql.Stmt
stmtPostGetAll *sql.Stmt
@ -69,6 +72,11 @@ func initDBStatements(db *sql.DB) error {
return errors.Wrap(err, "failed to prepare queryCategoryNew")
}
stmtCategoryEdit, err = db.Prepare(queryCategoryEdit)
if err != nil {
return errors.Wrap(err, "failed to prepare queryCategoryEdit")
}
stmtCategoryDelete, err = db.Prepare(queryCategoryDelete)
if err != nil {
return errors.Wrap(err, "failed to prepare queryCategoryDelete")
@ -259,6 +267,23 @@ func (s *SQLiteMindflow) NewCategory(name string) (int64, error) {
return id, nil
}
func (s *SQLiteMindflow) EditCategory(category *mindflow.Category) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.Stmt(stmtPostEdit).Exec(category.Name, category.ID)
if err != nil {
return err
}
tx.Commit()
return nil
}
func (s *SQLiteMindflow) GetCategoryByID(id int64) (name string, err error) {
tx, err := s.db.Begin()
if err != nil {
@ -316,6 +341,7 @@ func (s *SQLiteMindflow) Close() error {
stmtCategoryById.Close()
stmtCategoryGetAll.Close()
stmtCategoryNew.Close()
stmtCategoryEdit.Close()
stmtCategoryDelete.Close()
stmtPostDelete.Close()
stmtPostEdit.Close()

View File

@ -0,0 +1,4 @@
UPDATE OR REPLACE `category`
SET
`name` = ?
WHERE `category_id` = ?;

View File

@ -43,6 +43,7 @@ type Mindflow interface {
Categories() ([]Category, error)
NewCategory(name string) (int64, error)
GetCategoryByID(id int64) (string, error)
EditCategory(category *Category) error
DeleteCategory(id int64) error
DeleteUnusedCategories() error
Close() error