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",