model.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package friend
  2. import (
  3. "fmt"
  4. . "prime/basic"
  5. auth "prime/user"
  6. "gorm.io/gorm"
  7. )
  8. type Application struct {
  9. gorm.Model
  10. SenderID uint
  11. Sender auth.User
  12. ReceiverID uint
  13. Receiver auth.User
  14. Status uint8
  15. Message []byte
  16. }
  17. func Migrate() {
  18. db := DB()
  19. db.AutoMigrate(&Application{})
  20. // db.Create(&User{Username: "foo", Password: "bar"})
  21. }
  22. func GetFriends(uid uint) ([]auth.Profile, error) {
  23. db := DB()
  24. var friends []auth.User
  25. var profiles []auth.Profile
  26. var user auth.User
  27. res := db.Find(&user, uid)
  28. if res.RowsAffected == 1 {
  29. if err := db.Model(&user).Association("Friends").Find(&friends); err != nil {
  30. return nil, err
  31. }
  32. for _, friend := range friends {
  33. var profile auth.Profile
  34. res := db.Find(&profile, "user_id = ?", friend.ID)
  35. if res.Error != nil {
  36. return nil, res.Error
  37. }
  38. profiles = append(profiles, profile)
  39. }
  40. return profiles, nil
  41. }
  42. return nil, res.Error
  43. }
  44. func IsFriend(a uint, b uint) (bool, error) {
  45. db := DB()
  46. var alice, bob auth.User
  47. res := db.Find(&alice, a)
  48. if res.RowsAffected == 1 {
  49. err := db.Model(&alice).Association("Friends").Find(&bob, b)
  50. return bob.ID == b, err
  51. }
  52. return false, res.Error
  53. }
  54. func AddFriend(a uint, b uint) error {
  55. res, err := IsFriend(a, b)
  56. if err != nil {
  57. fmt.Printf("isfriend err %s\n", err.Error())
  58. return err
  59. }
  60. if res {
  61. return ErrFriendExist
  62. }
  63. var alice, bob auth.User
  64. db := DB()
  65. resA := db.Take(&alice, a)
  66. resB := db.Take(&bob, b)
  67. if resA.RowsAffected == 1 && resB.RowsAffected == 1 {
  68. alice.Friends = append(alice.Friends, &bob)
  69. bob.Friends = append(bob.Friends, &alice)
  70. db.Save(alice)
  71. db.Save(bob)
  72. return nil
  73. }
  74. return gorm.ErrRecordNotFound
  75. }
  76. func ApplyFriend(from uint, to uint, message []byte) (uint, error) {
  77. res, err := IsFriend(from, to)
  78. if err != nil {
  79. return 0, err
  80. }
  81. if res {
  82. return 0, ErrFriendExist
  83. }
  84. db := DB()
  85. var sender, receiver auth.User
  86. resA := db.Take(&sender, from)
  87. resB := db.Take(&receiver, to)
  88. if resA.RowsAffected != 1 || resB.RowsAffected != 1 {
  89. return 0, gorm.ErrRecordNotFound
  90. }
  91. app := Application{
  92. Sender: sender, Receiver: receiver, Status: 0, Message: message,
  93. }
  94. db.Create(&app)
  95. // TODO: add a notice ?
  96. return app.ID, nil
  97. }
  98. func ReplyApplication(uid uint, appID uint, accepted bool) error {
  99. db := DB()
  100. var app Application
  101. res := db.Find(&app, appID)
  102. fmt.Printf("%+v\n%d\n", app, res.RowsAffected)
  103. if res.RowsAffected != 1 {
  104. return res.Error
  105. }
  106. if app.ReceiverID != uid {
  107. return ErrForbidden
  108. }
  109. if app.Status != 0 {
  110. return ErrApplicationAlreadyReplied
  111. }
  112. if accepted {
  113. app.Status = 1
  114. err := AddFriend(app.SenderID, uid)
  115. if err != nil {
  116. fmt.Printf("addfriend error %s\n", err.Error())
  117. return err
  118. }
  119. } else {
  120. app.Status = 2
  121. }
  122. res = db.Save(&app)
  123. return res.Error
  124. }
  125. func GetApplications(uid uint) ([]Application, error) {
  126. db := DB()
  127. var user auth.User
  128. res := db.Take(&user, uid)
  129. var apps []Application
  130. if res.RowsAffected == 1 {
  131. db.Find(&apps, "sender_id = ? OR receiver_id = ?", uid, uid)
  132. return apps, nil
  133. }
  134. return apps, res.Error
  135. }