public Task SyncCategory(DeserializeCategoryItem item) { return(Task.Run(() => { var category = _database.Table <Category>().FirstOrDefault(i => i.DocumentId == item.Id); if (item.IsDeleted) { if (category != null) { _database.BeginTransaction(); _database.Table <Category>().Delete(i => i.Id == category.Id); _database.Table <DiscountCategory>().Delete(i => i.CategoryId == category.DocumentId); _database.Table <DiscountsStrings>() .Where(i => i.OwnerId == category.Id && i.Appointment == StrAppointmentCategory) .Select(i => i.LangStringId) .ForEach(i => _database.Table <LangString>().Delete(j => j.Id == i)); _database.Table <DiscountsStrings>().Delete(i => i.OwnerId == category.Id && i.Appointment == StrAppointmentCategory); _database.Commit(); } } else { SaveCategory(item, category); } })); }
private void SaveCategory(DeserializeCategoryItem categoryItem, Category categoryDbData) { _database.BeginTransaction(); if (categoryDbData != null) { _database.Table <DiscountsStrings>() .Where(i => i.OwnerId == categoryDbData.Id && i.Appointment == StrAppointmentCategory) .Select(i => i.LangStringId).ForEach(i => _database.Table <LangString>().Delete(j => j.Id == i)); _database.Table <DiscountsStrings>().Delete(i => i.OwnerId == categoryDbData.Id && i.Appointment == StrAppointmentCategory); categoryDbData.DocumentId = categoryItem.Id; categoryDbData.Color = categoryItem.Color; categoryDbData.Modified = categoryItem.Modified ?? DateTime.UtcNow; _database.Update(categoryDbData); } else { categoryDbData = new Category { DocumentId = categoryItem.Id, Color = categoryItem.Color, Modified = categoryItem.Modified ?? DateTime.UtcNow }; _database.Insert(categoryDbData); } #region save children if (categoryItem.Name != null) { foreach (var name in categoryItem.Name) { var langStringRec = new LangString { LanguageCode = name.Lan.ToUpper(), Text = name.LocText }; _database.Insert(langStringRec); var discountsStringsRec = new DiscountsStrings { Appointment = StrAppointmentCategory, OwnerId = categoryDbData.Id, LangStringId = langStringRec.Id }; _database.Insert(discountsStringsRec); } } #endregion _database.Commit(); }