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.
 
 
 
 

54 lines
1.3 KiB

  1. package friend
  2. import (
  3. "gim/pkg/db"
  4. "gim/pkg/gerrors"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type friendRepo struct{}
  8. var FriendRepo = new(friendRepo)
  9. // Get 获取好友
  10. func (*friendRepo) Get(userId, friendId int64) (*Friend, error) {
  11. friend := Friend{}
  12. err := db.DB.First(&friend, "user_id = ? and friend_id = ?", userId, friendId).Error
  13. if err != nil && err != gorm.ErrRecordNotFound {
  14. return nil, err
  15. }
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, nil
  18. }
  19. return &friend, nil
  20. }
  21. // Save 添加好友
  22. func (*friendRepo) Save(friend *Friend) error {
  23. return gerrors.WrapError(db.DB.Save(&friend).Error)
  24. }
  25. // Delete 删除好友
  26. func (*friendRepo) Delete(userId, friendId int64) (err error) {
  27. friend := Friend{}
  28. //1、正向删除自己这一方
  29. err = gerrors.WrapError(db.DB.Where("user_id = ? and friend_id = ?", userId, friendId).Delete(&friend).Error)
  30. if err != nil {
  31. return err
  32. }
  33. //2、反向删除好友这一方
  34. err = gerrors.WrapError(db.DB.Where("user_id = ? and friend_id = ?", friendId, userId).Delete(&friend).Error)
  35. if err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. // List 获取好友列表
  41. func (*friendRepo) List(userId int64, status int) ([]Friend, error) {
  42. var friends []Friend
  43. err := db.DB.Where("user_id = ? and status = ?", userId, status).Find(&friends).Error
  44. return friends, gerrors.WrapError(err)
  45. }