Fixed task update function to be able to update a task with minimal informaton

This commit is contained in:
kolaente 2018-09-10 07:41:39 +02:00
parent b16c88a6f3
commit b91d9eb712
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -2,26 +2,8 @@ package models
// Create is the implementation to create a list task // Create is the implementation to create a list task
func (i *ListTask) Create(doer *User) (err error) { func (i *ListTask) Create(doer *User) (err error) {
//i.ListID = lID
i.ID = 0 i.ID = 0
return createOrUpdateListTask(i, doer)
}
// 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 createOrUpdateListTask(i, &User{})
}
// Helper function for creation or updating of new lists as both methods share most of their logic
func createOrUpdateListTask(i *ListTask, doer *User) (err error) {
// Check if we have at least a text // Check if we have at least a text
if i.Text == "" { if i.Text == "" {
return ErrListTaskCannotBeEmpty{} return ErrListTaskCannotBeEmpty{}
@ -33,19 +15,26 @@ func createOrUpdateListTask(i *ListTask, doer *User) (err error) {
return return
} }
// Do the update user, err := GetUserByID(doer.ID)
if i.ID != 0 { if err != nil {
_, err = x.ID(i.ID).Update(i) return err
} else {
user, err := GetUserByID(doer.ID)
if err != nil {
return err
}
i.CreatedByID = user.ID
i.CreatedBy = user
_, err = x.Insert(i)
} }
return i.CreatedByID = user.ID
i.CreatedBy = user
_, err = x.Insert(i)
return err
}
// Update updates a list task
func (i *ListTask) Update() (err error) {
// Check if the task exists
_, err = GetListTaskByID(i.ID)
if err != nil {
return
}
// Do the update
_, err = x.ID(i.ID).Update(i)
return err
} }