gte/internal/process/fetch.go

62 lines
1.2 KiB
Go
Raw Normal View History

2021-06-25 09:14:27 +02:00
package process
import (
"errors"
"fmt"
"time"
2021-09-19 11:59:26 +02:00
"ewintr.nl/gte/internal/storage"
"ewintr.nl/gte/internal/task"
2021-06-25 09:14:27 +02:00
)
var (
2021-09-03 07:12:16 +02:00
ErrFetchProcess = errors.New("could not fetch tasks")
2021-06-25 09:14:27 +02:00
)
2021-09-03 07:12:16 +02:00
// Fetch fetches all tasks in regular folders from the remote repository and overwrites what is stored locally
type Fetch struct {
2021-06-25 09:14:27 +02:00
remote *storage.RemoteRepository
local storage.LocalRepository
}
2021-09-03 07:12:16 +02:00
type FetchResult struct {
2021-06-25 09:14:27 +02:00
Duration string `json:"duration"`
Count int `json:"count"`
}
2021-09-03 07:12:16 +02:00
func NewFetch(remote *storage.RemoteRepository, local storage.LocalRepository) *Fetch {
return &Fetch{
2021-06-25 09:14:27 +02:00
remote: remote,
local: local,
}
}
2021-09-03 07:12:16 +02:00
func (s *Fetch) Process() (*FetchResult, error) {
2021-06-25 09:14:27 +02:00
start := time.Now()
2022-10-18 16:58:12 +02:00
fmt.Printf("proc: %+v\n", s)
2021-06-25 09:14:27 +02:00
tasks := []*task.Task{}
for _, folder := range task.KnownFolders {
if folder == task.FOLDER_INBOX {
continue
}
folderTasks, err := s.remote.FindAll(folder)
if err != nil {
2021-09-03 07:12:16 +02:00
return &FetchResult{}, fmt.Errorf("%w: %v", ErrFetchProcess, err)
2021-06-25 09:14:27 +02:00
}
for _, t := range folderTasks {
tasks = append(tasks, t)
}
}
if err := s.local.SetTasks(tasks); err != nil {
2021-09-03 07:12:16 +02:00
return &FetchResult{}, fmt.Errorf("%w: %v", ErrFetchProcess, err)
2021-06-25 09:14:27 +02:00
}
2021-09-03 07:12:16 +02:00
return &FetchResult{
2021-06-25 09:14:27 +02:00
Duration: time.Since(start).String(),
Count: len(tasks),
}, nil
}