2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-12-31 02:18:41 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-12-31 02:18:41 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-12-31 02:18:41 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-31 02:18:41 +01:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"time"
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-12-31 02:18:41 +01:00
|
|
|
"code.vikunja.io/web"
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
2018-12-31 02:18:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Label represents a label
|
|
|
|
type Label struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this label.
|
2020-12-18 17:51:22 +01:00
|
|
|
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"label"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The title of the lable. You'll see this one on tasks associated with it.
|
2020-09-26 23:02:17 +02:00
|
|
|
Title string `xorm:"varchar(250) not null" json:"title" valid:"runelength(1|250)" minLength:"1" maxLength:"250"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The label description.
|
2019-07-21 23:57:19 +02:00
|
|
|
Description string `xorm:"longtext null" json:"description"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The color this label has
|
2019-03-29 18:54:35 +01:00
|
|
|
HexColor string `xorm:"varchar(6) null" json:"hex_color" valid:"runelength(0|6)" maxLength:"6"`
|
2018-12-31 02:18:41 +01:00
|
|
|
|
2020-12-18 17:51:22 +01:00
|
|
|
CreatedByID int64 `xorm:"bigint not null" json:"-"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who created this label
|
2020-01-26 18:08:06 +01:00
|
|
|
CreatedBy *user.User `xorm:"-" json:"created_by"`
|
2018-12-31 02:18:41 +01:00
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this label was created. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time `xorm:"created not null" json:"created"`
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this label was last updated. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Updated time.Time `xorm:"updated not null" json:"updated"`
|
2018-12-31 02:18:41 +01:00
|
|
|
|
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName makes a pretty table name
|
|
|
|
func (Label) TableName() string {
|
|
|
|
return "labels"
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Create creates a new label
|
|
|
|
// @Summary Create a label
|
|
|
|
// @Description Creates a new label.
|
|
|
|
// @tags labels
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param label body models.Label true "The label object"
|
2021-05-26 21:56:31 +02:00
|
|
|
// @Success 201 {object} models.Label "The created label object."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid label object provided."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /labels [put]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) Create(s *xorm.Session, a web.Auth) (err error) {
|
2020-01-26 18:08:06 +01:00
|
|
|
u, err := user.GetFromAuth(a)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.CreatedBy = u
|
|
|
|
l.CreatedByID = u.ID
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.Insert(l)
|
2019-07-16 16:15:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a label
|
|
|
|
// @Summary Update a label
|
|
|
|
// @Description Update an existing label. The user needs to be the creator of the label to be able to do this.
|
|
|
|
// @tags labels
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Label ID"
|
|
|
|
// @Param label body models.Label true "The label object"
|
|
|
|
// @Success 200 {object} models.Label "The created label object."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid label object provided."
|
|
|
|
// @Failure 403 {object} web.HTTPError "Not allowed to update the label."
|
|
|
|
// @Failure 404 {object} web.HTTPError "Label not found."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /labels/{id} [put]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (l *Label) Update(s *xorm.Session, a web.Auth) (err error) {
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.
|
2020-06-15 11:32:07 +02:00
|
|
|
ID(l.ID).
|
|
|
|
Cols(
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"hex_color",
|
|
|
|
).
|
|
|
|
Update(l)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-02 23:48:37 +01:00
|
|
|
err = l.ReadOne(s, a)
|
2019-07-16 16:15:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a label
|
|
|
|
// @Summary Delete a label
|
|
|
|
// @Description Delete an existing label. The user needs to be the creator of the label to be able to do this.
|
|
|
|
// @tags labels
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Label ID"
|
|
|
|
// @Success 200 {object} models.Label "The label was successfully deleted."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 403 {object} web.HTTPError "Not allowed to delete the label."
|
|
|
|
// @Failure 404 {object} web.HTTPError "Label not found."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /labels/{id} [delete]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (l *Label) Delete(s *xorm.Session, a web.Auth) (err error) {
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.ID(l.ID).Delete(&Label{})
|
2019-07-16 16:15:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAll gets all labels a user can use
|
|
|
|
// @Summary Get all labels a user has access to
|
|
|
|
// @Description Returns all labels which are either created by the user or associated with a task the user has at least read-access to.
|
|
|
|
// @tags labels
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-10-23 23:11:40 +02:00
|
|
|
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Param s query string false "Search labels by label text."
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Success 200 {array} models.Label "The labels"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /labels [get]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (l *Label) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (ls interface{}, resultCount int, numberOfEntries int64, err error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
if _, is := a.(*LinkSharing); is {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, ErrGenericForbidden{}
|
2019-08-31 22:56:41 +02:00
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
2021-03-24 22:46:20 +01:00
|
|
|
u, err := user.GetUserByID(s, a.GetID())
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, err
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
return getLabelsByTaskIDs(s, &LabelByTaskIDsOptions{
|
2019-09-20 17:52:09 +02:00
|
|
|
Search: search,
|
|
|
|
User: u,
|
2021-03-24 22:46:20 +01:00
|
|
|
GetForUser: u.ID,
|
2019-10-23 23:11:40 +02:00
|
|
|
Page: page,
|
|
|
|
PerPage: perPage,
|
2019-09-20 17:52:09 +02:00
|
|
|
GetUnusedLabels: true,
|
|
|
|
GroupByLabelIDsOnly: true,
|
2019-07-16 16:15:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOne gets one label
|
|
|
|
// @Summary Gets one label
|
|
|
|
// @Description Returns one label by its ID.
|
|
|
|
// @tags labels
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "Label ID"
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Success 200 {object} models.Label "The label"
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the label"
|
|
|
|
// @Failure 404 {object} web.HTTPError "Label not found"
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /labels/{id} [get]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (l *Label) ReadOne(s *xorm.Session, a web.Auth) (err error) {
|
2020-12-23 16:32:28 +01:00
|
|
|
label, err := getLabelByIDSimple(s, l.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*l = *label
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
u, err := user.GetUserByID(s, l.CreatedByID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
l.CreatedBy = u
|
2019-07-16 16:15:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
func getLabelByIDSimple(s *xorm.Session, labelID int64) (*Label, error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
label := Label{}
|
2020-12-23 16:32:28 +01:00
|
|
|
exists, err := s.ID(labelID).Get(&label)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return &label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return &Label{}, ErrLabelDoesNotExist{labelID}
|
|
|
|
}
|
|
|
|
return &label, err
|
|
|
|
}
|