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.
 
 
 
 

44 regels
1.1 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) error {
  27. friend := Friend{}
  28. return gerrors.WrapError(db.DB.Where("user_id = ? and friend_id = ?", userId, friendId).Delete(&friend).Error)
  29. }
  30. // List 获取好友列表
  31. func (*friendRepo) List(userId int64, status int) ([]Friend, error) {
  32. var friends []Friend
  33. err := db.DB.Where("user_id = ? and status = ?", userId, status).Find(&friends).Error
  34. return friends, gerrors.WrapError(err)
  35. }