Add task identifier #115
|
@ -38,7 +38,7 @@ func init() {
|
||||||
return tx.Sync2(list20191207204427{})
|
return tx.Sync2(list20191207204427{})
|
||||||
},
|
},
|
||||||
Rollback: func(tx *xorm.Engine) error {
|
Rollback: func(tx *xorm.Engine) error {
|
||||||
return tx.DropTables(list20191207204427{})
|
return dropTableColum(tx, "list", "indentifier")
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
77
pkg/migration/20191207220736.go
Normal file
77
pkg/migration/20191207220736.go
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
// Copyright 2019 Vikunja and contriubtors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This file is part of Vikunja.
|
||||||
|
//
|
||||||
|
// Vikunja 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.
|
||||||
|
//
|
||||||
|
// Vikunja 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 Vikunja. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
|
"src.techknowlogick.com/xormigrate"
|
||||||
|
)
|
||||||
|
|
||||||
|
type task20191207220736 struct {
|
||||||
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listtask"`
|
||||||
|
Index int64 `xorm:"int(11) not null default 0" json:"index"`
|
||||||
|
ListID int64 `xorm:"int(11) INDEX not null" json:"listID" param:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (task20191207220736) TableName() string {
|
||||||
|
return "tasks"
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
migrations = append(migrations, &xormigrate.Migration{
|
||||||
|
ID: "20191207220736",
|
||||||
|
Description: "Add task index to tasks",
|
||||||
|
Migrate: func(tx *xorm.Engine) error {
|
||||||
|
err := tx.Sync2(task20191207220736{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all tasks, ordered by list and id
|
||||||
|
tasks := []*task20191207220736{}
|
||||||
|
err = tx.
|
||||||
|
OrderBy("list_id asc, id asc").
|
||||||
|
Find(&tasks)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentIndex int64 = 1
|
||||||
|
for i, task := range tasks {
|
||||||
|
// Reset the current counter if we're encountering a new list
|
||||||
|
// We can do this because the list is sorted by list id
|
||||||
|
if i > 0 && tasks[i-1].ListID != task.ListID {
|
||||||
|
currentIndex = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
task.Index = currentIndex
|
||||||
|
_, err = tx.Where("id = ?", task.ID).Update(task)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
currentIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Rollback: func(tx *xorm.Engine) error {
|
||||||
|
return dropTableColum(tx, "tasks", "index")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"code.vikunja.io/web"
|
"code.vikunja.io/web"
|
||||||
"github.com/imdario/mergo"
|
"github.com/imdario/mergo"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,6 +63,11 @@ type Task struct {
|
||||||
// Determines how far a task is left from being done
|
// Determines how far a task is left from being done
|
||||||
PercentDone float64 `xorm:"DOUBLE null" json:"percentDone"`
|
PercentDone float64 `xorm:"DOUBLE null" json:"percentDone"`
|
||||||
|
|
||||||
|
// The task identifier, based on the list identifier and the task's index
|
||||||
|
Identifier string `xorm:"-" json:"identifier"`
|
||||||
|
// The task index, calculated per list
|
||||||
|
Index int64 `xorm:"int(11) not null 0" json:"index"`
|
||||||
|
|
||||||
// The UID is currently not used for anything other than caldav, which is why we don't expose it over json
|
// The UID is currently not used for anything other than caldav, which is why we don't expose it over json
|
||||||
UID string `xorm:"varchar(250) null" json:"-"`
|
UID string `xorm:"varchar(250) null" json:"-"`
|
||||||
|
|
||||||
|
@ -230,19 +236,6 @@ func getTasksForLists(lists []*List, opts *taskOptions) (tasks []*Task, resultCo
|
||||||
return tasks, resultCount, totalItems, err
|
return tasks, resultCount, totalItems, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTasksByListID gets all todotasks for a list
|
|
||||||
func GetTasksByListID(listID int64) (tasks []*Task, err error) {
|
|
||||||
// make a map so we can put in a lot of other stuff more easily
|
|
||||||
taskMap := make(map[int64]*Task, len(tasks))
|
|
||||||
err = x.Where("list_id = ?", listID).Find(&taskMap)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks, err = addMoreInfoToTasks(taskMap)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTaskByIDSimple returns a raw task without extra data by the task ID
|
// GetTaskByIDSimple returns a raw task without extra data by the task ID
|
||||||
func GetTaskByIDSimple(taskID int64) (task Task, err error) {
|
func GetTaskByIDSimple(taskID int64) (task Task, err error) {
|
||||||
if taskID < 1 {
|
if taskID < 1 {
|
||||||
|
@ -314,9 +307,11 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
|
||||||
// Get all users & task ids and put them into the array
|
// Get all users & task ids and put them into the array
|
||||||
var userIDs []int64
|
var userIDs []int64
|
||||||
var taskIDs []int64
|
var taskIDs []int64
|
||||||
|
var listIDs []int64
|
||||||
for _, i := range taskMap {
|
for _, i := range taskMap {
|
||||||
taskIDs = append(taskIDs, i.ID)
|
taskIDs = append(taskIDs, i.ID)
|
||||||
userIDs = append(userIDs, i.CreatedByID)
|
userIDs = append(userIDs, i.CreatedByID)
|
||||||
|
listIDs = append(listIDs, i.ListID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all assignees
|
// Get all assignees
|
||||||
|
@ -399,6 +394,13 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
|
||||||
taskRemindersUnix[r.TaskID] = append(taskRemindersUnix[r.TaskID], r.ReminderUnix)
|
taskRemindersUnix[r.TaskID] = append(taskRemindersUnix[r.TaskID], r.ReminderUnix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get all identifiers
|
||||||
|
lists := make(map[int64]*List, len(listIDs))
|
||||||
|
err = x.In("id", listIDs).Find(&lists)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Add all user objects to the appropriate tasks
|
// Add all user objects to the appropriate tasks
|
||||||
for _, task := range taskMap {
|
for _, task := range taskMap {
|
||||||
|
|
||||||
|
@ -410,6 +412,9 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (tasks []*Task, err error) {
|
||||||
|
|
||||||
// Prepare the subtasks
|
// Prepare the subtasks
|
||||||
task.RelatedTasks = make(RelatedTaskMap)
|
task.RelatedTasks = make(RelatedTaskMap)
|
||||||
|
|
||||||
|
// Build the task identifier from the list identifier and task index
|
||||||
|
task.Identifier = lists[task.ListID].Identifier + strconv.FormatInt(task.Index, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all related tasks
|
// Get all related tasks
|
||||||
|
|
Loading…
Reference in New Issue
Block a user