2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
import "code.vikunja.io/web"
|
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// Team holds a team object
|
|
|
|
type Team struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this team.
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"team"`
|
|
|
|
// The name of this team.
|
|
|
|
Name string `xorm:"varchar(250) not null" json:"name" valid:"required,runelength(5|250)" minLength:"5" maxLength:"250"`
|
|
|
|
// The team's description.
|
|
|
|
Description string `xorm:"varchar(250)" json:"description" valid:"runelength(0|250)" minLength:"0" maxLength:"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
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who created this team.
|
2019-01-14 23:32:56 +01:00
|
|
|
CreatedBy User `xorm:"-" json:"createdBy"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// An array of all members in this team.
|
|
|
|
Members []*TeamUser `xorm:"-" json:"members"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// A unix timestamp when this relation was created. You cannot change this value.
|
2018-11-24 13:44:40 +01:00
|
|
|
Created int64 `xorm:"created" json:"created"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// A unix timestamp when this relation was last updated. You cannot change this value.
|
2018-11-24 13:44:40 +01:00
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// TeamMember defines the relationship between a user and a team
|
|
|
|
type TeamMember struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this team member relation.
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
|
|
|
// The team id.
|
2019-01-14 23:32:56 +01:00
|
|
|
TeamID int64 `xorm:"int(11) not null INDEX" json:"-" param:"team"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The id of the member.
|
2019-01-14 23:32:56 +01:00
|
|
|
UserID int64 `xorm:"int(11) not null INDEX" json:"userID" param:"user"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// Whether or not the member is an admin of the team. See the docs for more about what a team admin can do
|
|
|
|
Admin bool `xorm:"tinyint(1) INDEX" json:"admin"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// A unix timestamp when this relation was created. You cannot change this value.
|
2018-11-24 13:44:40 +01:00
|
|
|
Created int64 `xorm:"created" json:"created"`
|
2018-07-14 18:29:24 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-01-03 23:22:06 +01:00
|
|
|
User `xorm:"extends"`
|
|
|
|
// Whether or not the member is an admin of the team. See the docs for more about what a team admin can do
|
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) {
|
2018-09-13 20:07:11 +02:00
|
|
|
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
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Gets one team
|
|
|
|
// @Description Returns a team by its ID.
|
|
|
|
// @tags team
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param id path int true "Team ID"
|
|
|
|
// @Success 200 {object} models.Team "The team"
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the team"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @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
|
|
|
|
}
|
2018-07-16 08:52:16 +02:00
|
|
|
|
|
|
|
// ReadAll gets all teams the user is part of
|
2018-11-12 16:46:35 +01:00
|
|
|
// @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."
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Success 200 {array} models.Team "The teams."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /teams [get]
|
2018-12-01 00:26:56 +01:00
|
|
|
func (t *Team) ReadAll(search string, a web.Auth, page int) (interface{}, error) {
|
|
|
|
user, err := getUserWithError(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-16 08:52:16 +02:00
|
|
|
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)).
|
2018-11-09 18:33:06 +01:00
|
|
|
Where("teams.name LIKE ?", "%"+search+"%").
|
2018-07-16 08:52:16 +02:00
|
|
|
Find(&all)
|
|
|
|
|
|
|
|
return all, err
|
|
|
|
}
|