1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package group_m
import (
"gorm.io/gorm"
)
type GroupMember struct {
GroupId string
UserId uint64
}
func (gm *GroupMember) Create(db *gorm.DB) error {
return db.Create(gm).Error
}
func (gm *GroupMember) Remove(db *gorm.DB) error {
return db.Where(gm).Delete(&GroupMember{}).Error
}
func (gm *GroupMember) Find(db *gorm.DB) ([]GroupMember, error) {
rows := make([]GroupMember, 0)
if err := db.Where(gm).Order("created_time DESC").Find(&rows).Error; err != nil {
return nil, err
}
return rows, nil
}
func GetMembers(db *gorm.DB, groupId string) ([]GroupMember, error) {
gm := GroupMember{
GroupId: groupId,
}
return gm.Find(db)
}
func GetMemberCount(db *gorm.DB, groupId string) (uint, error) {
gm := GroupMember{
GroupId: groupId,
}
var c int64
if err := db.Model(&GroupMember{}).Where(&gm).Count(&c).Error; err != nil {
return 0, err
}
return uint(c), nil
}
func GetJoinedGroups(db *gorm.DB, userId uint64) ([]GroupMember, error) {
gm := GroupMember{
UserId: userId,
}
return gm.Find(db)
}
func IsGroupMember(db *gorm.DB, groupId string, userId uint64) (bool, error) {
gm := GroupMember{
GroupId: groupId,
UserId: userId,
}
rows, err := gm.Find(db)
if err != nil {
return false, err
}
return len(rows) > 0, nil
}