api/models/teams.go

103 lines
2.6 KiB
Go
Raw Normal View History

package models
// Team holds a team object
type Team struct {
2018-07-21 13:08:46 +00:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"team"`
2018-07-14 15:34:59 +00:00
Name string `xorm:"varchar(250) not null" json:"name"`
Description string `xorm:"varchar(250)" json:"description"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
2018-07-16 06:43:47 +00:00
CreatedBy User `xorm:"-" json:"created_by"`
Members []*TeamUser `xorm:"-" json:"members"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-07-14 15:34:59 +00:00
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (Team) TableName() string {
return "teams"
}
2018-07-14 15:37:46 +00:00
// AfterLoad gets the created by user object
2018-07-14 15:34:59 +00:00
func (t *Team) AfterLoad() {
// Get the owner
2018-07-14 16:29:24 +00:00
t.CreatedBy, _, _ = GetUserByID(t.CreatedByID)
2018-07-16 06:43:47 +00:00
// Get all members
x.Select("*").
Table("users").
Join("INNER", "team_members", "team_members.user_id = users.id").
Where("team_id = ?", t.ID).
Find(&t.Members)
2018-07-14 15:34:59 +00:00
}
// TeamMember defines the relationship between a user and a team
type TeamMember struct {
2018-07-14 16:29:24 +00:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
2018-07-26 07:53:32 +00:00
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"team"`
UserID int64 `xorm:"int(11) not null" json:"user_id" param:"user"`
Admin bool `xorm:"tinyint(1)" json:"admin"`
2018-07-14 15:34:59 +00:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-07-14 16:29:24 +00:00
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (TeamMember) TableName() string {
return "team_members"
}
2018-07-16 06:45:38 +00:00
// TeamUser is the team member type
2018-07-16 06:43:47 +00:00
type TeamUser struct {
User `xorm:"extends"`
2018-07-26 07:53:32 +00:00
Admin bool `json:"admin"`
2018-07-16 06:43:47 +00:00
}
2018-07-10 12:02:23 +00:00
// GetAllTeamsByNamespaceID returns all teams for a namespace
2018-06-14 15:43:00 +00:00
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
err = x.Table("teams").
Join("INNER", "team_namespaces", "teams.id = team_id").
Where("teams.namespace_id = ?", id).
Find(teams)
return
}
2018-07-14 16:29:24 +00:00
// GetTeamByID gets a team by its ID
func GetTeamByID(id int64) (team Team, err error) {
exists, err := x.Where("id = ?", id).Get(&team)
if err != nil {
return
}
if !exists {
return team, ErrTeamDoesNotExist{id}
}
return
}
2018-07-16 06:43:47 +00:00
// ReadOne implements the CRUD method to get one team
2018-07-21 13:08:46 +00:00
func (t *Team) ReadOne() (err error) {
*t, err = GetTeamByID(t.ID)
2018-07-16 06:43:47 +00:00
return
}
// ReadAll gets all teams the user is part of
func (t *Team) ReadAll(user *User) (teams interface{}, err error) {
all := []*Team{}
err = x.Select("teams.*").
Table("teams").
Join("INNER", "team_members", "team_members.team_id = teams.id").
Where("team_members.user_id = ?", user.ID).
Find(&all)
return all, err
}