2018-07-14 18:29:24 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
// Create implements the create method to assign a user to a team
|
2018-07-18 08:56:19 +02:00
|
|
|
func (tm *TeamMember) Create(doer *User) (err error) {
|
2018-07-26 09:53:32 +02:00
|
|
|
// Check if the team extst
|
|
|
|
_, err = GetTeamByID(tm.TeamID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user exists
|
|
|
|
_, _, err = GetUserByID(tm.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:06:41 +02:00
|
|
|
// Check if that user is already part of the team
|
|
|
|
exists, err := x.Where("team_id = ? AND user_id = ?", tm.TeamID, tm.UserID).
|
|
|
|
Get(&TeamMember{})
|
|
|
|
if exists {
|
|
|
|
return ErrUserIsMemberOfTeam{tm.TeamID, tm.UserID}
|
|
|
|
}
|
|
|
|
|
2018-07-26 09:53:32 +02:00
|
|
|
// Insert the user
|
2018-07-14 18:29:24 +02:00
|
|
|
_, err = x.Insert(tm)
|
|
|
|
return
|
|
|
|
}
|