mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add tags page.
This commit is contained in:
@@ -122,6 +122,21 @@ func handleGetTagByName(c fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func getAllTags(c fiber.Ctx) error {
|
||||
tags, err := service.GetTagList()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tags == nil {
|
||||
tags = []model.TagViewWithCount{}
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[*[]model.TagViewWithCount]{
|
||||
Success: true,
|
||||
Data: &tags,
|
||||
Message: "All tags retrieved successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddTagRoutes(api fiber.Router) {
|
||||
tag := api.Group("/tag")
|
||||
{
|
||||
@@ -130,5 +145,6 @@ func AddTagRoutes(api fiber.Router) {
|
||||
tag.Delete("/:id", handleDeleteTag)
|
||||
tag.Put("/:id/info", handleSetTagInfo)
|
||||
tag.Get("/:name", handleGetTagByName)
|
||||
tag.Get("/", getAllTags)
|
||||
}
|
||||
}
|
||||
|
@@ -268,6 +268,36 @@ func GetResourceByTag(tagID uint, page int, pageSize int) ([]model.Resource, int
|
||||
return resources, int(totalPages), nil
|
||||
}
|
||||
|
||||
// CountResourcesByTag counts the number of resources associated with a specific tag.
|
||||
func CountResourcesByTag(tagID uint) (int64, error) {
|
||||
tag, err := GetTagByID(tagID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if tag.AliasOf != nil {
|
||||
tag, err = GetTagByID(*tag.AliasOf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
var tagIds []uint
|
||||
tagIds = append(tagIds, tag.ID)
|
||||
for _, alias := range tag.Aliases {
|
||||
tagIds = append(tagIds, alias.ID)
|
||||
}
|
||||
var count int64
|
||||
subQuery := db.Table("resource_tags").
|
||||
Select("resource_id").
|
||||
Where("tag_id IN ?", tagIds).
|
||||
Group("resource_id")
|
||||
if err := db.Model(&model.Resource{}).
|
||||
Where("id IN (?)", subQuery).
|
||||
Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func ExistsResource(id uint) (bool, error) {
|
||||
var r model.Resource
|
||||
if err := db.Model(&model.Resource{}).Where("id = ?", id).First(&r).Error; err != nil {
|
||||
|
@@ -82,3 +82,13 @@ func SetTagInfo(id uint, description string, aliasOf *uint, tagType string) erro
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListTags retrieves all tags from the database.
|
||||
// Only returns the ID, name, and type of each tag.
|
||||
func ListTags() ([]model.Tag, error) {
|
||||
var tags []model.Tag
|
||||
if err := db.Select("id", "name", "type").Where("alias_of is null").Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
|
@@ -33,3 +33,15 @@ func (t *Tag) ToView() *TagView {
|
||||
Aliases: aliases,
|
||||
}
|
||||
}
|
||||
|
||||
type TagViewWithCount struct {
|
||||
TagView
|
||||
ResourceCount int `json:"resources_count"` // Count of resources associated with the tag
|
||||
}
|
||||
|
||||
func (t *TagView) WithCount(count int) *TagViewWithCount {
|
||||
return &TagViewWithCount{
|
||||
TagView: *t,
|
||||
ResourceCount: count,
|
||||
}
|
||||
}
|
||||
|
@@ -56,6 +56,10 @@ func CreateResource(uid uint, params *ResourceCreateParams) (uint, error) {
|
||||
if r, err = dao.CreateResource(r); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
err = updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return r.ID, nil
|
||||
}
|
||||
|
||||
@@ -169,6 +173,10 @@ func DeleteResource(uid, id uint) error {
|
||||
if err := dao.DeleteResource(id); err != nil {
|
||||
return err
|
||||
}
|
||||
err = updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -241,5 +249,9 @@ func EditResource(uid, rid uint, params *ResourceCreateParams) error {
|
||||
log.Error("UpdateResource error: ", err)
|
||||
return model.NewInternalServerError("Failed to update resource")
|
||||
}
|
||||
err = updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -19,6 +19,10 @@ func CreateTag(uid uint, name string) (*model.TagView, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return t.ToView(), nil
|
||||
}
|
||||
|
||||
@@ -63,6 +67,10 @@ func SearchTag(name string, mainTag bool) ([]model.TagView, error) {
|
||||
}
|
||||
|
||||
func DeleteTag(id uint) error {
|
||||
err := updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return dao.DeleteTag(id)
|
||||
}
|
||||
|
||||
@@ -88,5 +96,36 @@ func SetTagInfo(uid uint, id uint, description string, aliasOf *uint, tagType st
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
err = updateCachedTagList()
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
return t.ToView(), nil
|
||||
}
|
||||
|
||||
var cachedTagList []model.TagViewWithCount
|
||||
|
||||
func updateCachedTagList() error {
|
||||
tags, err := dao.ListTags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cachedTagList = make([]model.TagViewWithCount, 0, len(tags))
|
||||
for _, tag := range tags {
|
||||
count, err := dao.CountResourcesByTag(tag.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cachedTagList = append(cachedTagList, *tag.ToView().WithCount(int(count)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetTagList() ([]model.TagViewWithCount, error) {
|
||||
if cachedTagList == nil {
|
||||
if err := updateCachedTagList(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return cachedTagList, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user