From 0e28f8a16b16eac07c5a6fdcf1e828cf7dab02a9 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 23 May 2023 00:22:12 +0400 Subject: [PATCH] Impl. category editing. --- pkg/mindflow/database/sqlite/db.go | 26 +++++++++++++++++++ .../database/sqlite/queries/categoryEdit.sql | 4 +++ pkg/mindflow/mindflow.go | 1 + 3 files changed, 31 insertions(+) create mode 100644 pkg/mindflow/database/sqlite/queries/categoryEdit.sql diff --git a/pkg/mindflow/database/sqlite/db.go b/pkg/mindflow/database/sqlite/db.go index 2398b6b..7388123 100644 --- a/pkg/mindflow/database/sqlite/db.go +++ b/pkg/mindflow/database/sqlite/db.go @@ -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() diff --git a/pkg/mindflow/database/sqlite/queries/categoryEdit.sql b/pkg/mindflow/database/sqlite/queries/categoryEdit.sql new file mode 100644 index 0000000..60def28 --- /dev/null +++ b/pkg/mindflow/database/sqlite/queries/categoryEdit.sql @@ -0,0 +1,4 @@ +UPDATE OR REPLACE `category` + SET + `name` = ? + WHERE `category_id` = ?; \ No newline at end of file diff --git a/pkg/mindflow/mindflow.go b/pkg/mindflow/mindflow.go index 0f2ab03..1aed47a 100644 --- a/pkg/mindflow/mindflow.go +++ b/pkg/mindflow/mindflow.go @@ -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