Last active
July 27, 2018 15:52
-
-
Save Cibernomadas/4396877ba16d508f966ebb63706258c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (u *User) Follow(db *gorm.DB, user User) { | |
if !u.IsFollowing(db, user) { | |
db.Model(&u).Association("Followers").Append(&user) | |
} | |
} | |
func (u *User) Unfollow(db *gorm.DB, user User) { | |
if u.IsFollowing(db, user) { | |
db.Model(&u).Association("Followers").Delete(&user) | |
} | |
} | |
func (u *User) IsFollowing(db *gorm.DB, user User) bool { | |
return db.Model(&u).Association("Followers").Find(user).Count() > 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment