Class: TmsTaskManager::Services::TaskList

Inherits:
Object
  • Object
show all
Extended by:
Helpers::ErrorHandler
Defined in:
lib/tms_task_manager/services/task_list.rb

Class Method Summary collapse

Methods included from Helpers::ErrorHandler

handle_errors

Class Method Details

.create(title, description, start_date, end_date, status = 'pending') ⇒ Object

Adds a new task to the database



14
15
16
17
18
19
20
21
22
23
# File 'lib/tms_task_manager/services/task_list.rb', line 14

def self.create(title, description, start_date, end_date, status = 'pending')
  handle_errors do
    raise 'Status must be "pending", "in_progress" or "completed"' unless %w[pending in_progress completed].include?(status.downcase)

    @db.execute('INSERT INTO tasks (title, description, start_date, end_date, status) VALUES (?, ?, ?, ?, ?)',
                [title, description, start_date, end_date, status])
    puts "Task created successfully: #{title}"
    { success: true, message: "Task created: #{title}" }
  end
end

.delete_allObject

Deletes all tasks



48
49
50
51
52
53
54
# File 'lib/tms_task_manager/services/task_list.rb', line 48

def self.delete_all
  handle_errors do
    @db.execute('DELETE FROM tasks')
    puts "All tasks have been deleted."
    { success: true, message: 'All tasks deleted' }
  end
end

.get(id) ⇒ Object

Gets a task from the database



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tms_task_manager/services/task_list.rb', line 57

def self.get(id)
  handle_errors do
    task = @db.execute('SELECT * FROM tasks WHERE id = ?', [id]).first
    if task.nil?
      puts "Task not found: #{id}"
      { success: false, message: 'Task not found' }
    else
      puts "Task details: #{task}"
      task
    end
  end
end

.listObject

Lists all tasks in the database



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tms_task_manager/services/task_list.rb', line 26

def self.list
  handle_errors do
    tasks = @db.execute('SELECT id, title, status, start_date, end_date FROM tasks')
    if tasks.empty?
      puts "No tasks found."
    else
      puts "Listing all tasks: #{tasks}"
    end
    tasks.empty? ? [] : tasks
  end
end

.remove(id) ⇒ Object

Removes a task from the database



39
40
41
42
43
44
45
# File 'lib/tms_task_manager/services/task_list.rb', line 39

def self.remove(id)
  handle_errors do
    @db.execute('DELETE FROM tasks WHERE id = ?', [id])
    puts "Task removed successfully: #{id}"
    { success: true, message: "Task removed: #{id}" }
  end
end