ToggleLike 点赞/取消点赞
()
| 133 | |
| 134 | // ToggleLike 点赞/取消点赞 |
| 135 | func (l *Like) ToggleLike() error { |
| 136 | exists, err := l.Exists() |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | |
| 141 | if exists { |
| 142 | // 存在则更新状态(取消点赞) |
| 143 | currentLike, err := GetLikeByUserObject(l.UserID, l.Type, l.ObjectID) |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | newStatus := LikeStatusCancel |
| 148 | if currentLike.Status == LikeStatusCancel { |
| 149 | newStatus = LikeStatusActive |
| 150 | } |
| 151 | return currentLike.UpdateStatus(newStatus) |
| 152 | } |
| 153 | |
| 154 | // 不存在则创建新记录 |
| 155 | return l.Create() |
| 156 | } |
| 157 | |
| 158 | // GetLikeByUserObject 根据用户和对象获取点赞记录 |
| 159 | func GetLikeByUserObject(userID int64, objectType string, objectID int64) (*Like, error) { |
nothing calls this directly
no test coverage detected