Skip to content

Commit

Permalink
Merge pull request #448 from hit9/dev
Browse files Browse the repository at this point in the history
Delete related rules and user relationships on project detected.
  • Loading branch information
hit9 committed Mar 3, 2016
2 parents b0a6c58 + 9eae394 commit 93714ac
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion webapp/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,29 @@ func deleteProject(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
ResponseError(w, ErrProjectID)
return
}
proj := &models.Project{ID: id}
// Delete Its Rules
var rules []models.Rule
if err := db.Admin.DB().Model(proj).Related(&rules).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
for i := 0; i < len(rules); i++ {
db.Admin.DB().Delete(&rules[i])
db.Admin.RulesCache.Delete(rules[i].ID)
}
// Delete Its user relationships.
var users []models.User
if err := db.Admin.DB().Model(proj).Association("Users").Find(&users).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
if err := db.Admin.DB().Model(proj).Association("Users").Delete(users).Error; err != nil {
ResponseError(w, NewUnexceptedWebError(err))
return
}
// Delete.
if err := db.Admin.DB().Delete(&models.Project{ID: id}).Error; err != nil {
if err := db.Admin.DB().Delete(proj).Error; err != nil {
switch err {
case gorm.RecordNotFound:
ResponseError(w, ErrProjectNotFound)
Expand Down

0 comments on commit 93714ac

Please sign in to comment.