use transaction

master
Evan Chen 2021-12-25 00:56:16 +08:00
parent d9232010fc
commit 95dba85b9f
1 changed files with 11 additions and 13 deletions

View File

@ -35,17 +35,15 @@ func HasEventAccess(c *gin.Context, e *Event, cid string) error {
return nil return nil
} }
func ChangeCalGroup(cal_id, to string) error { func ChangeCalGroup(tx *gorm.DB, cal_id, to string) error {
return db.Transaction(func(tx *gorm.DB) error { affected := tx.Exec(`update calendars set group_id = (
affected := tx.Exec(`update calendars set group_id = ( select id from groups where name = ?
select id from groups where name = ? ) where id = ?`, to, cal_id).RowsAffected
) where id = ?`, to, cal_id).RowsAffected if affected == 0 {
if affected == 0 { return errors.ErrorNotFound
return errors.ErrorNotFound }
} tx.Exec(`update events set group_id = (
tx.Exec(`update events set group_id = ( select id from groups where name = ?
select id from groups where name = ? ) where calendar_id = ?`, to, cal_id)
) where calendar_id = ?`, to, cal_id) return nil
return nil
})
} }