1
0
Fix tests

Add error docs

Add swagger docs for bucket endpoints

Add integration tests

Fix tests

Fix err shadow

Make sure a bucket and a task belong to the same list when adding or updating a task

Add tests

Add getting users of a bucket

Fix log level when testing

Fix lint

Add migration for buckets

Cleanup/Comments/Reorganization

Add Kanban bucket handling

Co-authored-by: kolaente <k@knt.li>
Reviewed-on: https://kolaente.dev/vikunja/api/pulls/393
This commit is contained in:
konrad
2020-04-19 07:27:28 +00:00
parent 28ec44c91f
commit ecdecdd94e
20 changed files with 992 additions and 19 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,62 @@ 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.",
}
}
// ErrBucketDoesNotBelongToList represents an error where a kanban bucket does not belong to a list
type ErrBucketDoesNotBelongToList struct {
BucketID int64
ListID int64
}
// IsErrBucketDoesNotBelongToList checks if an error is ErrBucketDoesNotBelongToList.
func IsErrBucketDoesNotBelongToList(err error) bool {
_, ok := err.(ErrBucketDoesNotBelongToList)
return ok
}
func (err ErrBucketDoesNotBelongToList) Error() string {
return fmt.Sprintf("Bucket does not not belong to list [BucketID: %d, ListID: %d]", err.BucketID, err.ListID)
}
// ErrCodeBucketDoesNotBelongToList holds the unique world-error code of this error
const ErrCodeBucketDoesNotBelongToList = 10002
// HTTPError holds the http error description
func (err ErrBucketDoesNotBelongToList) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeBucketDoesNotBelongToList,
Message: "This bucket does not belong to that list.",
}
}

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

@ -0,0 +1,205 @@
// 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"`
// The title of this bucket.
Title string `xorm:"text not null" valid:"required" minLength:"1" json:"title"`
// The list this bucket belongs to.
ListID int64 `xorm:"int(11) not null" json:"list_id" param:"list"`
// All tasks which belong to this bucket.
Tasks []*Task `xorm:"-" json:"tasks"`
// A timestamp when this bucket was created. You cannot change this value.
Created timeutil.TimeStamp `xorm:"created not null" json:"created"`
// A timestamp when this bucket was last updated. You cannot change this value.
Updated timeutil.TimeStamp `xorm:"updated not null" json:"updated"`
// The user who initially created the bucket.
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
web.Rights `xorm:"-" json:"-"`
web.CRUDable `xorm:"-" json:"-"`
}
// TableName returns the table name for this bucket.
func (b *Bucket) TableName() string {
return "buckets"
}
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
}
// ReadAll returns all buckets with their tasks for a certain list
// @Summary Get all kanban buckets of a list
// @Description Returns all kanban buckets with belong to a list including their tasks.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "List Id"
// @Success 200 {array} models.Bucket "The buckets with their tasks"
// @Failure 500 {object} models.Message "Internal server error"
// @Router /lists/{id}/buckets [get]
func (b *Bucket) ReadAll(auth web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
// Note: I'm ignoring pagination for now since I've yet to figure out a way on how to make it work
// I'll probably just don't do it and instead make individual tasks archivable.
// 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()),
CreatedByID: auth.GetID(),
},
}
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))
userIDs := make([]int64, 0, len(buckets))
for _, bb := range buckets {
bucketMap[bb.ID] = bb
userIDs = append(userIDs, bb.CreatedByID)
}
// Get all users
users := make(map[int64]*user.User)
err = x.In("id", userIDs).Find(&users)
if err != nil {
return
}
for _, bb := range buckets {
bb.CreatedBy = users[bb.CreatedByID]
}
// Get all tasks for this list
tasks, _, _, err := getTasksForLists([]*List{{ID: b.ListID}}, &taskOptions{})
if err != nil {
return
}
// Put all tasks in their buckets
// All tasks which are not associated to any bucket will have bucket id 0 which is the nil value for int64
// Since we created a bucked with that id at the beginning, all tasks should be in there.
for _, task := range tasks {
bucketMap[task.BucketID].Tasks = append(bucketMap[task.BucketID].Tasks, task)
}
return buckets, len(buckets), int64(len(buckets)), nil
}
// Create creates a new bucket
// @Summary Create a new bucket
// @Description Creates a new kanban bucket on a list.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "List Id"
// @Param bucket body models.Bucket true "The bucket object"
// @Success 200 {object} models.Bucket "The created bucket object."
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid bucket object provided."
// @Failure 404 {object} code.vikunja.io/web.HTTPError "The list does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id}/buckets [put]
func (b *Bucket) Create(a web.Auth) (err error) {
b.CreatedByID = a.GetID()
_, err = x.Insert(b)
return
}
// Update Updates an existing bucket
// @Summary Update an existing bucket
// @Description Updates an existing kanban bucket.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param listID path int true "List Id"
// @Param bucketID path int true "Bucket Id"
// @Param bucket body models.Bucket true "The bucket object"
// @Success 200 {object} models.Bucket "The created bucket object."
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid bucket object provided."
// @Failure 404 {object} code.vikunja.io/web.HTTPError "The bucket does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{listID}/buckets/{bucketID} [post]
func (b *Bucket) Update() (err error) {
_, err = x.Where("id = ?", b.ID).Update(b)
return
}
// Delete removes a bucket, but no tasks
// @Summary Deletes an existing bucket
// @Description Deletes an existing kanban bucket and dissociates all of its task. It does not delete any tasks.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param listID path int true "List Id"
// @Param bucketID path int true "Bucket Id"
// @Success 200 {object} models.Message "Successfully deleted."
// @Failure 404 {object} code.vikunja.io/web.HTTPError "The bucket does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{listID}/buckets/{bucketID} [delete]
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,45 @@
// 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"
// CanCreate checks if a user can create a new bucket
func (b *Bucket) CanCreate(a web.Auth) (bool, error) {
l := &List{ID: b.ListID}
return l.CanWrite(a)
}
// CanUpdate checks if a user can update an existing bucket
func (b *Bucket) CanUpdate(a web.Auth) (bool, error) {
return b.canDoBucket(a)
}
// CanDelete checks if a user can delete an existing bucket
func (b *Bucket) CanDelete(a web.Auth) (bool, error) {
return b.canDoBucket(a)
}
// canDoBucket checks if the bucket exists and if the user has the right to act on it
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)
}

