vikunja-api/pkg/models/teams_create.go

64 lines
1.9 KiB
Go
Raw Normal View History

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-07-14 17:34:59 +02:00
package models
import (
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/web"
)
2018-12-01 00:26:56 +01:00
2018-07-14 17:37:46 +02:00
// Create is the handler to create a team
// @Summary Creates a new team
// @Description Creates a new team in a given namespace. The user needs write-access to the namespace.
// @tags team
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param team body models.Team true "The team you want to create."
// @Success 200 {object} models.Team "The created team."
2018-12-01 02:59:17 +01:00
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid team object provided."
// @Failure 500 {object} models.Message "Internal error"
// @Router /teams [put]
2018-12-01 00:26:56 +01:00
func (t *Team) Create(a web.Auth) (err error) {
doer, err := getUserWithError(a)
if err != nil {
return err
}
2018-07-14 17:34:59 +02:00
// Check if we have a name
if t.Name == "" {
return ErrTeamNameCannotBeEmpty{}
}
t.CreatedByID = doer.ID
2018-07-14 18:29:24 +02:00
t.CreatedBy = *doer
2018-07-14 17:34:59 +02:00
_, err = x.Insert(t)
if err != nil {
return
}
2018-07-14 18:29:24 +02:00
// Insert the current user as member and admin
2018-07-26 09:53:32 +02:00
tm := TeamMember{TeamID: t.ID, UserID: doer.ID, Admin: true}
if err = tm.Create(doer); err != nil {
return err
}
metrics.UpdateCount(1, metrics.TeamCountKey)
2018-07-14 17:34:59 +02:00
return
}