package repository import ( "database/sql" "errors" "time" "github.com/daniil/homelab-api/internal/model" "github.com/jmoiron/sqlx" ) var ErrTaskNotFound = errors.New("task not found") type TaskRepository struct { db *sqlx.DB } func NewTaskRepository(db *sqlx.DB) *TaskRepository { return &TaskRepository{db: db} } func (r *TaskRepository) Create(task *model.Task) error { query := ` INSERT INTO tasks (user_id, title, description, icon, color, due_date, priority, reminder_time, is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING id, created_at, updated_at` return r.db.QueryRow(query, task.UserID, task.Title, task.Description, task.Icon, task.Color, task.DueDate, task.Priority, task.ReminderTime, task.IsRecurring, task.RecurrenceType, task.RecurrenceInterval, task.RecurrenceEndDate, task.ParentTaskID, ).Scan(&task.ID, &task.CreatedAt, &task.UpdatedAt) } func (r *TaskRepository) GetByID(id, userID int64) (*model.Task, error) { var task model.Task query := ` SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id, created_at, updated_at FROM tasks WHERE id = $1 AND user_id = $2` err := r.db.QueryRow(query, id, userID).Scan( &task.ID, &task.UserID, &task.Title, &task.Description, &task.Icon, &task.Color, &task.DueDate, &task.Priority, &task.ReminderTime, &task.CompletedAt, &task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID, &task.CreatedAt, &task.UpdatedAt, ) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, ErrTaskNotFound } return nil, err } task.ProcessForJSON() return &task, nil } func (r *TaskRepository) ListByUser(userID int64, completed *bool) ([]model.Task, error) { query := ` SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id, created_at, updated_at FROM tasks WHERE user_id = $1` if completed != nil { if *completed { query += " AND completed_at IS NOT NULL" } else { query += " AND completed_at IS NULL" } } query += " ORDER BY COALESCE(due_date, '9999-12-31'::date), priority DESC, created_at DESC" rows, err := r.db.Query(query, userID) if err != nil { return nil, err } defer rows.Close() var tasks []model.Task for rows.Next() { var task model.Task if err := rows.Scan( &task.ID, &task.UserID, &task.Title, &task.Description, &task.Icon, &task.Color, &task.DueDate, &task.Priority, &task.ReminderTime, &task.CompletedAt, &task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID, &task.CreatedAt, &task.UpdatedAt, ); err != nil { return nil, err } task.ProcessForJSON() tasks = append(tasks, task) } return tasks, nil } func (r *TaskRepository) GetTodayTasks(userID int64) ([]model.Task, error) { today := time.Now().Format("2006-01-02") query := ` SELECT id, user_id, title, description, icon, color, due_date, priority, reminder_time, completed_at, is_recurring, recurrence_type, recurrence_interval, recurrence_end_date, parent_task_id, created_at, updated_at FROM tasks WHERE user_id = $1 AND completed_at IS NULL AND due_date <= $2 ORDER BY priority DESC, due_date, created_at` rows, err := r.db.Query(query, userID, today) if err != nil { return nil, err } defer rows.Close() var tasks []model.Task for rows.Next() { var task model.Task if err := rows.Scan( &task.ID, &task.UserID, &task.Title, &task.Description, &task.Icon, &task.Color, &task.DueDate, &task.Priority, &task.ReminderTime, &task.CompletedAt, &task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID, &task.CreatedAt, &task.UpdatedAt, ); err != nil { return nil, err } task.ProcessForJSON() tasks = append(tasks, task) } return tasks, nil } func (r *TaskRepository) GetTasksWithReminder(reminderTime string, date string) ([]model.Task, error) { query := ` SELECT t.id, t.user_id, t.title, t.description, t.icon, t.color, t.due_date, t.priority, t.reminder_time, t.completed_at, t.is_recurring, t.recurrence_type, t.recurrence_interval, t.recurrence_end_date, t.parent_task_id, t.created_at, t.updated_at FROM tasks t JOIN users u ON t.user_id = u.id WHERE t.reminder_time = $1 AND t.completed_at IS NULL AND t.due_date = $2 AND u.telegram_chat_id IS NOT NULL AND u.notifications_enabled = true` rows, err := r.db.Query(query, reminderTime, date) if err != nil { return nil, err } defer rows.Close() var tasks []model.Task for rows.Next() { var task model.Task if err := rows.Scan( &task.ID, &task.UserID, &task.Title, &task.Description, &task.Icon, &task.Color, &task.DueDate, &task.Priority, &task.ReminderTime, &task.CompletedAt, &task.IsRecurring, &task.RecurrenceType, &task.RecurrenceInterval, &task.RecurrenceEndDate, &task.ParentTaskID, &task.CreatedAt, &task.UpdatedAt, ); err != nil { return nil, err } task.ProcessForJSON() tasks = append(tasks, task) } return tasks, nil } func (r *TaskRepository) Update(task *model.Task) error { query := ` UPDATE tasks SET title = $2, description = $3, icon = $4, color = $5, due_date = $6, priority = $7, reminder_time = $8, is_recurring = $9, recurrence_type = $10, recurrence_interval = $11, recurrence_end_date = $12, updated_at = CURRENT_TIMESTAMP WHERE id = $1 AND user_id = $13 RETURNING updated_at` return r.db.QueryRow(query, task.ID, task.Title, task.Description, task.Icon, task.Color, task.DueDate, task.Priority, task.ReminderTime, task.IsRecurring, task.RecurrenceType, task.RecurrenceInterval, task.RecurrenceEndDate, task.UserID, ).Scan(&task.UpdatedAt) } func (r *TaskRepository) Delete(id, userID int64) error { query := `DELETE FROM tasks WHERE id = $1 AND user_id = $2` result, err := r.db.Exec(query, id, userID) if err != nil { return err } rows, _ := result.RowsAffected() if rows == 0 { return ErrTaskNotFound } return nil } func (r *TaskRepository) Complete(id, userID int64) error { query := `UPDATE tasks SET completed_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP WHERE id = $1 AND user_id = $2 AND completed_at IS NULL` result, err := r.db.Exec(query, id, userID) if err != nil { return err } rows, _ := result.RowsAffected() if rows == 0 { return ErrTaskNotFound } return nil } func (r *TaskRepository) Uncomplete(id, userID int64) error { query := `UPDATE tasks SET completed_at = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = $1 AND user_id = $2 AND completed_at IS NOT NULL` result, err := r.db.Exec(query, id, userID) if err != nil { return err } rows, _ := result.RowsAffected() if rows == 0 { return ErrTaskNotFound } return nil }