todo app
This commit is contained in:
47
internal/services/tasks.go
Normal file
47
internal/services/tasks.go
Normal file
@@ -0,0 +1,47 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user