fix: delete associations first when deleting token or machine
All checks were successful
continuous-integration/drone/tag Build is passing
All checks were successful
continuous-integration/drone/tag Build is passing
This commit is contained in:
parent
95e9ae41ef
commit
402f755497
1 changed files with 14 additions and 2 deletions
16
main.go
16
main.go
|
@ -121,14 +121,26 @@ func editMachine(c *gin.Context) {
|
||||||
c.Redirect(http.StatusFound, "/machines")
|
c.Redirect(http.StatusFound, "/machines")
|
||||||
}
|
}
|
||||||
func deleteMachine(c *gin.Context) {
|
func deleteMachine(c *gin.Context) {
|
||||||
if err := db.DB.Where("id = ?", c.Param("id")).Delete(db.Machine{}).Error; err != nil {
|
var machine db.Machine
|
||||||
|
if err := db.DB.Where("id = ?", c.Param("id")).First(&machine).Error; err != nil {
|
||||||
|
returnInternalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.DB.Model(&machine).Association("Tokens").Clear()
|
||||||
|
if err := db.DB.Delete(machine).Error; err != nil {
|
||||||
returnInternalError(c, err)
|
returnInternalError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Redirect(http.StatusFound, "/machines")
|
c.Redirect(http.StatusFound, "/machines")
|
||||||
}
|
}
|
||||||
func deleteToken(c *gin.Context) {
|
func deleteToken(c *gin.Context) {
|
||||||
if err := db.DB.Where("id = ?", c.Param("id")).Delete(db.Token{}).Error; err != nil {
|
var token db.Token
|
||||||
|
if err := db.DB.Where("id = ?", c.Param("id")).First(&token).Error; err != nil {
|
||||||
|
returnInternalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.DB.Model(&token).Association("Machines").Clear()
|
||||||
|
if err := db.DB.Delete(token).Error; err != nil {
|
||||||
returnInternalError(c, err)
|
returnInternalError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue