1
0

Added functionality to delete unused categories from a DB.

This commit is contained in:
Alexander Andreev 2023-05-22 23:05:52 +04:00
parent 0fbc121e38
commit 4e82fbddc1
Signed by: Arav
GPG Key ID: D22A817D95815393
5 changed files with 49 additions and 8 deletions

View File

@ -105,6 +105,7 @@ func main() {
mindflowApi := http.NewMindflowApiHandlers(mindflowDB)
srv.POST("/api/mindflow", mindflowApi.New)
srv.DELETE("/api/mindflow/category", mindflowApi.DeleteUnusedCategories)
srv.PATCH("/api/mindflow/:id", mindflowApi.Edit)
srv.DELETE("/api/mindflow/:id", mindflowApi.Delete)

View File

@ -3,6 +3,7 @@ package http
import (
"fmt"
"image/jpeg"
"log"
"net/http"
"strconv"
"strings"
@ -252,6 +253,14 @@ func (h *MindflowApiHandlers) New(w http.ResponseWriter, r *http.Request) {
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) {
var post *mindflow.Post
var err error

View File

@ -22,6 +22,8 @@ var (
queryCategoryNew string
//go:embed queries/categoryDelete.sql
queryCategoryDelete string
//go:embed queries/categoryDeleteUnused.sql
queryCategoryDeleteUnused string
//go:embed queries/postGetAll.sql
queryPostGetAll string
//go:embed queries/postNew.sql
@ -37,6 +39,7 @@ var (
stmtCategoryGetAll *sql.Stmt
stmtCategoryNew *sql.Stmt
stmtCategoryDelete *sql.Stmt
stmtCategoryDeleteUnused *sql.Stmt
stmtPostGetAll *sql.Stmt
stmtPostNew *sql.Stmt
stmtPostEdit *sql.Stmt
@ -71,6 +74,11 @@ func initDBStatements(db *sql.DB) error {
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)
if err != nil {
return errors.Wrap(err, "failed to prepare queryPostGetAll")
@ -288,6 +296,22 @@ func (s *SQLiteMindflow) DeleteCategory(id int64) (err error) {
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 {
stmtCategoryById.Close()
stmtCategoryGetAll.Close()

View File

@ -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 );

View File

@ -43,5 +43,6 @@ type Mindflow interface {
Categories() ([]Category, error)
NewCategory(name string) (int64, error)
GetCategoryByID(id int64) (string, error)
DeleteUnusedCategories() error
Close() error
}