Skip to content

Commit

Permalink
improve: app GET user public info api now response correct group data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Mar 14, 2024
1 parent 4c032aa commit 81075c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
4 changes: 1 addition & 3 deletions internal/db/dao/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ func (a *User) GetNoSshDevIds(tx *gorm.DB) ([]uint, error) {

func (a *User) GetUserInfoPubByIds(tx *gorm.DB, ids ...uint) ([]dto.UserInfoPublic, error) {
var t = make([]dto.UserInfoPublic, 0)
tx = tx.Model(a)
tx = a.sqlJoinUserGroups(tx)
return t, tx.Where("id IN ?", ids).
return t, tx.Model(a).Where("id IN ?", ids).
Clauses(clause.OrderBy{Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{ids}, WithoutParentheses: true}}).
Find(&t).Error
}
Expand Down
11 changes: 11 additions & 0 deletions internal/db/dao/UserGroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dao
import (
"github.com/ncuhome/GeniusAuthoritarian/internal/db/dto"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type UserGroups struct {
Expand Down Expand Up @@ -56,6 +57,16 @@ func (a *UserGroups) GetUserGroupsForShowByUID(tx *gorm.DB) ([]dto.Group, error)
return t, a.sqlGetUserGroupsByUID(tx).Find(&t).Error
}

func (a *UserGroups) GetUserGroupsForPubByUIDWithPreOrder(tx *gorm.DB, uid ...uint) ([]dto.GroupWithOrder, error) {
var t []dto.GroupWithOrder
groupModel := &BaseGroup{}
tx = tx.Model(groupModel)
tx = groupModel.sqlJoinUserGroups(tx)
return t, tx.Where("user_groups.uid IN ?", uid).
Clauses(clause.OrderBy{Expression: clause.Expr{SQL: "FIELD(user_groups.uid,?),user_groups.id DESC", Vars: []interface{}{uid}, WithoutParentheses: true}}).
Find(&t).Error
}

func (a *UserGroups) GetUserGroupsForAppCodeByUID(tx *gorm.DB, appCode string) *gorm.DB {
appGroupsTx := (&AppGroup{}).sqlGetGroupsByAppCode(tx, appCode).Select("base_groups.name")
return a.sqlGetUserGroupsByUID(appGroupsTx)
Expand Down
6 changes: 6 additions & 0 deletions internal/db/dto/Group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ type GroupRelateApp struct {
Group
AppID uint `json:"-"`
}

type GroupWithOrder struct {
ID uint
UID uint `gorm:"uid"`
Name string
}
33 changes: 32 additions & 1 deletion internal/service/User.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"container/list"
"context"
"github.com/Mmx233/daoUtil"
"github.com/ncuhome/GeniusAuthoritarian/internal/db/dao"
Expand Down Expand Up @@ -30,7 +31,37 @@ func (a UserSrv) GetUserNotInPhoneSlice(phone []string) ([]dao.User, error) {
}

func (a UserSrv) GetUserInfoPublic(id ...uint) ([]dto.UserInfoPublic, error) {
return (&dao.User{}).GetUserInfoPubByIds(a.DB, id...)
data, err := (&dao.User{}).GetUserInfoPubByIds(a.DB, id...)
if err != nil {
return nil, err
}

groups, err := (&dao.UserGroups{}).GetUserGroupsForPubByUIDWithPreOrder(a.DB, id...)
if err != nil {
return nil, err
}

for i, ii := 0, 0; i < len(data) && ii < len(groups); i++ {
user := &data[i]
userGroups := list.New()
for ii < len(groups) {
group := groups[ii]
if user.ID != group.UID {
break
}
userGroups.PushBack(dto.Group{
ID: group.ID,
Name: group.Name,
})
ii++
}
user.Groups = make([]dto.Group, userGroups.Len())
for el, iii := userGroups.Front(), 0; el != nil; el, iii = el.Next(), iii+1 {
user.Groups[iii] = el.Value.(dto.Group)
}
}

return data, nil
}

func (a UserSrv) CreateAll(users []dao.User) error {
Expand Down

0 comments on commit 81075c7

Please sign in to comment.