code.gitea.io/gitea@v1.21.7/models/user/badge.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package user
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  )
    11  
    12  // Badge represents a user badge
    13  type Badge struct {
    14  	ID          int64 `xorm:"pk autoincr"`
    15  	Description string
    16  	ImageURL    string
    17  }
    18  
    19  // UserBadge represents a user badge
    20  type UserBadge struct { //nolint:revive
    21  	ID      int64 `xorm:"pk autoincr"`
    22  	BadgeID int64
    23  	UserID  int64 `xorm:"INDEX"`
    24  }
    25  
    26  func init() {
    27  	db.RegisterModel(new(Badge))
    28  	db.RegisterModel(new(UserBadge))
    29  }
    30  
    31  // GetUserBadges returns the user's badges.
    32  func GetUserBadges(ctx context.Context, u *User) ([]*Badge, int64, error) {
    33  	sess := db.GetEngine(ctx).
    34  		Select("`badge`.*").
    35  		Join("INNER", "user_badge", "`user_badge`.badge_id=badge.id").
    36  		Where("user_badge.user_id=?", u.ID)
    37  
    38  	badges := make([]*Badge, 0, 8)
    39  	count, err := sess.FindAndCount(&badges)
    40  	return badges, count, err
    41  }