public TransactionItemEntity SaveTransactionItemFromFile(TransactionItemModel transactionItemModel) { bool isNew = false; var entity = Context.TransactionItems.FirstOrDefault(e => e.Id == transactionItemModel.Id); if (entity == null) { entity = new TransactionItemEntity(); isNew = true; } if (!isNew && entity.Name != transactionItemModel.Name || entity.IsIncome != (transactionItemModel.Type == BaseModel.TypeEnum.Income) || entity.LastValue != transactionItemModel.LastValue || (entity.Category.Name != transactionItemModel.CategoryName && !string.IsNullOrWhiteSpace(transactionItemModel.CategoryName))) { entity.Name = transactionItemModel.Name; entity.Category = _categoryService.SaveCategory(transactionItemModel.Category); entity.IsIncome = transactionItemModel.Type == BaseModel.TypeEnum.Income; entity.LastValue = transactionItemModel.LastValue; entity.CategoryId = entity.Category.Id; } if (isNew) { Context.TransactionItems.Add(entity); } Context.SaveChanges(); return(entity); }
public TransactionItemEntity SaveTransactionItem(TransactionItemModel transactionItemModel) { var entity = Context.TransactionItems.FirstOrDefault(e => e.Name == transactionItemModel.Name); if (entity != null) { if (entity.LastValue != transactionItemModel.LastValue) { entity.LastValue = transactionItemModel.LastValue; Context.SaveChanges(); } if (entity.Category.Name != transactionItemModel.CategoryName && !string.IsNullOrWhiteSpace(transactionItemModel.CategoryName)) { entity.Category = _categoryService.SaveCategory(transactionItemModel.Category); Context.SaveChanges(); } return(Context.TransactionItems.FirstOrDefault(e => e.Name == transactionItemModel.Name)); } entity = new TransactionItemEntity { Name = transactionItemModel.Name, Category = _categoryService.SaveCategory(transactionItemModel.Category), IsIncome = transactionItemModel.Type == BaseModel.TypeEnum.Income, LastValue = transactionItemModel.LastValue }; entity.CategoryId = entity.Category.Id; Context.TransactionItems.Add(entity); Context.SaveChanges(); return(entity); }