diff options
author | Unknown <joe2010xtmf@163.com> | 2014-07-02 16:42:16 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-07-02 16:42:16 -0400 |
commit | 465dc962b5e1febdfc988419d4d03e98f777019f (patch) | |
tree | 8c0f4abc434f75a7add718e98b077cb245608062 /models/org.go | |
parent | e57aebb31667a1de54e82cd26e9bbdb8e795039d (diff) | |
download | gitea-465dc962b5e1febdfc988419d4d03e98f777019f.tar.gz gitea-465dc962b5e1febdfc988419d4d03e98f777019f.zip |
Finish create organization team
Diffstat (limited to 'models/org.go')
-rw-r--r-- | models/org.go | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/models/org.go b/models/org.go index 71b1efe9cf..2625ed428d 100644 --- a/models/org.go +++ b/models/org.go @@ -5,11 +5,17 @@ package models import ( + "errors" "strings" "github.com/gogits/gogs/modules/base" ) +var ( + ErrOrgNotExist = errors.New("Organization does not exist") + ErrTeamAlreadyExist = errors.New("Team already exist") +) + // IsOrgOwner returns true if given user is in the owner team. func (org *User) IsOrgOwner(uid int64) bool { return IsOrganizationOwner(org.Id, uid) @@ -156,6 +162,13 @@ func DeleteOrganization(org *User) (err error) { return sess.Commit() } +// ___________ +// \__ ___/___ _____ _____ +// | |_/ __ \\__ \ / \ +// | |\ ___/ / __ \| Y Y \ +// |____| \___ >____ /__|_| / +// \/ \/ \/ + type AuthorizeType int const ( @@ -192,11 +205,41 @@ func (t *Team) GetMembers() (err error) { } // NewTeam creates a record of new team. +// It's caller's responsibility to assign organization ID. func NewTeam(t *Team) error { - // TODO: check if same name team of organization exists. + has, err := x.Id(t.OrgId).Get(new(User)) + if err != nil { + return err + } else if !has { + return ErrOrgNotExist + } + t.LowerName = strings.ToLower(t.Name) - _, err := x.Insert(t) - return err + has, err = x.Where("org_id=?", t.OrgId).And("lower_name=?", t.LowerName).Get(new(Team)) + if err != nil { + return err + } else if has { + return ErrTeamAlreadyExist + } + + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + if _, err = sess.Insert(t); err != nil { + sess.Rollback() + return err + } + + // Update organization number of teams. + rawSql := "UPDATE `user` SET num_teams = num_teams + 1 WHERE id = ?" + if _, err = sess.Exec(rawSql, t.OrgId); err != nil { + sess.Rollback() + return err + } + return sess.Commit() } // UpdateTeam updates information of team. |