diff --git a/pages/admin/tag/[id].tsx b/pages/admin/tag/[id].tsx index f964233b..0a599970 100644 --- a/pages/admin/tag/[id].tsx +++ b/pages/admin/tag/[id].tsx @@ -60,6 +60,7 @@ function PostEditPage() { return ( setEditMode(v)} publish={() => publish()} />} @@ -125,7 +126,10 @@ function HeaderContent(props: any) { }, }) const group = getRootProps() - + const onDelete = async () => { + await requestApi.delete(`/tag/${props.tagID}`) + } + return ( <> @@ -167,6 +171,7 @@ function HeaderContent(props: any) { icon={} /> + diff --git a/server/internal/api/tag.go b/server/internal/api/tag.go index 08da109a..718a31e5 100644 --- a/server/internal/api/tag.go +++ b/server/internal/api/tag.go @@ -3,7 +3,6 @@ package api import ( "net/http" "sort" - "strconv" "github.com/gin-gonic/gin" "github.com/imdotdev/im.dev/server/internal/interaction" @@ -88,8 +87,8 @@ func SubmitTag(c *gin.Context) { } func DeleteTag(c *gin.Context) { - id, _ := strconv.ParseInt(c.Param("id"), 10, 64) - if id == 0 { + id := c.Param("id") + if id == "" { c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid)) return } diff --git a/server/internal/tags/tags.go b/server/internal/tags/tags.go index bedf5613..7250b0fd 100644 --- a/server/internal/tags/tags.go +++ b/server/internal/tags/tags.go @@ -9,6 +9,7 @@ import ( "github.com/asaskevich/govalidator" "github.com/imdotdev/im.dev/server/internal/interaction" + "github.com/imdotdev/im.dev/server/internal/top" "github.com/imdotdev/im.dev/server/pkg/db" "github.com/imdotdev/im.dev/server/pkg/e" "github.com/imdotdev/im.dev/server/pkg/log" @@ -82,13 +83,57 @@ func GetTagsByIDs(ids []string) ([]*models.Tag, *e.Error) { return tags, nil } -func DeleteTag(id int64) *e.Error { - _, err := db.Conn.Exec("DELETE FROM tags WHERE id=?", id) +func DeleteTag(id string) *e.Error { + tag, err0 := models.GetSimpleTag(id, "") + if err0 != nil { + return err0 + } + + tx, err := db.Conn.Begin() + if err != nil { + logger.Warn("start transaction error", "error", err) + return e.New(http.StatusInternalServerError, e.Internal) + } + + // 删除tags表的数据 + _, err = tx.Exec("DELETE FROM tags WHERE id=?", id) + if err != nil { + logger.Warn("delete tag error", "error", err) + return e.New(http.StatusInternalServerError, e.Internal) + } + + // 删除关联的tag数据 + _, err = tx.Exec("DELETE FROM tags_using WHERE tag_id=?", id) + if err != nil { + logger.Warn("delete tag error", "error", err) + tx.Rollback() + return e.New(http.StatusInternalServerError, e.Internal) + } + + // 删除home sidebar的引用 + _, err = tx.Exec("DELETE FROM home_sidebar WHERE tag_name=?", tag.Name) if err != nil { - logger.Warn("delete post error", "error", err) + logger.Warn("delete tag error", "error", err) + tx.Rollback() return e.New(http.StatusInternalServerError, e.Internal) } + // 删除follows + _, err = tx.Exec("DELETE FROM follows WHERE target_id=?", id) + if err != nil { + logger.Warn("delete tag error", "error", err) + tx.Rollback() + return e.New(http.StatusInternalServerError, e.Internal) + } + + tx.Commit() + + // 删除top里的tag列表 + err = top.RemoveTag(id) + if err != nil { + logger.Warn("delete top error", "error", err) + } + return nil } diff --git a/server/internal/top/top.go b/server/internal/top/top.go index bd205ca8..0a0a68f4 100644 --- a/server/internal/top/top.go +++ b/server/internal/top/top.go @@ -217,3 +217,23 @@ func Init() { time.Sleep(24 * time.Hour) } } + +func RemoveTag(tagID string) error { + hots := make([]string, 0) + + hots = append(hots, fmt.Sprintf(TagFormat, tagID, TopYear)) + hots = append(hots, fmt.Sprintf(TagFormat, tagID, TopMonth)) + hots = append(hots, fmt.Sprintf(TagFormat, tagID, TopWeek)) + hots = append(hots, fmt.Sprintf(TagFormat, tagID, TopRecent)) + + ctx := context.Background() + for _, hot := range hots { + err := db.Redis.Del(ctx, hot).Err() + if err != nil { + logger.Warn("delete top error", "error", err, "key", hot, "tag_id", tagID) + return err + } + } + + return nil +} diff --git a/src/components/tags/tag-card.tsx b/src/components/tags/tag-card.tsx index 1bc2dd3e..c91a270e 100644 --- a/src/components/tags/tag-card.tsx +++ b/src/components/tags/tag-card.tsx @@ -5,18 +5,18 @@ import { ReserveUrls } from "src/data/reserve-urls" import NextLink from "next/link" import userCustomTheme from "theme/user-custom" import Count from "components/count" +import { requestApi } from "utils/axios/request" type Props = PropsOf & { tag: Tag showActions?: boolean onEdit?: any - onDelete?: any unit?: string } export const TagCard= (props:Props) =>{ - const {tag,showActions=false,onEdit,onDelete,unit="posts"} = props + const {tag,showActions=false,onEdit,unit="posts"} = props return (