Fix task relations not getting properly cleaned up when deleting them

This commit is contained in:
kolaente 2021-07-19 23:40:18 +02:00
parent 0b90d826be
commit d28390d792
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -19,6 +19,7 @@ package models
import ( import (
"time" "time"
"xorm.io/builder"
"xorm.io/xorm" "xorm.io/xorm"
"code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/user"
@ -208,9 +209,23 @@ func (rel *TaskRelation) Create(s *xorm.Session, a web.Auth) error {
// @Failure 500 {object} models.Message "Internal error" // @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{taskID}/relations/{relationKind}/{otherTaskId} [delete] // @Router /tasks/{taskID}/relations/{relationKind}/{otherTaskId} [delete]
func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error { func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error {
cond := builder.Or(
builder.And(
builder.Eq{"task_id": rel.TaskID},
builder.Eq{"other_task_id": rel.OtherTaskID},
builder.Eq{"relation_kind": rel.RelationKind},
),
builder.And(
builder.Eq{"task_id": rel.OtherTaskID},
builder.Eq{"other_task_id": rel.TaskID},
builder.Eq{"relation_kind": rel.RelationKind},
),
)
// Check if the relation exists // Check if the relation exists
exists, err := s. exists, err := s.
Where("task_id = ? AND other_task_id = ? AND relation_kind = ?", rel.TaskID, rel.OtherTaskID, rel.RelationKind). Where(cond).
Exist(&TaskRelation{}) Exist(&TaskRelation{})
if err != nil { if err != nil {
return err return err
@ -223,6 +238,8 @@ func (rel *TaskRelation) Delete(s *xorm.Session, a web.Auth) error {
} }
} }
_, err = s.Delete(rel) _, err = s.
Where(cond).
Delete(&TaskRelation{})
return err return err
} }