This is an automated email from the ASF dual-hosted git repository.
kumfo pushed a commit to branch feat/1.4.0/badge
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
The following commit(s) were added to refs/heads/feat/1.4.0/badge by this push:
new cf327f59 feat(badge): badge manage add badge search
cf327f59 is described below
commit cf327f59dcd621a914893b01100075549cea0a1b
Author: kumfo <[email protected]>
AuthorDate: Tue Aug 13 17:50:10 2024 +0800
feat(badge): badge manage add badge search
---
docs/docs.go | 25 +++--------
docs/swagger.json | 6 +++
docs/swagger.yaml | 4 ++
internal/controller_admin/badge_controller.go | 1 +
internal/repo/badge/badge_repo.go | 9 +++-
internal/schema/badge_schema.go | 2 +
internal/service/badge/badge_service.go | 61 ++++++++++++++++++++++-----
7 files changed, 78 insertions(+), 30 deletions(-)
diff --git a/docs/docs.go b/docs/docs.go
index 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 EDIT
package 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.json
index 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.yaml
index 116b5932..1a5979e6 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -3023,6 +3023,10 @@ paths:
in: query
name: status
type: string
+ - description: search param
+ in: query
+ name: q
+ type: string
produces:
- application/json
responses:
diff --git a/internal/controller_admin/badge_controller.go
b/internal/controller_admin/badge_controller.go
index 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.go
index 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 badges
func (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.go
index 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 status
Status 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.go
index 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.GetBadgeListPa
var (
groups []*entity.BadgeGroup
badges []*entity.Badge
+ badge *entity.Badge
+ exists bool
groupMap = 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.GetBadgeListPa
return
}
+// 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 info
func (b *BadgeService) GetBadgeInfo(ctx *gin.Context, id string, userID
string) (info *schema.GetBadgeInfoResp, err error) {
var (