add tag delete

pull/52/head
sunface 4 years ago
parent f5c1e1a8b2
commit 1810263a25

@ -60,6 +60,7 @@ function PostEditPage() {
return (
<PageContainer
nav={<Nav
tagID={tag.id}
changeEditMode={(v) => 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 (
<>
<Flex w="100%" h="100%" align="center" justify="space-between" px={{ base: "4", md: "6" }}>
@ -167,6 +171,7 @@ function HeaderContent(props: any) {
icon={<SwitchIcon />}
/>
<Button layerStyle="colorButton" ml="2" onClick={props.publish}></Button>
<Button colorScheme="red" ml="2" onClick={onDelete}></Button>
</Box>
</Flex>
</>

@ -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
}

@ -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
}

@ -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
}

@ -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<typeof chakra.div> & {
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 (
<Flex justifyContent="space-between" alignItems="center" className="hover-bg" p="2">
<NextLink href={`${ReserveUrls.Tags}/${tag.name}`}>

Loading…
Cancel
Save