From ca2620d4268fe43146a961afc4942a02cf9e1463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Thu, 4 Apr 2019 15:35:42 +0200 Subject: [PATCH] WIP: implemented logic to update category --- internal/pkg/category/category.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/pkg/category/category.go b/internal/pkg/category/category.go index e4e0dcf..31db69b 100644 --- a/internal/pkg/category/category.go +++ b/internal/pkg/category/category.go @@ -24,14 +24,27 @@ func SynchronizePiwigoCategories(piwigoApi piwigo.PiwigoCategoryApi, db datastor } for _, pwgcat := range categories { - cat := datastore.CategoryData{ - PiwigoId: pwgcat.Id, - PiwigoParentId: pwgcat.ParentId, - Name: pwgcat.Name, - Key: pwgcat.Key, + + dbcat, err := db.GetCategoryByPiwigoId(pwgcat.Id) + if err == datastore.ErrorRecordNotFound { + logrus.Debugf("Adding category %s", pwgcat.Key) + dbcat = datastore.CategoryData{ + PiwigoId: pwgcat.Id, + } + } else if err != nil { + return err } - err = db.SaveCategory(cat) + if dbcat.Name == pwgcat.Name && dbcat.Key == pwgcat.Key && dbcat.PiwigoParentId == pwgcat.ParentId { + logrus.Debugf("No changes for category %s", dbcat.Key) + continue + } + + dbcat.Name = pwgcat.Name + dbcat.Key = pwgcat.Key + dbcat.PiwigoParentId = pwgcat.ParentId + + err = db.SaveCategory(dbcat) if err != nil { return err }