mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Initial commit
This commit is contained in:
68
server/api/tag.go
Normal file
68
server/api/tag.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func handleCreateTag(c fiber.Ctx) error {
|
||||
tag := c.FormValue("name")
|
||||
if tag == "" {
|
||||
return model.NewRequestError("name is required")
|
||||
}
|
||||
t, err := service.CreateTag(tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[model.TagView]{
|
||||
Success: true,
|
||||
Data: *t,
|
||||
Message: "Tag created successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func handleSearchTag(c fiber.Ctx) error {
|
||||
keyword := c.Query("keyword")
|
||||
if keyword == "" {
|
||||
return model.NewRequestError("Keyword is required")
|
||||
}
|
||||
tags, err := service.SearchTag(keyword)
|
||||
if tags == nil {
|
||||
tags = []model.TagView{}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[*[]model.TagView]{
|
||||
Success: true,
|
||||
Data: &tags,
|
||||
Message: "Tags retrieved successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func handleDeleteTag(c fiber.Ctx) error {
|
||||
id, err := strconv.Atoi(c.Params("id"))
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid tag ID")
|
||||
}
|
||||
err = service.DeleteTag(uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[any]{
|
||||
Success: true,
|
||||
Data: nil,
|
||||
Message: "Tag deleted successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddTagRoutes(api fiber.Router) {
|
||||
tag := api.Group("/tag")
|
||||
{
|
||||
tag.Post("/", handleCreateTag)
|
||||
tag.Get("/search", handleSearchTag)
|
||||
tag.Delete("/:id", handleDeleteTag)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user