golang-im聊天
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

81 satır
2.2 KiB

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