A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(badge): badge manage add badge search
kumfocf327f59dcd6
diff --git a/docs/docs.go b/docs/docs.goindex eaa7133a..7fef400f 100644--- a/docs/docs.go+++ b/docs/docs.go@@ -1,22 +1,3 @@-/*- * Licensed to the Apache Software Foundation (ASF) under one- * or more contributor license agreements. See the NOTICE file- * distributed with this work for additional information- * regarding copyright ownership. The ASF licenses this file- * to you under the Apache License, Version 2.0 (the- * "License"); you may not use this file except in compliance- * with the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing,- * software distributed under the License is distributed on an- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY- * KIND, either express or implied. See the License for the- * specific language governing permissions and limitations- * under the License.- */-// Package docs Code generated by swaggo/swag. DO NOT EDITpackage docs@@ -234,6 +215,12 @@ const docTemplate = `{"description": "badge status","name": "status","in": "query"+ },+ {+ "type": "string",+ "description": "search param",+ "name": "q",+ "in": "query"}],"responses": {diff --git a/docs/swagger.json b/docs/swagger.jsonindex 116a3a6a..234d2f13 100644--- a/docs/swagger.json+++ b/docs/swagger.json@@ -204,6 +204,12 @@"description": "badge status","name": "status","in": "query"+ },+ {+ "type": "string",+ "description": "search param",+ "name": "q",+ "in": "query"}],"responses": {diff --git a/docs/swagger.yaml b/docs/swagger.yamlindex 116b5932..1a5979e6 100644--- a/docs/swagger.yaml+++ b/docs/swagger.yaml@@ -3023,6 +3023,10 @@ paths:in: queryname: statustype: string+ - description: search param+ in: query+ name: q+ type: stringproduces:- application/jsonresponses:diff --git a/internal/controller_admin/badge_controller.go b/internal/controller_admin/badge_controller.goindex 8842592f..4a44f176 100644--- a/internal/controller_admin/badge_controller.go+++ b/internal/controller_admin/badge_controller.go@@ -47,6 +47,7 @@ func NewBadgeController(badgeService *badge.BadgeService) *BadgeController {// @Param page query int false "page"// @Param page_size query int false "page size"// @Param status query string false "badge status" Enums(, active, inactive)+// @Param q query string false "search param"// @Success 200 {object} handler.RespBody{data=[]schema.GetBadgeListPagedResp}// @Router /answer/admin/api/badges [get]func (b *BadgeController) GetBadgeList(ctx *gin.Context) {diff --git a/internal/repo/badge/badge_repo.go b/internal/repo/badge/badge_repo.goindex ebdb0425..8537e389 100644--- a/internal/repo/badge/badge_repo.go+++ b/internal/repo/badge/badge_repo.go@@ -95,8 +95,15 @@ func (r *badgeRepo) ListByLevelAndGroup(ctx context.Context, level entity.BadgeL// ListPaged returns a list of activated badgesfunc (r *badgeRepo) ListPaged(ctx context.Context, page int, pageSize int) (badges []*entity.Badge, total int64, err error) {badges = make([]*entity.Badge, 0)+ total = 0+session := r.data.DB.Context(ctx).Where("status <> ?", entity.BadgeStatusDeleted)- total, err = pager.Help(page, pageSize, &badges, &entity.Badge{}, session)+ if page == 0 || pageSize == 0 {+ err = session.Find(&badges)+ } else {+ total, err = pager.Help(page, pageSize, &badges, &entity.Badge{}, session)+ }+if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}diff --git a/internal/schema/badge_schema.go b/internal/schema/badge_schema.goindex 39a2e2ae..088193ad 100644--- a/internal/schema/badge_schema.go+++ b/internal/schema/badge_schema.go@@ -75,6 +75,8 @@ type GetBadgeListPagedReq struct {PageSize int `validate:"omitempty,min=1" form:"page_size"`// badge statusStatus BadgeStatus `validate:"omitempty" form:"status"`+ // query condition+ Query string `validate:"omitempty" form:"q"`}type GetBadgeListPagedResp struct {diff --git a/internal/service/badge/badge_service.go b/internal/service/badge/badge_service.goindex 7daab796..c9f3797a 100644--- a/internal/service/badge/badge_service.go+++ b/internal/service/badge/badge_service.go@@ -30,6 +30,7 @@ import ("github.com/apache/incubator-answer/pkg/uid""github.com/gin-gonic/gin""github.com/segmentfault/pacman/errors"+ "strings")type BadgeRepo interface {@@ -137,20 +138,44 @@ func (b *BadgeService) ListPaged(ctx context.Context, req *schema.GetBadgeListPavar (groups []*entity.BadgeGroupbadges []*entity.Badge+ badge *entity.Badge+ exists boolgroupMap = make(map[int64]string, 0))- switch req.Status {- case schema.BadgeStatusActive:- badges, total, err = b.badgeRepo.ListActivated(ctx, req.Page, req.PageSize)- case schema.BadgeStatusInactive:- badges, total, err = b.badgeRepo.ListInactivated(ctx, req.Page, req.PageSize)- default:- badges, total, err = b.badgeRepo.ListPaged(ctx, req.Page, req.PageSize)- }+ total = 0- if err != nil {- return+ if len(req.Query) > 0 {+ isID := strings.Index(req.Query, "badge:")+ if isID != 0 {+ badges, err = b.searchByName(ctx, req.Query)+ if err != nil {+ return+ }+ } else {+ req.Query = strings.TrimSpace(strings.TrimLeft(req.Query, "badge:"))+ id := uid.DeShortID(req.Query)+ if len(id) == 0 {+ return+ }+ badge, exists, err = b.badgeRepo.GetByID(ctx, id)+ if err != nil || !exists {+ return+ }+ badges = append(badges, badge)+ }+ } else {+ switch req.Status {+ case schema.BadgeStatusActive:+ badges, total, err = b.badgeRepo.ListActivated(ctx, req.Page, req.PageSize)+ case schema.BadgeStatusInactive:+ badges, total, err = b.badgeRepo.ListInactivated(ctx, req.Page, req.PageSize)+ default:+ badges, total, err = b.badgeRepo.ListPaged(ctx, req.Page, req.PageSize)+ }+ if err != nil {+ return+ }}// find all group and build group map@@ -179,6 +204,22 @@ func (b *BadgeService) ListPaged(ctx context.Context, req *schema.GetBadgeListPareturn}+// searchByName+func (b *BadgeService) searchByName(ctx context.Context, name string) (result []*entity.Badge, err error) {+ var badges []*entity.Badge+ name = strings.ToLower(name)+ result = make([]*entity.Badge, 0)++ badges, _, err = b.badgeRepo.ListPaged(ctx, 0, 0)+ for _, badge := range badges {+ tn := strings.ToLower(translator.Tr(handler.GetLangByCtx(ctx), badge.Name))+ if strings.Contains(tn, name) {+ result = append(result, badge)+ }+ }+ return+}+// GetBadgeInfo get badge infofunc (b *BadgeService) GetBadgeInfo(ctx *gin.Context, id string, userID string) (info *schema.GetBadgeInfoResp, err error) {var (