From 41bf8585231bddf90e729f71deaebbd66058495e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Thu, 4 Apr 2019 08:54:35 +0200 Subject: [PATCH] WIP: Sync now saves the data --- internal/pkg/category/category.go | 24 ++++++++++++++++++++++-- internal/pkg/category/category_test.go | 11 +++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/internal/pkg/category/category.go b/internal/pkg/category/category.go index b9061eb..e4e0dcf 100644 --- a/internal/pkg/category/category.go +++ b/internal/pkg/category/category.go @@ -16,8 +16,28 @@ import ( "sort" ) -func SynchronizePiwigoCategories(piwigoApi piwigo.PiwigoCategoryApi, datastore datastore.CategoryProvider) error { - return errors.New("N/A") +func SynchronizePiwigoCategories(piwigoApi piwigo.PiwigoCategoryApi, db datastore.CategoryProvider) error { + + categories, err := piwigoApi.GetAllCategories() + if err != nil { + return err + } + + for _, pwgcat := range categories { + cat := datastore.CategoryData{ + PiwigoId: pwgcat.Id, + PiwigoParentId: pwgcat.ParentId, + Name: pwgcat.Name, + Key: pwgcat.Key, + } + + err = db.SaveCategory(cat) + if err != nil { + return err + } + } + + return nil } func GetAllCategoriesFromServer(piwigoApi piwigo.PiwigoCategoryApi) (map[string]*piwigo.PiwigoCategory, error) { diff --git a/internal/pkg/category/category_test.go b/internal/pkg/category/category_test.go index eebc79f..e9b9cb7 100644 --- a/internal/pkg/category/category_test.go +++ b/internal/pkg/category/category_test.go @@ -20,15 +20,18 @@ func Test_SynchronizePiwigoCategories_adds_new_categories(t *testing.T) { defer mockCtrl.Finish() piwigoCategory := createTestPiwigoCategory(1) - piwigoCategories := []piwigo.PiwigoCategory{piwigoCategory} + + piwigoCategories := make(map[string]*piwigo.PiwigoCategory) + piwigoCategories[piwigoCategory.Key] = &piwigoCategory + numberOfCategories := len(piwigoCategories) category := createTestCategoryData(1) dbmock := NewMockCategoryProvider(mockCtrl) - dbmock.EXPECT().SaveCategory(category).Times(1) + dbmock.EXPECT().SaveCategory(category).Times(numberOfCategories) piwigoMock := NewMockPiwigoCategoryApi(mockCtrl) - piwigoMock.EXPECT().GetAllCategories().Return(piwigoCategories).Times(1) + piwigoMock.EXPECT().GetAllCategories().Return(piwigoCategories, nil).Times(len(piwigoCategories)) err := SynchronizePiwigoCategories(piwigoMock, dbmock) if err != nil { @@ -48,7 +51,7 @@ func createTestPiwigoCategory(piwigoId int) piwigo.PiwigoCategory { func createTestCategoryData(piwigoId int) datastore.CategoryData { cat := datastore.CategoryData{ - CategoryId: 1, + CategoryId: 0, PiwigoId: piwigoId, PiwigoParentId: 0, Name: "2019",