Skip to content

Commit

Permalink
Merge pull request #142 from decert-me/feature/statistics
Browse files Browse the repository at this point in the history
Feature 用户标签显示
  • Loading branch information
0xdwong authored Jun 17, 2024
2 parents 40c221f + 4db2a80 commit 79d6cb3
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 4 deletions.
3 changes: 2 additions & 1 deletion internal/app/api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ func GetTwitterInfo(c *gin.Context) {
// GetUserInfo Get User Info
func GetUserInfo(c *gin.Context) {
address := c.Param("address")
LoginAddress := c.GetString("address")
if address == "" {
FailWithMessage(GetMessage(c, "ParameterError"), c)
return
}
if list, err := srv.GetUserInfo(address); err != nil {
if list, err := srv.GetUserInfo(address, LoginAddress); err != nil {
OkWithData(list, c)
} else {
OkWithData(list, c)
Expand Down
3 changes: 2 additions & 1 deletion internal/app/api/v2/open_quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func GetUserOpenQuestDetailList(c *gin.Context) {
return
}
address := c.GetString("address")
if list, total, err := srv.GetUserOpenQuestDetailListV2(address, r); err != nil {
LoginAddress := c.GetString("address")
if list, total, err := srv.GetUserOpenQuestDetailListV2(address, LoginAddress, r); err != nil {
FailWithMessage("获取失败:"+err.Error(), c)
} else {
OkWithDetailed(response.PageResult{
Expand Down
16 changes: 16 additions & 0 deletions internal/app/dao/admin_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dao

import (
"backend-go/internal/app/model"
)

func (d *Dao) IsAdmin(address string) (is bool, err error) {
var count int64
if err = d.db.Model(&model.AdminUser{}).Where("address = ?", address).Count(&count).Error; err != nil {
return false, err
}
if count == 0 {
return false, nil
}
return true, nil
}
32 changes: 32 additions & 0 deletions internal/app/dao/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dao

import "backend-go/internal/app/model"

func (d *Dao) GetUserTags(userID uint) (tags []string, err error) {
err = d.db.Model(&model.UsersTag{}).Select("tag.name").
Joins("join tag on users_tag.tag_id = tag.id").
Where("users_tag.user_id = ?", userID).
Find(&tags).Error
return
}

func (d *Dao) GetUserNameTagsByAddress(address string) (nickname, name string, tags []string, err error) {
var user model.Users
err = d.db.Model(&model.Users{}).
Where("address = ?", address).
First(&user).Error
if err != nil {
return
}
if user.Name != nil {
name = *user.Name
}
if user.NickName != nil {
nickname = *user.NickName
}
err = d.db.Model(&model.UsersTag{}).Select("tag.name").
Joins("join tag on users_tag.tag_id = tag.id").
Where("users_tag.user_id = ?", user.ID).
Find(&tags).Error
return
}
1 change: 1 addition & 0 deletions internal/app/model/response/open_quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type UserOpenQuestJsonElements struct {
PassScore int64 `gorm:"column:pass_score" form:"pass_score" json:"pass_score"` // 通过分数
TotalScore int64 `gorm:"column:total_score" form:"total_score" json:"total_score"` // 总分
UserScore int64 `gorm:"column:user_score" form:"user_score" json:"user_score"` // 用户分数
NickName *string `gorm:"column:nickname;type:varchar(200);default:''" json:"nickname" form:"nickname"`
}

type UserOpenQuestJsonElementsV2 struct {
Expand Down
13 changes: 13 additions & 0 deletions internal/app/model/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model

import "time"

type Tag struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
Name string `gorm:"column:name;unique;type:varchar(100);comment:标签名" json:"name"`
}

func (Tag) TableName() string {
return "tag"
}
1 change: 1 addition & 0 deletions internal/app/model/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Users struct {
Socials datatypes.JSON `gorm:"column:socials" json:"socials"`
ResourceTime time.Time `gorm:"column:resource_time"`
ParticleUserinfo datatypes.JSON `gorm:"column:particle_userinfo" json:"particle_userinfo"`
Name *string `gorm:"column:name;type:varchar(200);comment:用户名称;default:''" json:"name" form:"name"`
}
11 changes: 11 additions & 0 deletions internal/app/model/users_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

type UsersTag struct {
ID uint `gorm:"primarykey" json:"id"` // 主键ID
UserID uint `gorm:"column:user_id;comment:用户ID;index:user_id_tag_id,unique" json:"userID"` // 用户ID
TagID uint `gorm:"column:tag_id;comment:标签ID;index:user_id_tag_id,unique" json:"tagID"` // 标签ID
}

func (UsersTag) TableName() string {
return "users_tag"
}
25 changes: 24 additions & 1 deletion internal/app/service/open_quest_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ func (s *Service) GetUserOpenQuestListV2(address string, r request.GetUserOpenQu
}

// GetUserOpenQuestDetailListV2 获取用户开放题详情
func (s *Service) GetUserOpenQuestDetailListV2(address string, r request.GetUserOpenQuestDetailListRequest) (list []response.UserOpenQuestJsonElements, total int64, err error) {
func (s *Service) GetUserOpenQuestDetailListV2(address string, loginAddress string, r request.GetUserOpenQuestDetailListRequest) (list []response.UserOpenQuestJsonElements, total int64, err error) {
offset := (r.Page - 1) * r.PageSize
limit := r.PageSize
db := s.dao.DB().Model(&model.UserOpenQuest{})
// 是否管理员
isAdmin, err := s.dao.IsAdmin(loginAddress)
if err != nil {
return
}
// OpenQuestReviewStatus 1 未审核 2 已审核
countSQL := `
SELECT
Expand Down Expand Up @@ -205,6 +210,24 @@ func (s *Service) GetUserOpenQuestDetailListV2(address string, r request.GetUser
log.Errorv("AnswerCheck error", zap.Error(err))
return
}
var showStr string
showStr = fmt.Sprintf("%s...%s", list[i].Address[:6], list[i].Address[len(list[i].Address)-4:])
if isAdmin {
// 显示标签
nickname, name, tags, err := s.dao.GetUserNameTagsByAddress(list[i].Address)
if err == nil {
if nickname != "" {
showStr = nickname
}
if name != "" {
showStr += "-" + name
}
for i := 0; i < len(tags); i++ {
showStr += "," + tags[i]
}
}
}
list[i].NickName = &showStr
}
return
}
27 changes: 26 additions & 1 deletion internal/app/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,36 @@ import (
"strings"
)

func (s *Service) GetUserInfo(address string) (res interface{}, err error) {
func (s *Service) GetUserInfo(address string, loginAddress string) (res interface{}, err error) {
var user model.Users
if user, err = s.dao.GetUser(address); err != nil {
return
}
// 是否管理员
isAdmin, err := s.dao.IsAdmin(loginAddress)
if err != nil {
return
}
// 管理员显示
if isAdmin {
var showStr string
showStr = fmt.Sprintf("%s...%s", address[:6], address[len(address)-4:])
if user.NickName != nil && *user.NickName != "" {
showStr = *user.NickName
}
if user.Name != nil && *user.Name != "" {
showStr += "-" + *user.Name
}
// 显示标签
tags, err := s.dao.GetUserTags(user.ID)
if err == nil {
for i := 0; i < len(tags); i++ {
showStr += "," + tags[i]
}
}
user.NickName = &showStr
return user, err
}
// default nickname
if user.NickName == nil || *user.NickName == "" {
if len(user.Address) > 10 {
Expand Down

0 comments on commit 79d6cb3

Please sign in to comment.