Skip to content

Commit

Permalink
feat(badge): badge manage add badge search
Browse files Browse the repository at this point in the history
  • Loading branch information
kumfo committed Aug 13, 2024
1 parent bb40366 commit cf327f5
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
25 changes: 6 additions & 19 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -234,6 +215,12 @@ const docTemplate = `{
"description": "badge status",
"name": "status",
"in": "query"
},
{
"type": "string",
"description": "search param",
"name": "q",
"in": "query"
}
],
"responses": {
Expand Down
6 changes: 6 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@
"description": "badge status",
"name": "status",
"in": "query"
},
{
"type": "string",
"description": "search param",
"name": "q",
"in": "query"
}
],
"responses": {
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,10 @@ paths:
in: query
name: status
type: string
- description: search param
in: query
name: q
type: string
produces:
- application/json
responses:
Expand Down
1 change: 1 addition & 0 deletions internal/controller_admin/badge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion internal/repo/badge/badge_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/schema/badge_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 51 additions & 10 deletions internal/service/badge/badge_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit cf327f5

Please sign in to comment.