2018-06-10 15:55:56 +02:00
|
|
|
package models
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// ListTask represents an task in a todolist
|
|
|
|
type ListTask struct {
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listtask"`
|
2018-06-10 15:55:56 +02:00
|
|
|
Text string `xorm:"varchar(250)" json:"text"`
|
|
|
|
Description string `xorm:"varchar(250)" json:"description"`
|
|
|
|
Done bool `json:"done"`
|
|
|
|
DueDateUnix int64 `xorm:"int(11)" json:"dueDate"`
|
|
|
|
ReminderUnix int64 `xorm:"int(11)" json:"reminderDate"`
|
2018-08-30 08:09:17 +02:00
|
|
|
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that task on the list
|
2018-07-21 15:08:46 +02:00
|
|
|
ListID int64 `xorm:"int(11)" json:"listID" param:"list"`
|
2018-06-10 15:55:56 +02:00
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
|
|
|
|
2018-06-10 19:49:40 +02:00
|
|
|
CreatedBy User `xorm:"-" json:"createdBy"`
|
2018-07-11 02:13:53 +02:00
|
|
|
|
|
|
|
CRUDable `xorm:"-" json:"-"`
|
|
|
|
Rights `xorm:"-" json:"-"`
|
2018-06-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// TableName returns the table name for listtasks
|
|
|
|
func (ListTask) TableName() string {
|
|
|
|
return "tasks"
|
2018-06-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// GetTasksByListID gets all todotasks for a list
|
|
|
|
func GetTasksByListID(listID int64) (tasks []*ListTask, err error) {
|
|
|
|
err = x.Where("list_id = ?", listID).Find(&tasks)
|
2018-06-10 19:49:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// No need to iterate over users if the list doesn't has tasks
|
|
|
|
if len(tasks) == 0 {
|
2018-07-09 19:49:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-10 19:49:40 +02:00
|
|
|
// Get all users and put them into the array
|
|
|
|
var userIDs []int64
|
2018-08-30 08:09:17 +02:00
|
|
|
for _, i := range tasks {
|
2018-06-10 19:49:40 +02:00
|
|
|
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 08:09:17 +02:00
|
|
|
for in, task := range tasks {
|
2018-06-10 19:49:40 +02:00
|
|
|
for _, user := range users {
|
2018-08-30 08:09:17 +02:00
|
|
|
if task.CreatedByID == user.ID {
|
|
|
|
tasks[in].CreatedBy = user
|
2018-06-10 19:49:40 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// obsfucate the user password
|
2018-08-30 08:09:17 +02:00
|
|
|
tasks[in].CreatedBy.Password = ""
|
2018-06-10 19:49:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 15:55:56 +02:00
|
|
|
return
|
|
|
|
}
|
2018-06-12 18:07:47 +02:00
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// GetListTaskByID returns all tasks a list has
|
|
|
|
func GetListTaskByID(listTaskID int64) (listTask ListTask, err error) {
|
|
|
|
exists, err := x.ID(listTaskID).Get(&listTask)
|
2018-06-12 18:35:36 +02:00
|
|
|
if err != nil {
|
2018-08-30 08:09:17 +02:00
|
|
|
return ListTask{}, err
|
2018-06-12 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-08-30 08:09:17 +02:00
|
|
|
return ListTask{}, ErrListTaskDoesNotExist{listTaskID}
|
2018-06-12 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 19:14:02 +02:00
|
|
|
user, err := GetUserByID(listTask.CreatedByID)
|
2018-06-12 19:57:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-08-30 08:09:17 +02:00
|
|
|
listTask.CreatedBy = user
|
2018-06-12 19:57:38 +02:00
|
|
|
|
2018-06-12 18:35:36 +02:00
|
|
|
return
|
|
|
|
}
|