vikunja/pkg/models/list_tasks.go

116 lines
3.1 KiB
Go
Raw Normal View History

2018-11-26 20:17:33 +00:00
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018 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/>.
2018-06-10 13:55:56 +00:00
package models
2018-08-30 06:09:17 +00:00
// ListTask represents an task in a todolist
type ListTask struct {
2018-11-21 15:03:47 +00:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listtask"`
2018-11-24 12:44:40 +00:00
Text string `xorm:"varchar(250)" json:"text" valid:"runelength(3|250)"`
2018-11-21 15:03:47 +00:00
Description string `xorm:"varchar(250)" json:"description" valid:"runelength(0|250)"`
Done bool `xorm:"INDEX" json:"done"`
DueDateUnix int64 `xorm:"int(11) INDEX" json:"dueDate"`
RemindersUnix []int64 `xorm:"JSON TEXT" json:"reminderDates"`
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that task on the list
ListID int64 `xorm:"int(11) INDEX" json:"listID" param:"list"`
2018-11-16 23:17:37 +00:00
2018-11-24 12:44:40 +00:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-06-10 13:55:56 +00:00
2018-11-25 21:38:50 +00:00
CreatedBy User `xorm:"-" json:"createdBy" valid:"-"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
2018-06-10 13:55:56 +00:00
}
2018-08-30 06:09:17 +00:00
// TableName returns the table name for listtasks
func (ListTask) TableName() string {
return "tasks"
2018-06-10 13:55:56 +00:00
}
2018-08-30 06:09:17 +00:00
// GetTasksByListID gets all todotasks for a list
func GetTasksByListID(listID int64) (tasks []*ListTask, err error) {
err = x.Where("list_id = ?", listID).Find(&tasks)
if err != nil {
return
}
2018-08-30 06:09:17 +00:00
// No need to iterate over users if the list doesn't has tasks
if len(tasks) == 0 {
2018-07-09 17:49:27 +00:00
return
}
// Get all users and put them into the array
var userIDs []int64
2018-08-30 06:09:17 +00:00
for _, i := range tasks {
found := false
for _, u := range userIDs {
if i.CreatedByID == u {
found = true
break
}
}
if !found {
userIDs = append(userIDs, i.CreatedByID)
}
}
var users []User
err = x.In("id", userIDs).Find(&users)
if err != nil {
return
}
2018-08-30 06:09:17 +00:00
for in, task := range tasks {
2018-10-31 12:42:38 +00:00
for _, u := range users {
if task.CreatedByID == u.ID {
tasks[in].CreatedBy = u
break
}
}
// obsfucate the user password
2018-08-30 06:09:17 +00:00
tasks[in].CreatedBy.Password = ""
}
2018-06-10 13:55:56 +00:00
return
}
2018-08-30 06:09:17 +00:00
// GetListTaskByID returns all tasks a list has
func GetListTaskByID(listTaskID int64) (listTask ListTask, err error) {
if listTaskID < 1 {
return ListTask{}, ErrListTaskDoesNotExist{listTaskID}
}
2018-08-30 06:09:17 +00:00
exists, err := x.ID(listTaskID).Get(&listTask)
if err != nil {
2018-08-30 06:09:17 +00:00
return ListTask{}, err
}
if !exists {
2018-08-30 06:09:17 +00:00
return ListTask{}, ErrListTaskDoesNotExist{listTaskID}
}
2018-10-31 12:42:38 +00:00
u, err := GetUserByID(listTask.CreatedByID)
2018-06-12 17:57:38 +00:00
if err != nil {
return
}
2018-10-31 12:42:38 +00:00
listTask.CreatedBy = u
2018-06-12 17:57:38 +00:00
return
}