This commit is contained in:
@@ -105,5 +105,10 @@ func (r *projectRepository) Delete(ctx context.Context, project models.Project)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE project_id=$1`, project.ID)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -28,7 +28,17 @@ func (r *taskRepository) Find(ctx context.Context, id string) (*models.Task, err
|
||||
}
|
||||
|
||||
func (r *taskRepository) List(ctx context.Context, filter models.TaskFilter) ([]*models.Task, error) {
|
||||
sq := squirrel.Select("task.*", "p.icon").From("task").PlaceholderFormat(squirrel.Dollar)
|
||||
tasks, _, err := r.ListWithLinks(ctx, filter)
|
||||
return tasks, err
|
||||
}
|
||||
|
||||
func (r *taskRepository) ListWithLinks(ctx context.Context, filter models.TaskFilter) ([]*models.Task, []*models.TaskLink, error) {
|
||||
type tasksWithLinks struct {
|
||||
models.Task
|
||||
LinkedProjectID *string `db:"tl_project_id"`
|
||||
}
|
||||
|
||||
sq := squirrel.Select("task.*", "tl.project_id as tl_project_id", "p.icon").From("task").PlaceholderFormat(squirrel.Dollar)
|
||||
sq = sq.Where(squirrel.Eq{"task.user_id": filter.UserID})
|
||||
if filter.Active != nil {
|
||||
sq = sq.Where(squirrel.Eq{"task.active": *filter.Active})
|
||||
@@ -47,7 +57,12 @@ func (r *taskRepository) List(ctx context.Context, filter models.TaskFilter) ([]
|
||||
sq = sq.Where(squirrel.Eq{"task.item_id": filter.ItemIDs})
|
||||
}
|
||||
if filter.ProjectIDs != nil {
|
||||
sq = sq.Where(squirrel.Eq{"task.project_id": filter.ProjectIDs})
|
||||
sq = sq.LeftJoin("task_link AS tl ON task.task_id = tl.task_id")
|
||||
|
||||
sq = sq.Where(squirrel.Or{
|
||||
squirrel.Eq{"task.project_id": filter.ProjectIDs},
|
||||
squirrel.Eq{"tl.project_id": filter.ProjectIDs},
|
||||
})
|
||||
}
|
||||
|
||||
sq = sq.InnerJoin("project AS p ON task.project_id = p.project_id")
|
||||
@@ -55,19 +70,37 @@ func (r *taskRepository) List(ctx context.Context, filter models.TaskFilter) ([]
|
||||
|
||||
query, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
res := make([]*models.Task, 0, 8)
|
||||
err = r.db.SelectContext(ctx, &res, query, args...)
|
||||
rows := make([]tasksWithLinks, 0, 8)
|
||||
err = r.db.SelectContext(ctx, &rows, query, args...)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return res, nil
|
||||
return []*models.Task{}, []*models.TaskLink{}, nil
|
||||
}
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
added := make(map[string]bool)
|
||||
tasks := make([]*models.Task, 0, len(rows))
|
||||
links := make([]*models.TaskLink, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if row.LinkedProjectID != nil {
|
||||
links = append(links, &models.TaskLink{
|
||||
TaskID: row.Task.ID,
|
||||
ProjectID: *row.LinkedProjectID,
|
||||
})
|
||||
}
|
||||
|
||||
if !added[row.Task.ID] {
|
||||
task := row.Task
|
||||
tasks = append(tasks, &task)
|
||||
added[row.Task.ID] = true
|
||||
}
|
||||
}
|
||||
|
||||
return tasks, links, nil
|
||||
}
|
||||
|
||||
func (r *taskRepository) Insert(ctx context.Context, task models.Task) error {
|
||||
@@ -101,7 +134,7 @@ func (r *taskRepository) Update(ctx context.Context, task models.Task) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
_, err = r.db.NamedExecContext(ctx, `UPDATE log SET item_id = :item_id WHERE task_id=:task_id`, &task)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -110,6 +143,48 @@ func (r *taskRepository) Update(ctx context.Context, task models.Task) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *taskRepository) CreateLink(ctx context.Context, link models.TaskLink) error {
|
||||
_, err := r.db.NamedExecContext(ctx, `
|
||||
INSERT INTO task_link (project_id, task_id) VALUES (:project_id, :task_id)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, &link)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *taskRepository) DeleteLink(ctx context.Context, link models.TaskLink) error {
|
||||
_, err := r.db.NamedExecContext(ctx, `
|
||||
DELETE FROM task_link WHERE task_id=:task_id AND project_id=:project_id
|
||||
`, &link)
|
||||
if err == sql.ErrNoRows {
|
||||
err = slerrors.NotFound("Link")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *taskRepository) UnlinkTask(ctx context.Context, task models.Task) error {
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
DELETE FROM task_link WHERE task_id=$1;
|
||||
`, task.ID)
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *taskRepository) UnlinkProject(ctx context.Context, project models.Project) error {
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
DELETE FROM task_link WHERE task_id=$1;
|
||||
`, project.ID)
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *taskRepository) Delete(ctx context.Context, task models.Task) error {
|
||||
_, err := r.db.ExecContext(ctx, `DELETE FROM task WHERE task_id=$1`, task.ID)
|
||||
if err != nil {
|
||||
@@ -119,5 +194,10 @@ func (r *taskRepository) Delete(ctx context.Context, task models.Task) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE task_id=$1`, task.ID)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user