working
This commit is contained in:
parent
f7cc1082b9
commit
39e1c46fa5
|
@ -2,6 +2,7 @@ package command
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
@ -52,33 +53,46 @@ func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.Local
|
|||
return fmt.Errorf("could not receive updates: %v", err)
|
||||
}
|
||||
|
||||
// import to local
|
||||
updated := make([]item.Item, 0)
|
||||
for _, ri := range recItems {
|
||||
if ri.Deleted {
|
||||
if err := localIDRepo.Delete(ri.ID); err != nil {
|
||||
return fmt.Errorf("could not delete local id: %v", err)
|
||||
}
|
||||
if err := eventRepo.Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
|
||||
return fmt.Errorf("could not delete event: %v", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
updated = append(updated, ri)
|
||||
}
|
||||
|
||||
lidMap, err := localIDRepo.FindAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get local ids: %v", err)
|
||||
}
|
||||
for _, ri := range recItems {
|
||||
for _, u := range updated {
|
||||
var eBody item.EventBody
|
||||
if err := json.Unmarshal([]byte(ri.Body), &eBody); err != nil {
|
||||
if err := json.Unmarshal([]byte(u.Body), &eBody); err != nil {
|
||||
return fmt.Errorf("could not unmarshal event body: %v", err)
|
||||
}
|
||||
e := item.Event{
|
||||
ID: ri.ID,
|
||||
ID: u.ID,
|
||||
EventBody: eBody,
|
||||
}
|
||||
|
||||
if err := eventRepo.Store(e); err != nil {
|
||||
return fmt.Errorf("could not store event: %v", err)
|
||||
}
|
||||
lid, ok := lidMap[ri.ID]
|
||||
lid, ok := lidMap[u.ID]
|
||||
if !ok {
|
||||
lid, err = localIDRepo.Next()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get next local id: %v", err)
|
||||
}
|
||||
}
|
||||
if err := localIDRepo.Store(ri.ID, lid); err != nil {
|
||||
return fmt.Errorf("could not store local id: %v", err)
|
||||
|
||||
if err := localIDRepo.Store(u.ID, lid); err != nil {
|
||||
return fmt.Errorf("could not store local id: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("%+v\n", conf)
|
||||
syncClient := client.New(conf.SyncURL, conf.ApiKey)
|
||||
|
||||
app := &cli.App{
|
||||
|
|
Loading…
Reference in New Issue