diff --git a/OpenBudgeteer.Core.Data.Entities/Models/Bucket.cs b/OpenBudgeteer.Core.Data.Entities/Models/Bucket.cs index 35f8e3b..2615c5a 100644 --- a/OpenBudgeteer.Core.Data.Entities/Models/Bucket.cs +++ b/OpenBudgeteer.Core.Data.Entities/Models/Bucket.cs @@ -32,9 +32,9 @@ public class Bucket : IEntity [NotMapped] public BucketVersion? CurrentVersion { get; set; } - public ICollection BucketVersions { get; set; } + public ICollection? BucketVersions { get; set; } - public ICollection BudgetedTransactions { get; set; } + public ICollection? BudgetedTransactions { get; set; } - public ICollection BucketMovements { get; set; } + public ICollection? BucketMovements { get; set; } } diff --git a/OpenBudgeteer.Core.Data.Entities/Models/BucketGroup.cs b/OpenBudgeteer.Core.Data.Entities/Models/BucketGroup.cs index c4a353e..0807e69 100644 --- a/OpenBudgeteer.Core.Data.Entities/Models/BucketGroup.cs +++ b/OpenBudgeteer.Core.Data.Entities/Models/BucketGroup.cs @@ -14,5 +14,5 @@ public class BucketGroup : IEntity [Required] public int Position { get; set; } - public ICollection Buckets { get; set; } + public ICollection? Buckets { get; set; } } diff --git a/OpenBudgeteer.Core.Data.Entities/Models/BucketRuleSet.cs b/OpenBudgeteer.Core.Data.Entities/Models/BucketRuleSet.cs index fe70bf1..5e8e58a 100644 --- a/OpenBudgeteer.Core.Data.Entities/Models/BucketRuleSet.cs +++ b/OpenBudgeteer.Core.Data.Entities/Models/BucketRuleSet.cs @@ -19,5 +19,5 @@ public class BucketRuleSet : IEntity public Bucket TargetBucket { get; set; } = null!; - public ICollection MappingRules { get; set; } + public ICollection? MappingRules { get; set; } } diff --git a/OpenBudgeteer.Core.Data.Repository/BucketGroupRepository.cs b/OpenBudgeteer.Core.Data.Repository/BucketGroupRepository.cs index 5453bc7..7993c4c 100644 --- a/OpenBudgeteer.Core.Data.Repository/BucketGroupRepository.cs +++ b/OpenBudgeteer.Core.Data.Repository/BucketGroupRepository.cs @@ -60,7 +60,7 @@ public int Delete(Guid id) .Include(i => i.Buckets) .FirstOrDefault(i => i.Id == id); if (entity == null) throw new Exception($"BucketGroup with id {id} not found."); - if (entity.Buckets.Count != 0) throw new Exception($"Cannot delete a BucketGroup with Buckets assigned to it."); + if (entity.Buckets != null && entity.Buckets.Count != 0) throw new Exception($"Cannot delete a BucketGroup with Buckets assigned to it."); DatabaseContext.BucketGroup.Remove(entity); return DatabaseContext.SaveChanges(); @@ -75,7 +75,7 @@ public int DeleteRange(IEnumerable ids) .Include(i => i.Buckets) .Where(i => cleansedEntities.Contains(i.Id)); if (!entities.Any()) throw new Exception($"No BucketGroup found with passed IDs."); - if (entities.Any(i => i.Buckets.Count != 0)) throw new Exception($"Cannot delete a BucketGroup with Buckets assigned to it."); + if (entities.Any(i => i.Buckets != null && i.Buckets.Count != 0)) throw new Exception($"Cannot delete a BucketGroup with Buckets assigned to it."); DatabaseContext.BucketGroup.RemoveRange(entities); return DatabaseContext.SaveChanges(); diff --git a/OpenBudgeteer.Core.Data.Repository/BucketRepository.cs b/OpenBudgeteer.Core.Data.Repository/BucketRepository.cs index 32d70c5..d4a273c 100644 --- a/OpenBudgeteer.Core.Data.Repository/BucketRepository.cs +++ b/OpenBudgeteer.Core.Data.Repository/BucketRepository.cs @@ -23,7 +23,7 @@ public IQueryable AllWithVersions() => DatabaseContext.Bucket public IQueryable AllWithActivities() => DatabaseContext.Bucket .Include(i => i.BucketMovements) - .Include(i => i.BudgetedTransactions).ThenInclude(i => i.Transaction) + .Include(i => i.BudgetedTransactions)!.ThenInclude(i => i.Transaction) .AsNoTracking(); public IQueryable AllWithIncludedEntities() => DatabaseContext.Bucket @@ -45,7 +45,7 @@ public IQueryable AllWithIncludedEntities() => DatabaseContext.Bucket .FirstOrDefault(i => i.Id == id); public Bucket? ByIdWithTransactions(Guid id) => DatabaseContext.Bucket - .Include(i => i.BudgetedTransactions).ThenInclude(i => i.Transaction) + .Include(i => i.BudgetedTransactions)!.ThenInclude(i => i.Transaction) .FirstOrDefault(i => i.Id == id); /*public Bucket? ByIdWithActivities(Guid id) => DatabaseContext.Bucket @@ -95,8 +95,8 @@ public int Delete(Guid id) .Include(i => i.BudgetedTransactions) .FirstOrDefault(i => i.Id == id); if (entity == null) throw new Exception($"Bucket with id {id} not found."); - if (entity.BucketMovements.Count != 0) throw new Exception($"Cannot delete a Bucket with BucketMovements assigned to it."); - if (entity.BudgetedTransactions.Count != 0) throw new Exception($"Cannot delete a Bucket with BudgetedTransactions assigned to it."); + if (entity.BucketMovements != null && entity.BucketMovements.Count != 0) throw new Exception($"Cannot delete a Bucket with BucketMovements assigned to it."); + if (entity.BudgetedTransactions != null && entity.BudgetedTransactions.Count != 0) throw new Exception($"Cannot delete a Bucket with BudgetedTransactions assigned to it."); DatabaseContext.Bucket.Remove(entity); return DatabaseContext.SaveChanges(); @@ -116,8 +116,8 @@ public int DeleteRange(IEnumerable ids) .Include(i => i.BudgetedTransactions) .Where(i => cleansedEntities.Contains(i.Id)); if (!entities.Any()) throw new Exception($"No Buckets found with passed IDs."); - if (entities.Any(i => i.BucketMovements.Count != 0)) throw new Exception($"Cannot delete a Bucket with BucketMovements assigned to it."); - if (entities.Any(i => i.BudgetedTransactions.Count != 0)) throw new Exception($"Cannot delete a Bucket with BudgetedTransactions assigned to it."); + if (entities.Any(i => i.BucketMovements != null && i.BucketMovements.Count != 0)) throw new Exception($"Cannot delete a Bucket with BucketMovements assigned to it."); + if (entities.Any(i => i.BudgetedTransactions != null && i.BudgetedTransactions.Count != 0)) throw new Exception($"Cannot delete a Bucket with BudgetedTransactions assigned to it."); DatabaseContext.Bucket.RemoveRange(entities); return DatabaseContext.SaveChanges(); diff --git a/OpenBudgeteer.Core.Data.Services/BucketGroupService.cs b/OpenBudgeteer.Core.Data.Services/BucketGroupService.cs index f906460..b63f527 100644 --- a/OpenBudgeteer.Core.Data.Services/BucketGroupService.cs +++ b/OpenBudgeteer.Core.Data.Services/BucketGroupService.cs @@ -146,7 +146,7 @@ public override void Delete(Guid id) var entity = bucketGroupRepository.ByIdWithIncludedEntities(id); if (entity == null) throw new Exception("BucketGroup not found"); - if (entity.Buckets.Any()) throw new Exception("BucketGroup with Buckets cannot be deleted"); + if (entity.Buckets != null && entity.Buckets.Any()) throw new Exception("BucketGroup with Buckets cannot be deleted"); var oldPosition = entity.Position; bucketGroupRepository.Delete(id); diff --git a/OpenBudgeteer.Core.Data.Services/BucketRuleSetService.cs b/OpenBudgeteer.Core.Data.Services/BucketRuleSetService.cs index 4dfd27c..7f1a27c 100644 --- a/OpenBudgeteer.Core.Data.Services/BucketRuleSetService.cs +++ b/OpenBudgeteer.Core.Data.Services/BucketRuleSetService.cs @@ -83,7 +83,7 @@ public override BucketRuleSet Update(BucketRuleSet entity) .Where(i => i.BucketRuleSetId == entity.Id) .ToList() // Select which of the database IDs are no longer available in entity - .Where(i => entity.MappingRules + .Where(i => entity.MappingRules != null && entity.MappingRules .All(j => j.Id != i.Id)) .Select(i => i.Id) .ToList(); diff --git a/OpenBudgeteer.Core.Data.Services/BucketService.cs b/OpenBudgeteer.Core.Data.Services/BucketService.cs index af69f02..0157c5c 100644 --- a/OpenBudgeteer.Core.Data.Services/BucketService.cs +++ b/OpenBudgeteer.Core.Data.Services/BucketService.cs @@ -23,7 +23,7 @@ public Bucket GetWithLatestVersion(Guid id) var result = repository.ByIdWithVersions(id); if (result == null) throw new Exception($"{typeof(Bucket)} not found in database"); result.CurrentVersion = GetLatestVersion(id, DateTime.Now); - result.BucketVersions = result.BucketVersions.OrderByDescending(i => i.Version).ToList(); + result.BucketVersions = result.BucketVersions!.OrderByDescending(i => i.Version).ToList(); return result; } @@ -97,10 +97,10 @@ public IEnumerable GetActiveBuckets(DateTime validFrom) .ToList(); foreach (var bucket in result) { - bucket.CurrentVersion = bucket.BucketVersions + bucket.CurrentVersion = bucket.BucketVersions! .OrderByDescending(i => i.ValidFrom) .ToList() - .First(i => i!.ValidFrom <= validFrom); + .First(i => i.ValidFrom <= validFrom); } return result; @@ -147,19 +147,19 @@ public BucketFigures GetFigures(Guid bucketId, DateTime yearMonth) decimal input = 0, output = 0; // Calculate Balance - var balance = bucketWithTransactions.BudgetedTransactions + var balance = bucketWithTransactions.BudgetedTransactions! .Where(i => i.Transaction.TransactionDate < yearMonth.AddMonths(1)) .ToList() .Sum(i => i.Amount); - balance += bucketWithMovements.BucketMovements + balance += bucketWithMovements.BucketMovements! .Where(i => i.MovementDate < yearMonth.AddMonths(1)) .ToList() .Sum(i => i.Amount); // Calculate In & Out - var bucketTransactionsCurrentMonth = bucketWithTransactions.BudgetedTransactions + var bucketTransactionsCurrentMonth = bucketWithTransactions.BudgetedTransactions! .Where(i => i.Transaction.TransactionDate.Year == yearMonth.Year && i.Transaction.TransactionDate.Month == yearMonth.Month) @@ -173,7 +173,7 @@ public BucketFigures GetFigures(Guid bucketId, DateTime yearMonth) input += bucketTransaction.Amount; } - var bucketMovementsCurrentMonth = bucketWithMovements.BucketMovements + var bucketMovementsCurrentMonth = bucketWithMovements.BucketMovements! .Where(i => i.MovementDate.Year == yearMonth.Year && i.MovementDate.Month == yearMonth.Month) @@ -205,12 +205,12 @@ public decimal GetBalance(Guid bucketId, DateTime yearMonth) var bucketWithTransactions = repository.ByIdWithTransactions(bucketId) ?? throw new Exception("Bucket not found."); var bucketWithMovements = repository.ByIdWithMovements(bucketId) ?? throw new Exception("Bucket not found."); - var result = bucketWithTransactions.BudgetedTransactions + var result = bucketWithTransactions.BudgetedTransactions! .Where(i => i.Transaction.TransactionDate < yearMonth.AddMonths(1)) .ToList() .Sum(i => i.Amount); - result += bucketWithMovements.BucketMovements + result += bucketWithMovements.BucketMovements! .Where(i => i.MovementDate < yearMonth.AddMonths(1)) .ToList() .Sum(i => i.Amount); @@ -235,7 +235,7 @@ public BucketFigures GetInAndOut(Guid bucketId, DateTime yearMonth) decimal input = 0, output = 0; - var bucketTransactionsCurrentMonth = bucketWithTransactions.BudgetedTransactions + var bucketTransactionsCurrentMonth = bucketWithTransactions.BudgetedTransactions! .Where(i => i.Transaction.TransactionDate.Year == yearMonth.Year && i.Transaction.TransactionDate.Month == yearMonth.Month) @@ -249,7 +249,7 @@ public BucketFigures GetInAndOut(Guid bucketId, DateTime yearMonth) input += bucketTransaction.Amount; } - var bucketMovementsCurrentMonth = bucketWithMovements.BucketMovements + var bucketMovementsCurrentMonth = bucketWithMovements.BucketMovements! .Where(i => i.MovementDate.Year == yearMonth.Year && i.MovementDate.Month == yearMonth.Month) diff --git a/OpenBudgeteer.Core/ViewModels/EntityViewModels/RuleSetViewModel.cs b/OpenBudgeteer.Core/ViewModels/EntityViewModels/RuleSetViewModel.cs index 4be32b1..c673350 100644 --- a/OpenBudgeteer.Core/ViewModels/EntityViewModels/RuleSetViewModel.cs +++ b/OpenBudgeteer.Core/ViewModels/EntityViewModels/RuleSetViewModel.cs @@ -177,10 +177,13 @@ protected RuleSetViewModel(IServiceManager serviceManager, IEnumerable a _targetBucketName = bucketRuleSet.TargetBucket.Name ?? string.Empty; _targetBucketColorCode = bucketRuleSet.TargetBucket.ColorCode ?? string.Empty; _targetBucketTextColorCode = bucketRuleSet.TargetBucket.TextColorCode ?? string.Empty; - - foreach (var mappingRule in bucketRuleSet.MappingRules) + + if (bucketRuleSet.MappingRules != null) { - MappingRules.Add(new MappingRuleViewModel(serviceManager, mappingRule)); + foreach (var mappingRule in bucketRuleSet.MappingRules) + { + MappingRules.Add(new MappingRuleViewModel(serviceManager, mappingRule)); + } } } } diff --git a/OpenBudgeteer.Core/ViewModels/Helper/BucketListingViewModel.cs b/OpenBudgeteer.Core/ViewModels/Helper/BucketListingViewModel.cs index b25b258..db80ed0 100644 --- a/OpenBudgeteer.Core/ViewModels/Helper/BucketListingViewModel.cs +++ b/OpenBudgeteer.Core/ViewModels/Helper/BucketListingViewModel.cs @@ -60,7 +60,7 @@ public virtual async Task LoadDataAsync(bool excludeIn var bucketItemTasks = new List>(); - foreach (var bucket in bucketGroup.Buckets.OrderBy(i => i.Name)) + foreach (var bucket in bucketGroup.Buckets!.OrderBy(i => i.Name)) { if (excludeInactive && bucket.IsInactive) continue; // Skip as inactive Buckets should be excluded if (bucket.ValidFrom > YearMonthViewModel.CurrentMonth) continue; // Bucket not yet active for selected month diff --git a/OpenBudgeteer.Core/ViewModels/PageViewModels/ReportPageViewModel.cs b/OpenBudgeteer.Core/ViewModels/PageViewModels/ReportPageViewModel.cs index 57024cc..620ed13 100644 --- a/OpenBudgeteer.Core/ViewModels/PageViewModels/ReportPageViewModel.cs +++ b/OpenBudgeteer.Core/ViewModels/PageViewModels/ReportPageViewModel.cs @@ -214,7 +214,7 @@ protected async Task> LoadMonthExpensesB i.Id != Guid.Parse("00000000-0000-0000-0000-000000000002"))) { // Check on right Bucket Type - var latestVersion = bucket.BucketVersions + var latestVersion = bucket.BucketVersions! .OrderByDescending(i => i.Version) .First(); if (latestVersion.BucketType != 2) continue;