golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

group_user_repo.go 1.8 KiB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package repo
  2. import (
  3. "gim/internal/logic/domain/group/model"
  4. "gim/pkg/db"
  5. "gim/pkg/gerrors"
  6. "github.com/jinzhu/gorm"
  7. )
  8. type groupUserRepo struct{}
  9. var GroupUserRepo = new(groupUserRepo)
  10. // ListByUserId 获取用户加入的群组信息
  11. func (*groupUserRepo) ListByUserId(userId int64) ([]model.Group, error) {
  12. var groups []model.Group
  13. err := db.DB.Select("g.id,g.name,g.avatar_url,g.introduction,g.user_num,g.extra,g.create_time,g.update_time").
  14. Table("group_user u").
  15. Joins("join `group` g on u.group_id = g.id").
  16. Where("u.user_id = ?", userId).
  17. Find(&groups).Error
  18. if err != nil {
  19. return nil, gerrors.WrapError(err)
  20. }
  21. return groups, nil
  22. }
  23. // ListUser 获取群组用户信息
  24. func (*groupUserRepo) ListUser(groupId int64) ([]model.GroupUser, error) {
  25. var groupUsers []model.GroupUser
  26. err := db.DB.Find(&groupUsers, "group_id = ?", groupId).Error
  27. if err != nil {
  28. return nil, gerrors.WrapError(err)
  29. }
  30. return groupUsers, nil
  31. }
  32. // Get 获取群组用户信息,用户不存在返回nil
  33. func (*groupUserRepo) Get(groupId, userId int64) (*model.GroupUser, error) {
  34. var groupUser model.GroupUser
  35. err := db.DB.First(&groupUser, "group_id = ? and user_id = ?", groupId, userId).Error
  36. if err != nil && err != gorm.ErrRecordNotFound {
  37. return nil, gerrors.WrapError(err)
  38. }
  39. if err == gorm.ErrRecordNotFound {
  40. return nil, nil
  41. }
  42. return &groupUser, nil
  43. }
  44. // Save 将用户添加到群组
  45. func (*groupUserRepo) Save(groupUser *model.GroupUser) error {
  46. err := db.DB.Save(&groupUser).Error
  47. if err != nil {
  48. return gerrors.WrapError(err)
  49. }
  50. return nil
  51. }
  52. // Delete 将用户从群组删除
  53. func (d *groupUserRepo) Delete(groupId int64, userId int64) error {
  54. err := db.DB.Exec("delete from group_user where group_id = ? and user_id = ?",
  55. groupId, userId).Error
  56. if err != nil {
  57. return gerrors.WrapError(err)
  58. }
  59. return nil
  60. }