Added functionality to delete unused categories from a DB.
This commit is contained in:
parent
0fbc121e38
commit
4e82fbddc1
@ -105,6 +105,7 @@ func main() {
|
|||||||
mindflowApi := http.NewMindflowApiHandlers(mindflowDB)
|
mindflowApi := http.NewMindflowApiHandlers(mindflowDB)
|
||||||
|
|
||||||
srv.POST("/api/mindflow", mindflowApi.New)
|
srv.POST("/api/mindflow", mindflowApi.New)
|
||||||
|
srv.DELETE("/api/mindflow/category", mindflowApi.DeleteUnusedCategories)
|
||||||
srv.PATCH("/api/mindflow/:id", mindflowApi.Edit)
|
srv.PATCH("/api/mindflow/:id", mindflowApi.Edit)
|
||||||
srv.DELETE("/api/mindflow/:id", mindflowApi.Delete)
|
srv.DELETE("/api/mindflow/:id", mindflowApi.Delete)
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package http
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -252,6 +253,14 @@ func (h *MindflowApiHandlers) New(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently)
|
http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *MindflowApiHandlers) DeleteUnusedCategories(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := h.db.DeleteUnusedCategories(); err != nil {
|
||||||
|
log.Println("Cannot delete unused categories:", err)
|
||||||
|
http.Error(w, "Cannot delete unused categories: "+err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *MindflowApiHandlers) Edit(w http.ResponseWriter, r *http.Request) {
|
func (h *MindflowApiHandlers) Edit(w http.ResponseWriter, r *http.Request) {
|
||||||
var post *mindflow.Post
|
var post *mindflow.Post
|
||||||
var err error
|
var err error
|
||||||
|
@ -22,6 +22,8 @@ var (
|
|||||||
queryCategoryNew string
|
queryCategoryNew string
|
||||||
//go:embed queries/categoryDelete.sql
|
//go:embed queries/categoryDelete.sql
|
||||||
queryCategoryDelete string
|
queryCategoryDelete string
|
||||||
|
//go:embed queries/categoryDeleteUnused.sql
|
||||||
|
queryCategoryDeleteUnused string
|
||||||
//go:embed queries/postGetAll.sql
|
//go:embed queries/postGetAll.sql
|
||||||
queryPostGetAll string
|
queryPostGetAll string
|
||||||
//go:embed queries/postNew.sql
|
//go:embed queries/postNew.sql
|
||||||
@ -33,14 +35,15 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
stmtCategoryById *sql.Stmt
|
stmtCategoryById *sql.Stmt
|
||||||
stmtCategoryGetAll *sql.Stmt
|
stmtCategoryGetAll *sql.Stmt
|
||||||
stmtCategoryNew *sql.Stmt
|
stmtCategoryNew *sql.Stmt
|
||||||
stmtCategoryDelete *sql.Stmt
|
stmtCategoryDelete *sql.Stmt
|
||||||
stmtPostGetAll *sql.Stmt
|
stmtCategoryDeleteUnused *sql.Stmt
|
||||||
stmtPostNew *sql.Stmt
|
stmtPostGetAll *sql.Stmt
|
||||||
stmtPostEdit *sql.Stmt
|
stmtPostNew *sql.Stmt
|
||||||
stmtPostDelete *sql.Stmt
|
stmtPostEdit *sql.Stmt
|
||||||
|
stmtPostDelete *sql.Stmt
|
||||||
)
|
)
|
||||||
|
|
||||||
func initDBStatements(db *sql.DB) error {
|
func initDBStatements(db *sql.DB) error {
|
||||||
@ -71,6 +74,11 @@ func initDBStatements(db *sql.DB) error {
|
|||||||
return errors.Wrap(err, "failed to prepare queryCategoryDelete")
|
return errors.Wrap(err, "failed to prepare queryCategoryDelete")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stmtCategoryDeleteUnused, err = db.Prepare(queryCategoryDeleteUnused)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to prepare queryCategoryDeleteUnused")
|
||||||
|
}
|
||||||
|
|
||||||
stmtPostGetAll, err = db.Prepare(queryPostGetAll)
|
stmtPostGetAll, err = db.Prepare(queryPostGetAll)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to prepare queryPostGetAll")
|
return errors.Wrap(err, "failed to prepare queryPostGetAll")
|
||||||
@ -288,6 +296,22 @@ func (s *SQLiteMindflow) DeleteCategory(id int64) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLiteMindflow) DeleteUnusedCategories() (err error) {
|
||||||
|
tx, err := s.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
if _, err = tx.Stmt(stmtCategoryDeleteUnused).Exec(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SQLiteMindflow) Close() error {
|
func (s *SQLiteMindflow) Close() error {
|
||||||
stmtCategoryById.Close()
|
stmtCategoryById.Close()
|
||||||
stmtCategoryGetAll.Close()
|
stmtCategoryGetAll.Close()
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
DELETE FROM `category`
|
||||||
|
WHERE `category`.`category_id` NOT IN (
|
||||||
|
SELECT `category_id`
|
||||||
|
FROM `post`
|
||||||
|
GROUP BY `post`.`category_id`
|
||||||
|
HAVING COUNT(`post`.`post_id`) > 0 );
|
@ -43,5 +43,6 @@ type Mindflow interface {
|
|||||||
Categories() ([]Category, error)
|
Categories() ([]Category, error)
|
||||||
NewCategory(name string) (int64, error)
|
NewCategory(name string) (int64, error)
|
||||||
GetCategoryByID(id int64) (string, error)
|
GetCategoryByID(id int64) (string, error)
|
||||||
|
DeleteUnusedCategories() error
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user