Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// The tests were developed before the code was written.
package task

import "fmt"
import (
"fmt"
"sync"
)

type Task struct {
ID int64 // Unique identifier
Expand All @@ -33,6 +36,7 @@ func NewTask(title string) (*Task, error) {

// TaskManager manages a list of tasks in memory.
type TaskManager struct {
sync.RWMutex
tasks []*Task
lastID int64
}
Expand All @@ -44,6 +48,8 @@ func NewTaskManager() *TaskManager {

// Save saves the given Task in the TaskManager.
func (m *TaskManager) Save(task *Task) error {
m.Lock()
defer m.Unlock()
if task.ID == 0 {
m.lastID++
task.ID = m.lastID
Expand All @@ -68,12 +74,18 @@ func cloneTask(t *Task) *Task {

// All returns the list of all the Tasks in the TaskManager.
func (m *TaskManager) All() []*Task {
return m.tasks
m.RLock()
defer m.RUnlock()
out := make([]*Task, len(m.tasks))
copy(out, m.tasks)
return out
}

// Find returns the Task with the given id in the TaskManager and a boolean
// indicating if the id was found.
func (m *TaskManager) Find(ID int64) (*Task, bool) {
m.RLock()
defer m.RUnlock()
for _, t := range m.tasks {
if t.ID == ID {
return t, true
Expand Down