You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

216 lines
5.4 KiB

package postgres
import (
"context"
"database/sql"
"github.com/Masterminds/squirrel"
"github.com/gissleh/stufflog/internal/slerrors"
"github.com/gissleh/stufflog/models"
"github.com/jmoiron/sqlx"
)
type taskRepository struct {
db *sqlx.DB
}
func (r *taskRepository) Find(ctx context.Context, id string) (*models.Task, error) {
res := models.Task{}
err := r.db.GetContext(ctx, &res, "SELECT task.*, p.icon FROM task INNER JOIN project AS p ON task.project_id = p.project_id WHERE task_id=$1", id)
if err != nil {
if err == sql.ErrNoRows {
return nil, slerrors.NotFound("Task")
}
return nil, err
}
return &res, nil
}
func (r *taskRepository) List(ctx context.Context, filter models.TaskFilter) ([]*models.Task, error) {
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 {
if *filter.Active {
sq = sq.Where(squirrel.Or{
squirrel.Eq{"task.active": true},
squirrel.Eq{"task.status_tag": []string{
"on hold", "to do",
}},
})
} else {
sq = sq.Where(squirrel.And{
squirrel.Eq{"task.active": false},
squirrel.Eq{"task.status_tag": []string{
"failed", "completed", "declined",
}},
})
}
}
if filter.Expiring != nil {
if *filter.Expiring {
sq = sq.Where("task.end_time IS NOT NULL")
} else {
sq = sq.Where("task.end_time IS NULL")
}
}
if filter.IDs != nil {
sq = sq.Where(squirrel.Eq{"task.task_id": filter.IDs})
}
if filter.ItemIDs != nil {
sq = sq.Where(squirrel.Eq{"task.item_id": filter.ItemIDs})
}
if filter.ProjectIDs != nil {
sq = sq.Where(squirrel.Or{
squirrel.Eq{"task.project_id": filter.ProjectIDs},
squirrel.Eq{"tl.project_id": filter.ProjectIDs},
})
}
sq = sq.LeftJoin("task_link AS tl ON task.task_id = tl.task_id")
sq = sq.InnerJoin("project AS p ON task.project_id = p.project_id")
sq = sq.OrderBy("active DESC", "status_tag ASC", "created_time")
query, args, err := sq.ToSql()
if err != nil {
return nil, nil, err
}
rows := make([]tasksWithLinks, 0, 8)
err = r.db.SelectContext(ctx, &rows, query, args...)
if err != nil {
if err == sql.ErrNoRows {
return []*models.Task{}, []*models.TaskLink{}, nil
}
return nil, nil, err
}
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 {
_, err := r.db.NamedExecContext(ctx, `
INSERT INTO task (
task_id, user_id, item_id, project_id, item_amount, name, description, active, created_time, end_time, status_tag
) VALUES (
:task_id, :user_id, :item_id, :project_id, :item_amount, :name, :description, :active, :created_time, :end_time, :status_tag
)
`, &task)
if err != nil {
return err
}
return nil
}
func (r *taskRepository) Update(ctx context.Context, task models.Task) error {
_, err := r.db.NamedExecContext(ctx, `
UPDATE task SET
item_id = :item_id,
item_amount = :item_amount,
name = :name,
description = :description,
active = :active,
end_time = :end_time,
status_tag = :status_tag,
project_id = :project_id
WHERE task_id=:task_id
`, &task)
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
}
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 {
if err == sql.ErrNoRows {
return slerrors.NotFound("Task")
}
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
}