Add duplicating tasks
This commit is contained in:
parent
d10e58b37c
commit
559c2cf0d1
|
@ -49,12 +49,52 @@ func (ld *ListDuplicate) CanCreate(a web.Auth) (canCreate bool, err error) {
|
||||||
|
|
||||||
// Create duplicates a list
|
// Create duplicates a list
|
||||||
func (ld *ListDuplicate) Create(a web.Auth) (err error) {
|
func (ld *ListDuplicate) Create(a web.Auth) (err error) {
|
||||||
// Get the list
|
|
||||||
|
ld.List.ID = 0
|
||||||
|
ld.List.Identifier = "" // Reset the identifier to trigger regenerating a new one
|
||||||
|
// Set the owner to the current user
|
||||||
|
ld.List.OwnerID = a.GetID()
|
||||||
|
if err := CreateOrUpdateList(ld.List); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate kanban buckets
|
||||||
|
// Old bucket ID as key, new id as value
|
||||||
|
// Used to map the newly created tasks to their new buckets
|
||||||
|
bucketMap := make(map[int64]int64)
|
||||||
|
buckets := []*Bucket{}
|
||||||
|
err = x.Where("list_id = ?", ld.ListID).Find(&buckets)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, b := range buckets {
|
||||||
|
oldID := b.ID
|
||||||
|
b.ID = 0
|
||||||
|
b.ListID = ld.List.ID
|
||||||
|
if err := b.Create(a); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bucketMap[oldID] = b.ID
|
||||||
|
}
|
||||||
|
|
||||||
// Get all tasks + all task details
|
// Get all tasks + all task details
|
||||||
// copy stuff
|
tasks, _, _, err := getTasksForLists([]*List{{ID: ld.ListID}}, &taskOptions{})
|
||||||
// * List
|
if err != nil {
|
||||||
// * Tasks
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create + update all tasks
|
||||||
|
for _, t := range tasks {
|
||||||
|
t.ListID = ld.List.ID
|
||||||
|
t.BucketID = bucketMap[t.ID]
|
||||||
|
t.ID = 0
|
||||||
|
t.UID = ""
|
||||||
|
err := createTask(t, a, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// * Files
|
// * Files
|
||||||
// * Label Tasks (not the labels)
|
// * Label Tasks (not the labels)
|
||||||
// * assignees
|
// * assignees
|
||||||
|
@ -62,7 +102,9 @@ func (ld *ListDuplicate) Create(a web.Auth) (err error) {
|
||||||
// * comments
|
// * comments
|
||||||
// * relations in that list
|
// * relations in that list
|
||||||
// * reminders
|
// * reminders
|
||||||
|
// * Background files + unsplash info
|
||||||
|
|
||||||
// * rights / shares
|
// * rights / shares
|
||||||
// * Generate new link shares if any are available
|
// * Generate new link shares if any are available
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
43
pkg/models/list_duplicate_test.go
Normal file
43
pkg/models/list_duplicate_test.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// 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 TestListDuplicate(t *testing.T) {
|
||||||
|
|
||||||
|
db.LoadAndAssertFixtures(t)
|
||||||
|
|
||||||
|
u := &user.User{
|
||||||
|
ID: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
l := &ListDuplicate{
|
||||||
|
ListID: 1,
|
||||||
|
NamespaceID: 1,
|
||||||
|
}
|
||||||
|
can, err := l.CanCreate(u)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, can)
|
||||||
|
err = l.Create(u)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
|
@ -574,6 +574,10 @@ func checkBucketAndTaskBelongToSameList(fullTask *Task, bucketID int64) (err err
|
||||||
// @Failure 500 {object} models.Message "Internal error"
|
// @Failure 500 {object} models.Message "Internal error"
|
||||||
// @Router /lists/{id} [put]
|
// @Router /lists/{id} [put]
|
||||||
func (t *Task) Create(a web.Auth) (err error) {
|
func (t *Task) Create(a web.Auth) (err error) {
|
||||||
|
return createTask(t, a, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTask(t *Task, a web.Auth, updateAssignees bool) (err error) {
|
||||||
|
|
||||||
t.ID = 0
|
t.ID = 0
|
||||||
|
|
||||||
|
@ -637,9 +641,11 @@ func (t *Task) Create(a web.Auth) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the assignees
|
// Update the assignees
|
||||||
|
if updateAssignees {
|
||||||
if err := t.updateTaskAssignees(t.Assignees); err != nil {
|
if err := t.updateTaskAssignees(t.Assignees); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update the reminders
|
// Update the reminders
|
||||||
if err := t.updateReminders(t.Reminders); err != nil {
|
if err := t.updateReminders(t.Reminders); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user