48 lines
962 B
Go
48 lines
962 B
Go
package services
|
|
|
|
import "fmt"
|
|
|
|
type taskService struct {
|
|
tasks map[int]string
|
|
}
|
|
|
|
func NewTaskService() *taskService {
|
|
return &taskService{
|
|
tasks: make(map[int]string, 0),
|
|
}
|
|
}
|
|
|
|
func (r *taskService) Insert(key int, v string) error {
|
|
if _, found := r.tasks[key]; found {
|
|
return fmt.Errorf("already exist %d", key)
|
|
}
|
|
r.tasks[key] = v
|
|
return nil
|
|
}
|
|
func (r *taskService) List() map[int]string {
|
|
return r.tasks
|
|
}
|
|
|
|
func (r *taskService) Show(key int) string {
|
|
if task, found := r.tasks[key]; found {
|
|
return task
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (r *taskService) Delete(key int) error {
|
|
if _, found := r.tasks[key]; !found {
|
|
return fmt.Errorf("task with key %d not found", key)
|
|
}
|
|
delete(r.tasks, key)
|
|
return nil
|
|
}
|
|
|
|
func (r *taskService) Update(key int, newTask string) error {
|
|
if _, found := r.tasks[key]; !found {
|
|
return fmt.Errorf("task with key %d not found", key)
|
|
}
|
|
r.tasks[key] = newTask
|
|
return nil
|
|
}
|