vikunja-api/pkg/models/teams.go

121 lines
3.4 KiB
Go
Raw Normal View History

package models
// Team holds a team object
type Team struct {
2018-07-21 15:08:46 +02:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"team"`
2018-11-17 00:17:37 +01:00
Name string `xorm:"varchar(250) not null" json:"name" valid:"required,runelength(5|250)"`
Description string `xorm:"varchar(250)" json:"description" valid:"runelength(0|250)"`
2018-10-05 18:46:01 +02:00
CreatedByID int64 `xorm:"int(11) not null INDEX" json:"-"`
2018-07-14 17:34:59 +02:00
2018-07-16 08:43:47 +02:00
CreatedBy User `xorm:"-" json:"created_by"`
Members []*TeamUser `xorm:"-" json:"members"`
2018-11-24 13:44:40 +01:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-07-14 17:34:59 +02:00
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (Team) TableName() string {
return "teams"
}
2018-07-14 17:37:46 +02:00
// AfterLoad gets the created by user object
2018-07-14 17:34:59 +02:00
func (t *Team) AfterLoad() {
// Get the owner
2018-08-30 19:14:02 +02:00
t.CreatedBy, _ = GetUserByID(t.CreatedByID)
2018-07-16 08:43:47 +02: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 17:34:59 +02:00
}
// TeamMember defines the relationship between a user and a team
type TeamMember struct {
2018-07-26 10:08:01 +02:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
2018-10-05 18:46:01 +02:00
TeamID int64 `xorm:"int(11) not null INDEX" json:"team_id" param:"team"`
UserID int64 `xorm:"int(11) not null INDEX" json:"user_id" param:"user"`
Admin bool `xorm:"tinyint(1) INDEX" json:"admin"`
2018-11-24 13:44:40 +01:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-07-14 18:29:24 +02:00
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (TeamMember) TableName() string {
return "team_members"
}
2018-07-16 08:45:38 +02:00
// TeamUser is the team member type
2018-07-16 08:43:47 +02:00
type TeamUser struct {
2018-07-26 10:08:01 +02:00
User `xorm:"extends"`
2018-07-26 09:53:32 +02:00
Admin bool `json:"admin"`
2018-07-16 08:43:47 +02:00
}
2018-07-14 18:29:24 +02:00
// GetTeamByID gets a team by its ID
func GetTeamByID(id int64) (team Team, err error) {
if id < 1 {
return team, ErrTeamDoesNotExist{id}
}
2018-07-14 18:29:24 +02:00
exists, err := x.Where("id = ?", id).Get(&team)
if err != nil {
return
}
if !exists {
return team, ErrTeamDoesNotExist{id}
}
return
}
2018-07-16 08:43:47 +02:00
// ReadOne implements the CRUD method to get one team
// @Summary Gets one team
// @Description Returns a team by its ID.
// @tags team
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "Team ID"
// @Success 200 {object} models.Team "The team"
// @Failure 403 {object} models.HTTPError "The user does not have access to the team"
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id} [get]
2018-07-21 15:08:46 +02:00
func (t *Team) ReadOne() (err error) {
*t, err = GetTeamByID(t.ID)
2018-07-16 08:43:47 +02:00
return
}
// ReadAll gets all teams the user is part of
// @Summary Get teams
// @Description Returns all teams the current user is part of.
// @tags team
// @Accept json
// @Produce json
// @Param p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param s query string false "Search teams by its name."
// @Security ApiKeyAuth
// @Success 200 {array} models.Team "The teams."
// @Failure 500 {object} models.Message "Internal error"
// @Router /teams [get]
func (t *Team) ReadAll(search string, user *User, page int) (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).
2018-11-09 11:30:17 +01:00
Limit(getLimitFromPageIndex(page)).
Where("teams.name LIKE ?", "%"+search+"%").
Find(&all)
return all, err
}