From b366ce7594dc46deaa51b8f6a6b8d547916946bd Mon Sep 17 00:00:00 2001 From: boojack Date: Fri, 10 Feb 2023 23:57:02 +0800 Subject: [PATCH] fix: delete tag (#1062) --- api/tag.go | 2 +- server/tag.go | 23 ++++++++++------------- web/src/helpers/api.ts | 4 +++- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/api/tag.go b/api/tag.go index 50e6b5ed..8202688a 100644 --- a/api/tag.go +++ b/api/tag.go @@ -15,6 +15,6 @@ type TagFind struct { } type TagDelete struct { - Name string + Name string `json:"name"` CreatorID int } diff --git a/server/tag.go b/server/tag.go index 1d139501..d63f6ecd 100644 --- a/server/tag.go +++ b/server/tag.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" "regexp" "sort" @@ -127,29 +126,27 @@ func (s *Server) registerTagRoutes(g *echo.Group) { return nil }) - g.DELETE("/tag/:tagName", func(c echo.Context) error { + g.POST("/tag/delete", func(c echo.Context) error { ctx := c.Request().Context() userID, ok := c.Get(getUserIDContextKey()).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") } - tagName, err := url.QueryUnescape(c.Param("tagName")) - if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid tag name").SetInternal(err) - } else if tagName == "" { - return echo.NewHTTPError(http.StatusBadRequest, "Tag name cannot be empty") + tagDelete := &api.TagDelete{} + if err := json.NewDecoder(c.Request().Body).Decode(tagDelete); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err) + } + if tagDelete.Name == "" { + return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty") } - tagDelete := &api.TagDelete{ - Name: tagName, - CreatorID: userID, - } + tagDelete.CreatorID = userID if err := s.Store.DeleteTag(ctx, tagDelete); err != nil { if common.ErrorCode(err) == common.NotFound { - return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Tag name not found: %s", tagName)) + return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Tag name not found: %s", tagDelete.Name)) } - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagName)).SetInternal(err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err) } return c.JSON(http.StatusOK, true) diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts index f347de17..6c6a8eb8 100644 --- a/web/src/helpers/api.ts +++ b/web/src/helpers/api.ts @@ -202,7 +202,9 @@ export function upsertTag(tagName: string) { } export function deleteTag(tagName: string) { - return axios.delete>(`/api/tag/${encodeURIComponent(tagName)}`); + return axios.post>(`/api/tag/delete`, { + name: tagName, + }); } export async function getRepoStarCount() {