Implemented team delete method
This commit is contained in:
parent
4cf0cd233c
commit
8a06db5351
3 changed files with 51 additions and 0 deletions
32
models/teams_delete.go
Normal file
32
models/teams_delete.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
func (t *Team) Delete(id int64) (err error) {
|
||||||
|
|
||||||
|
// Check if the team exists
|
||||||
|
_, err = GetTeamByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the team
|
||||||
|
_, err = x.ID(id).Delete(&Team{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete team members
|
||||||
|
_, err = x.Where("team_id = ?", id).Delete(&TeamMember{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete team <-> namespace relations
|
||||||
|
_, err = x.Where("team_id = ?", id).Delete(&TeamNamespace{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete team <-> lists relations
|
||||||
|
_, err = x.Where("team_id = ?", id).Delete(&TeamList{})
|
||||||
|
return
|
||||||
|
}
|
|
@ -17,3 +17,18 @@ func (t *Team) CanUpdate(user *User, id int64) bool {
|
||||||
|
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanDelete
|
||||||
|
func (t *Team) CanDelete(user *User, id int64) bool {
|
||||||
|
t.ID = id
|
||||||
|
return t.IsAdmin(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsAdmin
|
||||||
|
func (t *Team) IsAdmin(user *User) bool {
|
||||||
|
exists, _ := x.Where("team_id = ?", t.ID).
|
||||||
|
And("user_id = ?", user.ID).
|
||||||
|
And("is_admin = ?", true).
|
||||||
|
Get(&TeamMember{})
|
||||||
|
return exists
|
||||||
|
}
|
|
@ -33,6 +33,10 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.")
|
return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if models.IsErrTeamDoesNotExist(err) {
|
||||||
|
return echo.NewHTTPError(http.StatusNotFound, "This team does not exist.")
|
||||||
|
}
|
||||||
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue