Sync instead of download.
Updated Sync functions to perform proper synchronizations instead of pure downloads. This means that if something is deleted on the server, it will be deleted on the client as well, instead of just left hanging. The SyncProclamations function was also updated to perform this way, but while retaining the user's saved list.
This commit is contained in:
+34
-5
@@ -17,7 +17,6 @@ func SyncProclamations(database *gorm.DB) ([]db.Proclamation, error) {
|
|||||||
fullURL := "https://portal.rhemawavetech.com/api/v1/proclamations"
|
fullURL := "https://portal.rhemawavetech.com/api/v1/proclamations"
|
||||||
logger.Debug("Syncronizing proclamations. URL: %s", fullURL)
|
logger.Debug("Syncronizing proclamations. URL: %s", fullURL)
|
||||||
|
|
||||||
// 1. Fetch from API
|
|
||||||
proclamations, err := netreq.GetJSON[db.Proclamation](fullURL)
|
proclamations, err := netreq.GetJSON[db.Proclamation](fullURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we're offline or API is down, we log it but don't crash.
|
// If we're offline or API is down, we log it but don't crash.
|
||||||
@@ -27,17 +26,37 @@ func SyncProclamations(database *gorm.DB) ([]db.Proclamation, error) {
|
|||||||
}
|
}
|
||||||
logger.Debug("Request Granted")
|
logger.Debug("Request Granted")
|
||||||
|
|
||||||
// 2. Data Transformation (Cleaning)
|
|
||||||
for i := range proclamations {
|
for i := range proclamations {
|
||||||
proclamations[i].Content = htmlRegex.ReplaceAllString(proclamations[i].Content, "")
|
proclamations[i].Content = htmlRegex.ReplaceAllString(proclamations[i].Content, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Persist to Database
|
var storedProclamationSettings []db.Proclamation
|
||||||
// Using Transaction ensures we don't end up with partial data if something fails
|
database.Select("ID", "Selected").Find(&storedProclamationSettings)
|
||||||
|
// [ID]Selected
|
||||||
|
stash := make(map[string]bool)
|
||||||
|
for _, row := range storedProclamationSettings {
|
||||||
|
stash[row.ID] = row.Selected
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range proclamations {
|
||||||
|
proclamations[i].Selected = stash[proclamations[i].ID]
|
||||||
|
}
|
||||||
|
|
||||||
err = database.Transaction(func(tx *gorm.DB) error {
|
err = database.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.Omit("selected").Save(&proclamations).Error; err != nil {
|
// Clear the database
|
||||||
|
// Where "1 = 1" because batch Delete() requires a condition
|
||||||
|
// 'Where "1 = 1"' is like saying "while true be true" and tricks GORM into deleting everything
|
||||||
|
if err := tx.Where("1 = 1").Delete(db.Proclamation{}).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Then re-populate the database
|
||||||
|
if err := tx.Save(&proclamations).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Old sync code
|
||||||
|
// if err := tx.Omit("selected").Save(&proclamations).Error; err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -59,12 +78,22 @@ func SyncCategories(database *gorm.DB) ([]db.ProclamationCategory, error) {
|
|||||||
logger.Error("Category sync failed, skipping update: %v", err)
|
logger.Error("Category sync failed, skipping update: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = database.Transaction(func(tx *gorm.DB) error {
|
err = database.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := tx.Where("1 = 1").Delete(&db.ProclamationCategory{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := tx.Save(&categories).Error; err != nil {
|
if err := tx.Save(&categories).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed to sync proclamation categories: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
logger.Info("Successfully synced proclamation categories: %d", len(categories))
|
logger.Info("Successfully synced proclamation categories: %d", len(categories))
|
||||||
return categories, nil
|
return categories, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user