Add Kanban bucket handling

This commit is contained in:
kolaente 2020-04-18 18:35:46 +02:00
parent 28ec44c91f
commit e938be1936
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 207 additions and 1 deletions

View File

@ -1178,7 +1178,7 @@ func IsErrInvalidRight(err error) bool {
}
func (err ErrInvalidRight) Error() string {
return fmt.Sprintf(" right invalid [Right: %d]", err.Right)
return fmt.Sprintf("Right invalid [Right: %d]", err.Right)
}
// ErrCodeInvalidRight holds the unique world-error code of this error
@ -1192,3 +1192,34 @@ func (err ErrInvalidRight) HTTPError() web.HTTPError {
Message: "The right is invalid.",
}
}
// ========
// Kanban
// ========
// ErrBucketDoesNotExist represents an error where a kanban bucket does not exist
type ErrBucketDoesNotExist struct {
BucketID int64
}
// IsErrBucketDoesNotExist checks if an error is ErrBucketDoesNotExist.
func IsErrBucketDoesNotExist(err error) bool {
_, ok := err.(ErrBucketDoesNotExist)
return ok
}
func (err ErrBucketDoesNotExist) Error() string {
return fmt.Sprintf("Bucket does not exist [BucketID: %d]", err.BucketID)
}
// ErrCodeBucketDoesNotExist holds the unique world-error code of this error
const ErrCodeBucketDoesNotExist = 10001
// HTTPError holds the http error description
func (err ErrBucketDoesNotExist) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusNotFound,
Code: ErrCodeBucketDoesNotExist,
Message: "This bucket does not exist.",
}
}

132
pkg/models/kanban.go Normal file
View File

@ -0,0 +1,132 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 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/>.
package models
import (
"code.vikunja.io/api/pkg/timeutil"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"time"
)
// Bucket represents a kanban bucket
type Bucket struct {
// The unique, numeric id of this bucket.
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"bucket"`
Title string `xorm:"text not null" valid:"runelength(1|)" minLength:"1" json:"title"`
ListID int64 `xorm:"int(11) not null" json:"list_id"`
Tasks []*Task `xorm:"-" json:"tasks"`
// A timestamp when this task was created. You cannot change this value.
Created timeutil.TimeStamp `xorm:"created not null" json:"created"`
// A timestamp when this task was last updated. You cannot change this value.
Updated timeutil.TimeStamp `xorm:"updated not null" json:"updated"`
// The user who initially created the task.
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
web.Rights `xorm:"-" json:"-"`
web.CRUDable `xorm:"-" json:"-"`
}
func getBucketByID(id int64) (b *Bucket, err error) {
b = &Bucket{}
exists, err := x.Where("id = ?", id).Get(b)
if err != nil {
return
}
if !exists {
return b, ErrBucketDoesNotExist{BucketID: id}
}
return
}
// Create creates a new bucket
func (b *Bucket) Create(a web.Auth) (err error) {
b.CreatedByID = a.GetID()
_, err = x.Insert(b)
return
}
// ReadAll returns all buckets with their tasks for a certain list
func (b *Bucket) ReadAll(auth web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
// Get all buckets for this list
buckets := []*Bucket{
{
// This is the default bucket for all tasks which are not associated to a bucket.
ID: 0,
Title: "Not associated to a bucket",
ListID: b.ListID,
Created: timeutil.FromTime(time.Now()),
Updated: timeutil.FromTime(time.Now()),
},
}
buckets[0].CreatedBy, err = user.GetFromAuth(auth)
if err != nil {
return
}
err = x.Where("list_id = ?", b.ListID).Find(&buckets)
if err != nil {
return
}
// Make a map from the bucket slice with their id as key so that we can use it to put the tasks in their buckets
bucketMap := make(map[int64]*Bucket, len(buckets))
for _, bb := range buckets {
bucketMap[bb.ID] = bb
}
// Get all tasks for this list
tasks, _, _, err := getTasksForLists([]*List{{ID: b.ListID}}, &taskOptions{})
if err != nil {
return
}
for _, task := range tasks {
bucketMap[task.BucketID].Tasks = append(bucketMap[task.BucketID].Tasks, task)
}
// Put all tasks in their buckets
// Put all tasks which are not explicitly associated to a bucket into a pseudo bucket
return buckets, len(buckets), int64(len(buckets)), nil
}
// Update Updates an existing bucket
func (b *Bucket) Update() (err error) {
_, err = x.Where("id = ?", b.ID).Update(b)
return
}
// Delete removes a bucket, but no tasks
func (b *Bucket) Delete() (err error) {
// Remove all associations of tasks to that bucket
_, err = x.Where("bucket_id = ?", b.ID).Cols("bucket_id").Update(&Task{BucketID: 0})
if err != nil {
return
}
// Remove the bucket itself
_, err = x.Where("id = ?", b.ID).Delete(&Bucket{})
return
}

View File

@ -0,0 +1,40 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 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/>.
package models
import "code.vikunja.io/web"
func (b *Bucket) CanCreate(a web.Auth) (bool, error) {
return b.canDoBucket(a)
}
func (b *Bucket) CanUpdate(a web.Auth) (bool, error) {
return b.canDoBucket(a)
}
func (b *Bucket) CanDelete(a web.Auth) (bool, error) {
return b.canDoBucket(a)
}
func (b *Bucket) canDoBucket(a web.Auth) (bool, error) {
bb, err := getBucketByID(b.ID)
if err != nil {
return false, err
}
l := &List{ID: bb.ListID}
return l.CanWrite(a)
}

View File

@ -85,6 +85,9 @@ type Task struct {
// A timestamp when this task was last updated. You cannot change this value.
Updated timeutil.TimeStamp `xorm:"updated not null" json:"updated"`
// BucketID is the ID of the kanban bucket this task belongs to.
BucketID int64 `xorm:"int(11) null" json:"-"`
// The user who initially created the task.
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`