mirror of
https://github.com/thomiceli/opengist.git
synced 2024-12-22 20:42:40 +00:00
Decrement likes counters on user delete
This commit is contained in:
parent
bee5d045c3
commit
8630f647c6
1 changed files with 17 additions and 0 deletions
|
@ -1,5 +1,7 @@
|
|||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Username string `form:"username" gorm:"uniqueIndex" validate:"required,max=24,alphanum,notreserved"`
|
||||
|
@ -46,6 +48,21 @@ func CreateUser(user *User) error {
|
|||
}
|
||||
|
||||
func DeleteUserByID(userid string) error {
|
||||
// Decrement likes counter for all gists liked by this user
|
||||
// The likes will be automatically deleted by the foreign key constraint
|
||||
err := db.Model(&Gist{}).
|
||||
Omit("updated_at").
|
||||
Where("id IN (?)", db.
|
||||
Select("gist_id").
|
||||
Table("likes").
|
||||
Where("user_id = ?", userid),
|
||||
).
|
||||
UpdateColumn("nb_likes", gorm.Expr("nb_likes - 1")).
|
||||
Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.Delete(&User{}, "id = ?", userid).Error
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue