From 57bfc38eb80631b84189dfb76d24d214ad8def89 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Mon, 27 Jan 2025 19:04:26 +0800 Subject: [PATCH] feat: Application details have been enhanced with tags. (#7777) Refs https://github.com/1Panel-dev/1Panel/issues/7774 --- backend/app/dto/response/app.go | 6 ++--- backend/app/model/app.go | 2 +- backend/app/service/app.go | 40 +++++++------------------------- backend/app/service/app_utils.go | 38 ++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/backend/app/dto/response/app.go b/backend/app/dto/response/app.go index 2bd231deb122..fc5754ce726f 100644 --- a/backend/app/dto/response/app.go +++ b/backend/app/dto/response/app.go @@ -23,9 +23,9 @@ type AppUpdateRes struct { type AppDTO struct { model.App - Installed bool `json:"installed"` - Versions []string `json:"versions"` - Tags []model.Tag `json:"tags"` + Installed bool `json:"installed"` + Versions []string `json:"versions"` + Tags []TagDTO `json:"tags"` } type AppItem struct { diff --git a/backend/app/model/app.go b/backend/app/model/app.go index 2fd729d79be4..4e3b4756687a 100644 --- a/backend/app/model/app.go +++ b/backend/app/model/app.go @@ -49,7 +49,7 @@ func (i *App) GetDescription(ctx *gin.Context) string { var translations = make(map[string]string) _ = json.Unmarshal([]byte(i.Description), &translations) lang := strings.ToLower(common.GetLang(ctx)) - if desc, ok := translations[lang]; ok { + if desc, ok := translations[lang]; ok && desc != "" { return desc } if lang == "zh" { diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 644af4c6ad35..ba74c6665cd7 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -104,39 +104,11 @@ func (a AppService) PageApp(ctx *gin.Context, req request.AppSearch) (interface{ } appDTO.Description = ap.GetDescription(ctx) appDTOs = append(appDTOs, appDTO) - appTags, err := appTagRepo.GetByAppId(ap.ID) + tags, err := getAppTags(ap.ID, lang) if err != nil { - continue - } - var tagIds []uint - for _, at := range appTags { - tagIds = append(tagIds, at.TagId) - } - tags, err := tagRepo.GetByIds(tagIds) - if err != nil { - continue - } - for _, t := range tags { - if t.Name != "" { - tagDTO := response.TagDTO{ - ID: t.ID, - Key: t.Key, - Name: t.Name, - } - appDTO.Tags = append(appDTO.Tags, tagDTO) - } else { - var translations = make(map[string]string) - _ = json.Unmarshal([]byte(t.Translations), &translations) - if name, ok := translations[lang]; ok { - tagDTO := response.TagDTO{ - ID: t.ID, - Key: t.Key, - Name: name, - } - appDTO.Tags = append(appDTO.Tags, tagDTO) - } - } + return nil, err } + appDTO.Tags = tags installs, _ := appInstallRepo.ListBy(appInstallRepo.WithAppId(ap.ID)) appDTO.Installed = len(installs) > 0 } @@ -185,7 +157,11 @@ func (a AppService) GetApp(ctx *gin.Context, key string) (*response.AppDTO, erro versionsRaw = append(versionsRaw, detail.Version) } appDTO.Versions = common.GetSortedVersions(versionsRaw) - + tags, err := getAppTags(app.ID, strings.ToLower(common.GetLang(ctx))) + if err != nil { + return nil, err + } + appDTO.Tags = tags return &appDTO, nil } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 8da2468562e6..711f3e53b871 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -1607,3 +1607,41 @@ func RequestDownloadCallBack(downloadCallBackUrl string) { } _, _, _ = httpUtil.HandleGet(downloadCallBackUrl, http.MethodGet, constant.TimeOut5s) } + +func getAppTags(appID uint, lang string) ([]response.TagDTO, error) { + appTags, err := appTagRepo.GetByAppId(appID) + if err != nil { + return nil, err + } + var tagIds []uint + for _, at := range appTags { + tagIds = append(tagIds, at.TagId) + } + tags, err := tagRepo.GetByIds(tagIds) + if err != nil { + return nil, err + } + var res []response.TagDTO + for _, t := range tags { + if t.Name != "" { + tagDTO := response.TagDTO{ + ID: t.ID, + Key: t.Key, + Name: t.Name, + } + res = append(res, tagDTO) + } else { + var translations = make(map[string]string) + _ = json.Unmarshal([]byte(t.Translations), &translations) + if name, ok := translations[lang]; ok { + tagDTO := response.TagDTO{ + ID: t.ID, + Key: t.Key, + Name: name, + } + res = append(res, tagDTO) + } + } + } + return res, nil +}