67
pkg/models/kanban_test.go Normal file
View File

@ -0,0 +1,67 @@
// 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/db"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"testing"
)
func TestBucket_ReadAll(t *testing.T) {
db.LoadAndAssertFixtures(t)
testuser := &user.User{ID: 1}
b := &Bucket{ListID: 1}
bucketsInterface, _, _, err := b.ReadAll(testuser, "", 0, 0)
assert.NoError(t, err)
buckets, is := bucketsInterface.([]*Bucket)
assert.True(t, is)
// Assert that we have a user for each bucket
assert.Equal(t, testuser.ID, buckets[0].CreatedBy.ID)
assert.Equal(t, testuser.ID, buckets[1].CreatedBy.ID)
assert.Equal(t, testuser.ID, buckets[2].CreatedBy.ID)
assert.Equal(t, testuser.ID, buckets[3].CreatedBy.ID)
// Assert our three test buckets + one for all tasks without a bucket
assert.Len(t, buckets, 4)
// Assert all tasks are in the right bucket
assert.Len(t, buckets[0].Tasks, 10)
assert.Len(t, buckets[1].Tasks, 2)
assert.Len(t, buckets[2].Tasks, 3)
assert.Len(t, buckets[3].Tasks, 3)
// Assert we have bucket 0, 1, 2, 3 but not 4 (that belongs to a different list)
assert.Equal(t, int64(1), buckets[1].ID)
assert.Equal(t, int64(2), buckets[2].ID)
assert.Equal(t, int64(3), buckets[3].ID)
// Kinda assert all tasks are in the right buckets
assert.Equal(t, int64(0), buckets[0].Tasks[0].BucketID)
assert.Equal(t, int64(1), buckets[1].Tasks[0].BucketID)
assert.Equal(t, int64(1), buckets[1].Tasks[1].BucketID)
assert.Equal(t, int64(2), buckets[2].Tasks[0].BucketID)
assert.Equal(t, int64(2), buckets[2].Tasks[1].BucketID)
assert.Equal(t, int64(2), buckets[2].Tasks[2].BucketID)
assert.Equal(t, int64(3), buckets[3].Tasks[0].BucketID)
assert.Equal(t, int64(3), buckets[3].Tasks[1].BucketID)
assert.Equal(t, int64(3), buckets[3].Tasks[2].BucketID)
}

View File

@ -51,6 +51,7 @@ func GetTables() []interface{} {
&TaskRelation{},
&TaskAttachment{},
&TaskComment{},
&Bucket{},
}
}

View File

@ -56,6 +56,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
BucketID: 1,
Labels: []*Label{
{
ID: 4,
@ -114,6 +115,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
BucketID: 1,
Labels: []*Label{
{
ID: 4,
@ -140,6 +142,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
Priority: 100,
BucketID: 2,
}
task4 := &Task{
ID: 4,
@ -153,6 +156,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
Priority: 1,
BucketID: 2,
}
task5 := &Task{
ID: 5,
@ -166,6 +170,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
DueDate: 1543636724,
BucketID: 2,
}
task6 := &Task{
ID: 6,
@ -179,6 +184,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
DueDate: 1543616724,
BucketID: 3,
}
task7 := &Task{
ID: 7,
@ -192,6 +198,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
StartDate: 1544600000,
BucketID: 3,
}
task8 := &Task{
ID: 8,
@ -205,6 +212,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
Created: 1543626724,
Updated: 1543626724,
EndDate: 1544700000,
BucketID: 3,
}
task9 := &Task{
ID: 9,
@ -445,6 +453,7 @@ func TestTaskCollection_ReadAll(t *testing.T) {
ListID: 1,
Created: 1543626724,
Updated: 1543626724,
BucketID: 1,
},
},
},

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:"bucket_id"`
// The user who initially created the task.
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`
@ -459,6 +462,22 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
return
}
func checkBucketAndTaskBelongToSameList(fullTask *Task, bucketID int64) (err error) {
if bucketID != 0 {
b, err := getBucketByID(bucketID)
if err != nil {
return err
}
if fullTask.ListID != b.ListID {
return ErrBucketDoesNotBelongToList{
ListID: fullTask.ListID,
BucketID: fullTask.BucketID,
}
}
}
return
}
// Create is the implementation to create a list task
// @Summary Create a task
// @Description Inserts a task into a list.
@ -498,6 +517,12 @@ func (t *Task) Create(a web.Auth) (err error) {
t.UID = utils.MakeRandomString(40)
}
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList(t, t.BucketID)
if err != nil {
return
}
// Get the index for this task
latestTask := &Task{}
_, err = x.Where("list_id = ?", t.ListID).OrderBy("id desc").Get(latestTask)
@ -573,6 +598,12 @@ func (t *Task) Update() (err error) {
return err
}
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList(&ot, t.BucketID)
if err != nil {
return
}
// Update the labels
//
// Maybe FIXME:

View File

@ -56,7 +56,9 @@ func SetupTests() {
"teams",
"users",
"users_list",
"users_namespace")
"users_namespace",
"buckets",
)
if err != nil {
log.Fatal(err)
}