Renamed list items to tasks
This commit is contained in:
@ -201,55 +201,55 @@ func IsErrListTitleCannotBeEmpty(err error) bool {
|
||||
}
|
||||
|
||||
func (err ErrListTitleCannotBeEmpty) Error() string {
|
||||
return fmt.Sprintf("List item text cannot be empty.")
|
||||
return fmt.Sprintf("List task text cannot be empty.")
|
||||
}
|
||||
|
||||
// ================
|
||||
// List item errors
|
||||
// List task errors
|
||||
// ================
|
||||
|
||||
// ErrListItemCannotBeEmpty represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListItemCannotBeEmpty struct{}
|
||||
// ErrListTaskCannotBeEmpty represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListTaskCannotBeEmpty struct{}
|
||||
|
||||
// IsErrListItemCannotBeEmpty checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListItemCannotBeEmpty(err error) bool {
|
||||
_, ok := err.(ErrListItemCannotBeEmpty)
|
||||
// IsErrListTaskCannotBeEmpty checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListTaskCannotBeEmpty(err error) bool {
|
||||
_, ok := err.(ErrListTaskCannotBeEmpty)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrListItemCannotBeEmpty) Error() string {
|
||||
return fmt.Sprintf("List item text cannot be empty.")
|
||||
func (err ErrListTaskCannotBeEmpty) Error() string {
|
||||
return fmt.Sprintf("List task text cannot be empty.")
|
||||
}
|
||||
|
||||
// ErrListItemDoesNotExist represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListItemDoesNotExist struct {
|
||||
// ErrListTaskDoesNotExist represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListTaskDoesNotExist struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
// IsErrListItemDoesNotExist checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListItemDoesNotExist(err error) bool {
|
||||
_, ok := err.(ErrListItemDoesNotExist)
|
||||
// IsErrListTaskDoesNotExist checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListTaskDoesNotExist(err error) bool {
|
||||
_, ok := err.(ErrListTaskDoesNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrListItemDoesNotExist) Error() string {
|
||||
return fmt.Sprintf("List item does not exist. [ID: %d]", err.ID)
|
||||
func (err ErrListTaskDoesNotExist) Error() string {
|
||||
return fmt.Sprintf("List task does not exist. [ID: %d]", err.ID)
|
||||
}
|
||||
|
||||
// ErrNeedToBeItemOwner represents an error, where the user is not the owner of that item (used i.e. when deleting a list)
|
||||
type ErrNeedToBeItemOwner struct {
|
||||
ItemID int64
|
||||
// ErrNeedToBeTaskOwner represents an error, where the user is not the owner of that task (used i.e. when deleting a list)
|
||||
type ErrNeedToBeTaskOwner struct {
|
||||
TaskID int64
|
||||
UserID int64
|
||||
}
|
||||
|
||||
// IsErrNeedToBeItemOwner checks if an error is a ErrNeedToBeItemOwner.
|
||||
func IsErrNeedToBeItemOwner(err error) bool {
|
||||
_, ok := err.(ErrNeedToBeItemOwner)
|
||||
// IsErrNeedToBeTaskOwner checks if an error is a ErrNeedToBeTaskOwner.
|
||||
func IsErrNeedToBeTaskOwner(err error) bool {
|
||||
_, ok := err.(ErrNeedToBeTaskOwner)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrNeedToBeItemOwner) Error() string {
|
||||
return fmt.Sprintf("You need to be item owner to do that [ItemID: %d, UserID: %d]", err.ItemID, err.UserID)
|
||||
func (err ErrNeedToBeTaskOwner) Error() string {
|
||||
return fmt.Sprintf("You need to be task owner to do that [TaskID: %d, UserID: %d]", err.TaskID, err.UserID)
|
||||
}
|
||||
|
||||
// =================
|
||||
|
@ -1,6 +1,6 @@
|
||||
package models
|
||||
|
||||
// List represents a list of items
|
||||
// List represents a list of tasks
|
||||
type List struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
|
||||
Title string `xorm:"varchar(250)" json:"title"`
|
||||
@ -9,7 +9,7 @@ type List struct {
|
||||
NamespaceID int64 `xorm:"int(11)" json:"-" param:"namespace"`
|
||||
|
||||
Owner User `xorm:"-" json:"owner"`
|
||||
Items []*ListItem `xorm:"-" json:"items"`
|
||||
Tasks []*ListTask `xorm:"-" json:"tasks"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
@ -18,14 +18,14 @@ type List struct {
|
||||
Rights `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// AfterLoad loads the owner and list items
|
||||
// AfterLoad loads the owner and list tasks
|
||||
func (l *List) AfterLoad() {
|
||||
|
||||
// Get the owner
|
||||
l.Owner, _, _ = GetUserByID(l.OwnerID)
|
||||
|
||||
// Get the list items
|
||||
l.Items, _ = GetItemsByListID(l.ID)
|
||||
// Get the list tasks
|
||||
l.Tasks, _ = GetTasksByListID(l.ID)
|
||||
}
|
||||
|
||||
// GetListByID returns a list by its ID
|
||||
|
@ -14,7 +14,7 @@ func (l *List) Delete() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete all todoitems on that list
|
||||
_, err = x.Where("list_id = ?", l.ID).Delete(&ListItem{})
|
||||
// Delete all todotasks on that list
|
||||
_, err = x.Where("list_id = ?", l.ID).Delete(&ListTask{})
|
||||
return
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
package models
|
||||
|
||||
// ListItem represents an item in a todolist
|
||||
type ListItem struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listitem"`
|
||||
// ListTask represents an task in a todolist
|
||||
type ListTask struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listtask"`
|
||||
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"`
|
||||
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that item on the list
|
||||
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that task on the list
|
||||
ListID int64 `xorm:"int(11)" json:"listID" param:"list"`
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
@ -19,26 +19,26 @@ type ListItem struct {
|
||||
Rights `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for listitems
|
||||
func (ListItem) TableName() string {
|
||||
return "items"
|
||||
// TableName returns the table name for listtasks
|
||||
func (ListTask) TableName() string {
|
||||
return "tasks"
|
||||
}
|
||||
|
||||
// GetItemsByListID gets all todoitems for a list
|
||||
func GetItemsByListID(listID int64) (items []*ListItem, err error) {
|
||||
err = x.Where("list_id = ?", listID).Find(&items)
|
||||
// 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
|
||||
}
|
||||
|
||||
// No need to iterate over users if the list doesn't has items
|
||||
if len(items) == 0 {
|
||||
// No need to iterate over users if the list doesn't has tasks
|
||||
if len(tasks) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Get all users and put them into the array
|
||||
var userIDs []int64
|
||||
for _, i := range items {
|
||||
for _, i := range tasks {
|
||||
found := false
|
||||
for _, u := range userIDs {
|
||||
if i.CreatedByID == u {
|
||||
@ -58,37 +58,37 @@ func GetItemsByListID(listID int64) (items []*ListItem, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
for in, item := range items {
|
||||
for in, task := range tasks {
|
||||
for _, user := range users {
|
||||
if item.CreatedByID == user.ID {
|
||||
items[in].CreatedBy = user
|
||||
if task.CreatedByID == user.ID {
|
||||
tasks[in].CreatedBy = user
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// obsfucate the user password
|
||||
items[in].CreatedBy.Password = ""
|
||||
tasks[in].CreatedBy.Password = ""
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetListItemByID returns all items a list has
|
||||
func GetListItemByID(listItemID int64) (listItem ListItem, err error) {
|
||||
exists, err := x.ID(listItemID).Get(&listItem)
|
||||
// GetListTaskByID returns all tasks a list has
|
||||
func GetListTaskByID(listTaskID int64) (listTask ListTask, err error) {
|
||||
exists, err := x.ID(listTaskID).Get(&listTask)
|
||||
if err != nil {
|
||||
return ListItem{}, err
|
||||
return ListTask{}, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return ListItem{}, ErrListItemDoesNotExist{listItemID}
|
||||
return ListTask{}, ErrListTaskDoesNotExist{listTaskID}
|
||||
}
|
||||
|
||||
user, _, err := GetUserByID(listItem.CreatedByID)
|
||||
user, _, err := GetUserByID(listTask.CreatedByID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
listItem.CreatedBy = user
|
||||
listTask.CreatedBy = user
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
package models
|
||||
|
||||
// Create is the implementation to create a list item
|
||||
func (i *ListItem) Create(doer *User) (err error) {
|
||||
// Create is the implementation to create a list task
|
||||
func (i *ListTask) Create(doer *User) (err error) {
|
||||
//i.ListID = lID
|
||||
i.ID = 0
|
||||
|
||||
return createOrUpdateListItem(i, doer)
|
||||
return createOrUpdateListTask(i, doer)
|
||||
}
|
||||
|
||||
// Update updates a list item
|
||||
func (i *ListItem) Update() (err error) {
|
||||
// Check if the item exists
|
||||
_, err = GetListItemByID(i.ID)
|
||||
// Update updates a list task
|
||||
func (i *ListTask) Update() (err error) {
|
||||
// Check if the task exists
|
||||
_, err = GetListTaskByID(i.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return createOrUpdateListItem(i, &User{})
|
||||
return createOrUpdateListTask(i, &User{})
|
||||
}
|
||||
|
||||
// Helper function for creation or updating of new lists as both methods share most of their logic
|
||||
func createOrUpdateListItem(i *ListItem, doer *User) (err error) {
|
||||
func createOrUpdateListTask(i *ListTask, doer *User) (err error) {
|
||||
|
||||
// Check if we have at least a text
|
||||
if i.Text == "" {
|
||||
return ErrListItemCannotBeEmpty{}
|
||||
return ErrListTaskCannotBeEmpty{}
|
||||
}
|
||||
|
||||
// Check if the list exists
|
||||
|
@ -1,14 +1,14 @@
|
||||
package models
|
||||
|
||||
// Delete implements the delete method for listItem
|
||||
func (i *ListItem) Delete() (err error) {
|
||||
// Delete implements the delete method for listTask
|
||||
func (i *ListTask) Delete() (err error) {
|
||||
|
||||
// Check if it exists
|
||||
_, err = GetListItemByID(i.ID)
|
||||
_, err = GetListTaskByID(i.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = x.ID(i.ID).Delete(ListItem{})
|
||||
_, err = x.ID(i.ID).Delete(ListTask{})
|
||||
return
|
||||
}
|
||||
|
@ -1,28 +1,28 @@
|
||||
package models
|
||||
|
||||
// CanDelete checks if the user can delete an item
|
||||
func (i *ListItem) CanDelete(doer *User) bool {
|
||||
// Get the item
|
||||
lI, _ := GetListItemByID(i.ID)
|
||||
// CanDelete checks if the user can delete an task
|
||||
func (i *ListTask) CanDelete(doer *User) bool {
|
||||
// Get the task
|
||||
lI, _ := GetListTaskByID(i.ID)
|
||||
|
||||
// A user can delete an item if he has write acces to its list
|
||||
// A user can delete an task if he has write acces to its list
|
||||
list, _ := GetListByID(lI.ListID)
|
||||
return list.CanWrite(doer)
|
||||
}
|
||||
|
||||
// CanUpdate determines if a user has the right to update a list item
|
||||
func (i *ListItem) CanUpdate(doer *User) bool {
|
||||
// Get the item
|
||||
lI, _ := GetListItemByID(i.ID)
|
||||
// CanUpdate determines if a user has the right to update a list task
|
||||
func (i *ListTask) CanUpdate(doer *User) bool {
|
||||
// Get the task
|
||||
lI, _ := GetListTaskByID(i.ID)
|
||||
|
||||
// A user can update an item if he has write acces to its list
|
||||
// A user can update an task if he has write acces to its list
|
||||
list, _ := GetListByID(lI.ListID)
|
||||
return list.CanWrite(doer)
|
||||
}
|
||||
|
||||
// CanCreate determines if a user has the right to create a list item
|
||||
func (i *ListItem) CanCreate(doer *User) bool {
|
||||
// A user can create an item if he has write acces to its list
|
||||
// CanCreate determines if a user has the right to create a list task
|
||||
func (i *ListTask) CanCreate(doer *User) bool {
|
||||
// A user can create an task if he has write acces to its list
|
||||
list, _ := GetListByID(i.ListID)
|
||||
return list.CanWrite(doer)
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListItem_Create(t *testing.T) {
|
||||
func TestListTask_Create(t *testing.T) {
|
||||
//assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
// Fake list item
|
||||
listitem := ListItem{
|
||||
// Fake list task
|
||||
listtask := ListTask{
|
||||
Text: "Lorem",
|
||||
Description: "Lorem Ipsum BACKERY",
|
||||
ListID: 1,
|
||||
@ -19,56 +19,56 @@ func TestListItem_Create(t *testing.T) {
|
||||
doer, _, err := GetUserByID(1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.True(t, listitem.CanCreate(&doer))
|
||||
assert.True(t, listtask.CanCreate(&doer))
|
||||
|
||||
err = listitem.Create(&doer)
|
||||
err = listtask.Create(&doer)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Update it
|
||||
listitem.Text = "Test34"
|
||||
assert.True(t, listitem.CanUpdate(&doer))
|
||||
err = listitem.Update()
|
||||
listtask.Text = "Test34"
|
||||
assert.True(t, listtask.CanUpdate(&doer))
|
||||
err = listtask.Update()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check if it was updated
|
||||
li, err := GetListItemByID(listitem.ID)
|
||||
li, err := GetListTaskByID(listtask.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, li.Text, "Test34")
|
||||
|
||||
// Delete the item
|
||||
assert.True(t, listitem.CanDelete(&doer))
|
||||
err = listitem.Delete()
|
||||
// Delete the task
|
||||
assert.True(t, listtask.CanDelete(&doer))
|
||||
err = listtask.Delete()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Delete a nonexistant item
|
||||
listitem.ID = 0
|
||||
err = listitem.Delete()
|
||||
// Delete a nonexistant task
|
||||
listtask.ID = 0
|
||||
err = listtask.Delete()
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrListItemDoesNotExist(err))
|
||||
assert.True(t, IsErrListTaskDoesNotExist(err))
|
||||
|
||||
// Try adding a list item with an empty text
|
||||
listitem.Text = ""
|
||||
err = listitem.Create(&doer)
|
||||
// Try adding a list task with an empty text
|
||||
listtask.Text = ""
|
||||
err = listtask.Create(&doer)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrListItemCannotBeEmpty(err))
|
||||
assert.True(t, IsErrListTaskCannotBeEmpty(err))
|
||||
|
||||
// Try adding one to a nonexistant list
|
||||
listitem.ListID = 99993939
|
||||
listitem.Text = "Lorem Ipsum"
|
||||
err = listitem.Create(&doer)
|
||||
listtask.ListID = 99993939
|
||||
listtask.Text = "Lorem Ipsum"
|
||||
err = listtask.Create(&doer)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrListDoesNotExist(err))
|
||||
|
||||
// Try updating a nonexistant item
|
||||
listitem.ID = 94829352
|
||||
err = listitem.Update()
|
||||
// Try updating a nonexistant task
|
||||
listtask.ID = 94829352
|
||||
err = listtask.Update()
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrListItemDoesNotExist(err))
|
||||
assert.True(t, IsErrListTaskDoesNotExist(err))
|
||||
|
||||
// Try inserting an item with a nonexistant user
|
||||
// Try inserting an task with a nonexistant user
|
||||
nUser := &User{ID: 9482385}
|
||||
listitem.ListID = 1
|
||||
err = listitem.Create(nUser)
|
||||
listtask.ListID = 1
|
||||
err = listtask.Create(nUser)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, IsErrUserDoesNotExist(err))
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func init() {
|
||||
tables = append(tables,
|
||||
new(User),
|
||||
new(List),
|
||||
new(ListItem),
|
||||
new(ListTask),
|
||||
new(Team),
|
||||
new(TeamMember),
|
||||
new(TeamList),
|
||||
|
@ -15,18 +15,18 @@ func (n *Namespace) Delete() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete all lists with their items
|
||||
// Delete all lists with their tasks
|
||||
lists, err := GetListsByNamespaceID(n.ID)
|
||||
var listIDs []int64
|
||||
// We need to do that for here because we need the list ids to delete two times:
|
||||
// 1) to delete the lists itself
|
||||
// 2) to delete the list items
|
||||
// 2) to delete the list tasks
|
||||
for _, list := range lists {
|
||||
listIDs = append(listIDs, list.ID)
|
||||
}
|
||||
|
||||
// Delete items
|
||||
_, err = x.In("list_id", listIDs).Delete(&ListItem{})
|
||||
// Delete tasks
|
||||
_, err = x.In("list_id", listIDs).Delete(&ListTask{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ const (
|
||||
const (
|
||||
// Can read lists in a Team
|
||||
TeamRightRead TeamRight = iota
|
||||
// Can write items in a Team like lists and todo items. Cannot create new lists.
|
||||
// Can write tasks in a Team like lists and todo tasks. Cannot create new lists.
|
||||
TeamRightWrite
|
||||
// Can manage a list/namespace, can do everything
|
||||
TeamRightAdmin
|
||||
|
Reference in New Issue
Block a user