mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add tag description.
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"net/url"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
@@ -65,11 +66,59 @@ func handleDeleteTag(c fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func handleSetTagDescription(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("You must be logged in to set tag description")
|
||||
}
|
||||
id, err := strconv.Atoi(c.Params("id"))
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid tag ID")
|
||||
}
|
||||
description := c.FormValue("description")
|
||||
if description == "" {
|
||||
return model.NewRequestError("Description is required")
|
||||
}
|
||||
description = strings.TrimSpace(description)
|
||||
t, err := service.SetTagDescription(uid, uint(id), description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[model.TagView]{
|
||||
Success: true,
|
||||
Data: *t,
|
||||
Message: "Tag description updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func handleGetTagByName(c fiber.Ctx) error {
|
||||
name := c.Params("name")
|
||||
if name == "" {
|
||||
return model.NewRequestError("Tag name is required")
|
||||
}
|
||||
name, err := url.PathUnescape(name)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid tag name format")
|
||||
}
|
||||
name = strings.TrimSpace(name)
|
||||
t, err := service.GetTagByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[model.TagView]{
|
||||
Success: true,
|
||||
Data: *t,
|
||||
Message: "Tag retrieved successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddTagRoutes(api fiber.Router) {
|
||||
tag := api.Group("/tag")
|
||||
{
|
||||
tag.Post("/", handleCreateTag)
|
||||
tag.Get("/search", handleSearchTag)
|
||||
tag.Delete("/:id", handleDeleteTag)
|
||||
tag.Put("/:id/description", handleSetTagDescription)
|
||||
tag.Get("/:name", handleGetTagByName)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user