Skip to content

Commit

Permalink
Resolve null warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
liguori committed Aug 26, 2024
1 parent 9c32266 commit 5764e68
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 118 deletions.
4 changes: 2 additions & 2 deletions src/Savings.API/Authentication/ApiKeyAuthenticationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class ApiKeyAuthOptions : AuthenticationSchemeOptions

public class CustomAuthHandler : AuthenticationHandler<ApiKeyAuthOptions>
{
public CustomAuthHandler(IOptionsMonitor<ApiKeyAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
public CustomAuthHandler(IOptionsMonitor<ApiKeyAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder)
{
}

Expand Down
26 changes: 20 additions & 6 deletions src/Savings.API/Controllers/MaterializedMoneyItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ public MaterializedMoneyItemsController(SavingsContext context)
public async Task<ActionResult> PostLastMaterializedMoneyItemPeriod(DateTime date, decimal amount)
{
var res = await _context.MaterializedMoneyItems.Where(x => x.EndPeriod).OrderByDescending(x => x.Date).FirstOrDefaultAsync();
res.Date = date;
res.Amount = amount;
res.Projection = amount;
await _context.SaveChangesAsync();
return Ok();
if (res != null)
{
res.Date = date;
res.Amount = amount;
res.Projection = amount;
await _context.SaveChangesAsync();
return Ok();
}
else
{
return NotFound();
}
}


Expand All @@ -33,7 +40,14 @@ public async Task<ActionResult> PostLastMaterializedMoneyItemPeriod(DateTime dat
public async Task<ActionResult<MaterializedMoneyItem>> GetLastMaterializedMoneyItemPeriod()
{
var res = await _context.MaterializedMoneyItems.Where(x => x.EndPeriod).OrderByDescending(x => x.Date).FirstOrDefaultAsync();
return res;
if(res == null)
{
return NotFound();
}
else
{
return res;
}
}

// GET: api/MaterializedMoneyItems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<ActionResult<IEnumerable<RecurrencyAdjustement>>> GetRecurrenc

// GET: api/RecurrencyAdjustements/ByIDRecurrencyAndDate
[HttpGet("ByIDRecurrencyAndDate", Name = "ByIDRecurrencyAndDate")]
public async Task<ActionResult<RecurrencyAdjustement>> GetByIDRecurrency(long idRecurrency, DateTime date)
public async Task<ActionResult<RecurrencyAdjustement?>> GetByIDRecurrency(long idRecurrency, DateTime date)
{
return await _context.RecurrencyAdjustements.Where(x => x.RecurrentMoneyItemID == idRecurrency && (x.RecurrencyNewDate.HasValue && x.RecurrencyNewDate == date.Date || !x.RecurrencyNewDate.HasValue && x.RecurrencyDate == date.Date)).FirstOrDefaultAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task<ActionResult<RecurrentMoneyItem>> InsertCreditFixedMoneyItem(F
DateTime targetDate = DateTime.Now.AddMonths(1);
targetDate = new DateTime(targetDate.Year, targetDate.Month, defaultCreditMoneyItem.StartDate.Day);

var recurrentMoneyItem = new RecurrentMoneyItem { CategoryID = fixedItem.CategoryID, Amount = fixedItem.Amount.Value, Note = fixedItem.Note, RecurrentMoneyItemID = defaultCreditMoneyItem.ID, StartDate = targetDate, EndDate = targetDate };
var recurrentMoneyItem = new RecurrentMoneyItem { CategoryID = fixedItem.CategoryID, Amount = fixedItem.Amount!.Value, Note = fixedItem.Note, RecurrentMoneyItemID = defaultCreditMoneyItem.ID, StartDate = targetDate, EndDate = targetDate };
_context.RecurrentMoneyItems.Add(recurrentMoneyItem);
await _context.SaveChangesAsync();

Expand Down
4 changes: 2 additions & 2 deletions src/Savings.API/Controllers/SavingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public async Task<ActionResult> PostSavingsToHistory(DateTime date)
public async Task<ActionResult> GetBackup()
{
byte[] fileContent;
using (var fs = new FileStream(configuration["DatabasePath"], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var fs = new FileStream(configuration["DatabasePath"]!, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var ms = new MemoryStream())
{
fs.CopyTo(ms);
await fs.CopyToAsync(ms);
fileContent = ms.ToArray();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Savings.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
options.DefaultAuthenticateScheme = ApiKeyAuthOptions.ApiKeySchemaName;
options.DefaultChallengeScheme = ApiKeyAuthOptions.ApiKeySchemaName;
})
.AddApiKeyAuth(options => options.AuthKeys = builder.Configuration[ApiKeys].Split(","));
.AddApiKeyAuth(options => options.AuthKeys = builder.Configuration[ApiKeys]?.Split(","));
}

builder.Services.AddTransient<IProjectionCalculator, ProjectionCalculator>();
Expand Down
1 change: 1 addition & 0 deletions src/Savings.API/Savings.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
Expand Down
30 changes: 15 additions & 15 deletions src/Savings.API/Services/ProjectionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public async Task<IEnumerable<MaterializedMoneyItem>> CalculateAsync(DateTime? f
var recurrentItems = await context.RecurrentMoneyItems
.Include(x => x.Adjustements).Include(x => x.AssociatedItems).Include(x => x.Category)
.Where(x => (x.StartDate <= periodEnd && (!x.EndDate.HasValue || periodStart <= x.EndDate) && x.RecurrentMoneyItemID == null) ||
(x.Adjustements.Count() > 0 && x.Adjustements.First().RecurrencyNewDate.HasValue && x.Adjustements.First().RecurrencyNewDate.Value >= periodStart && x.Adjustements.First().RecurrencyNewDate.Value <= periodEnd))
(x.Adjustements.Count() > 0 && x.Adjustements.First().RecurrencyNewDate.HasValue && x.Adjustements.First().RecurrencyNewDate!.Value >= periodStart && x.Adjustements.First().RecurrencyNewDate!.Value <= periodEnd))
.AsNoTracking().ToListAsync();

if (onlyInstallment) recurrentItems = recurrentItems.Where(x => x.Type == MoneyType.InstallmentPayment).ToList();
Expand All @@ -131,14 +131,14 @@ public async Task<IEnumerable<MaterializedMoneyItem>> CalculateAsync(DateTime? f
{
Date = fixedItem.Date,
CategoryID = fixedItem?.Category?.ID,
Amount = fixedItem.Amount ?? 0,
Amount = fixedItem?.Amount ?? 0,
EndPeriod = false,
Note = fixedItem.Note,
Note = fixedItem?.Note,
Type = MoneyType.Others,
TimelineWeight = fixedItem.TimelineWeight,
TimelineWeight = fixedItem?.TimelineWeight ?? 0,
IsRecurrent = false,
FixedMoneyItemID = fixedItem.ID,
Cash = fixedItem.Cash
FixedMoneyItemID = fixedItem?.ID,
Cash = fixedItem?.Cash ?? false
});
}

Expand Down Expand Up @@ -169,13 +169,13 @@ public async Task<IEnumerable<MaterializedMoneyItem>> CalculateAsync(DateTime? f
foreach (var installment in installments)
{
//Check the adjustements
var currentAdjustment = recurrentItem.Adjustements?.Where(x => x.RecurrencyDate == installment.currentDate || x.RecurrencyNewDate.HasValue && x.RecurrencyNewDate == installment.currentDate).FirstOrDefault();
var currentAdjustment = recurrentItem?.Adjustements?.Where(x => x.RecurrencyDate == installment.currentDate || x.RecurrencyNewDate.HasValue && x.RecurrencyNewDate == installment.currentDate).FirstOrDefault();
var currentInstallmentDate = currentAdjustment?.RecurrencyNewDate ?? installment.original;
var currentInstallmentAmount = currentAdjustment?.RecurrencyNewAmount ?? recurrentItem.Amount;
var currentInstallmentNote = recurrentItem.Note;
var currentInstallmentAmount = currentAdjustment?.RecurrencyNewAmount ?? recurrentItem!.Amount;
var currentInstallmentNote = recurrentItem?.Note;

//Check if is a budget for subtract the accumulated
if (recurrentItem.Type == MoneyType.PeriodicBudget)
if (recurrentItem!.Type == MoneyType.PeriodicBudget)
{
decimal accumulatorToSubtract = accumulatedForBudgetLeft < currentInstallmentAmount ? currentInstallmentAmount : accumulatedForBudgetLeft;
accumulatedForBudgetLeft -= accumulatorToSubtract;
Expand Down Expand Up @@ -216,10 +216,10 @@ public async Task<IEnumerable<MaterializedMoneyItem>> CalculateAsync(DateTime? f
Amount = currentInstallmentAmount,
EndPeriod = false,
Note = currentInstallmentNote,
Type = recurrentItem.Type,
TimelineWeight = recurrentItem.TimelineWeight,
Type = recurrentItem?.Type ?? MoneyType.Others,
TimelineWeight = recurrentItem?.TimelineWeight ?? 0,
IsRecurrent = true,
RecurrentMoneyItemID = recurrentItem.ID,
RecurrentMoneyItemID = recurrentItem?.ID,
Subitems = lstSubItemsRecurrent
});
}
Expand All @@ -232,7 +232,7 @@ public async Task<IEnumerable<MaterializedMoneyItem>> CalculateAsync(DateTime? f
periodStart = periodEnd.AddDays(1);
}
//Calculate the projection
var lastProjectionValue = context.MaterializedMoneyItems.Where(x => x.Date <= fromDate).OrderByDescending(x => x.Date).ThenByDescending(x => x.EndPeriod).AsNoTracking().FirstOrDefault().Projection;
var lastProjectionValue = context.MaterializedMoneyItems.Where(x => x.Date <= fromDate).OrderByDescending(x => x.Date).ThenByDescending(x => x.EndPeriod).AsNoTracking().FirstOrDefault()!.Projection;
res = res.OrderBy(x => x.Date).ThenByDescending(x => x.TimelineWeight).ToList();
foreach (var resItem in res)
{
Expand Down Expand Up @@ -266,7 +266,7 @@ private static DateTime CalculateActualInstallmentDate(RecurrentMoneyItem item,
DateTime currentInstallmentDate;
var adjustment = item.Adjustements?.Where(x => x.RecurrencyDate == currentInstallmentOriginal && x.RecurrencyNewDate.HasValue).FirstOrDefault();
if (adjustment != null)
currentInstallmentDate = adjustment.RecurrencyNewDate.Value;
currentInstallmentDate = adjustment.RecurrencyNewDate!.Value;
else
currentInstallmentDate = currentInstallmentOriginal;
return currentInstallmentDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public CustomAuthorizationMessageHandler(IConfiguration configuration, IAccessTo
: base(provider, navigationManager)
{
ConfigureHandler(
authorizedUrls: new[] { configuration["SavingsApiServiceUrl"] });
authorizedUrls: new[] { configuration["SavingsApiServiceUrl"] ?? throw new ArgumentNullException("SavingsApiServiceUrl") });
}
}
}
14 changes: 5 additions & 9 deletions src/Savings.SPA/Components/InputSelectNumber.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
using Microsoft.AspNetCore.Components.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Savings.SPA.Components
{
public class InputSelectNumber<T> : InputSelect<T>
{
protected override bool TryParseValueFromString(string value, out T result, out string validationErrorMessage)
protected override bool TryParseValueFromString(string? value, out T result, out string validationErrorMessage)
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(long?))
{
if (typeof(T) == typeof(int) && int.TryParse(value, out var resultInt))
{
result = (T)(object)resultInt;
validationErrorMessage = null;
validationErrorMessage = string.Empty;
return true;
}
else if (typeof(T) == typeof(long?) && long.TryParse(value, out var resultLong))
{
result = (T)(object)resultLong;
validationErrorMessage = null;
validationErrorMessage = string.Empty;
return true;
}
else
{
result = default;
result = default!;
validationErrorMessage = "The chosen value is not a valid number.";
return false;
}
}
else
{
return base.TryParseValueFromString(value, out result, out validationErrorMessage);
return base.TryParseValueFromString(value, out result!, out validationErrorMessage!);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Savings.SPA/Pages/ConfigurationEdit.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public partial class ConfigurationEdit : ComponentBase
{

[Inject]
private ISavingsApi savingsAPI { get; set; }
private ISavingsApi savingsAPI { get; set; } = default!;

private Configuration Configuration { get; set; }
private Configuration Configuration { get; set; } = default!;

public MoneyCategory[] Categories { get; set; }
public MoneyCategory[] Categories { get; set; } = default!;

public DateTime LastMaterializedDate { get; set; }

Expand Down
18 changes: 9 additions & 9 deletions src/Savings.SPA/Pages/FixedItemEdit.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ public partial class FixedItemEdit : ComponentBase
{

[Inject]
public ISavingsApi savingsAPI { get; set; }
public ISavingsApi savingsAPI { get; set; } = default!;

[Inject]
DialogService dialogService { get; set; }
DialogService dialogService { get; set; } = default!;

[Inject]
public NotificationService notificationService { get; set; }
public NotificationService notificationService { get; set; } = default!;

[Parameter]
public FixedMoneyItem fixedItemToEdit { get; set; }
public FixedMoneyItem fixedItemToEdit { get; set; } = default!;

public bool Incoming { get; set; }

Expand All @@ -30,9 +30,9 @@ public partial class FixedItemEdit : ComponentBase

public bool OperationRunning { get; set; } = false;

public MoneyCategory[] Categories { get; set; }
public MoneyCategory[]? Categories { get; set; }

InputNumber<decimal?> amountInputNumber;
InputNumber<decimal?>? amountInputNumber;

protected override void OnInitialized()
{
Expand All @@ -49,7 +49,7 @@ protected override async void OnAfterRender(bool firstRender)
if (firstRender)
{
await Task.Delay(500);
await amountInputNumber.Element.Value.FocusAsync();
await amountInputNumber!.Element!.Value.FocusAsync();
}
}

Expand Down Expand Up @@ -105,11 +105,11 @@ private async void OnValidSubmit()
if (!ValidateData()) return;
if (Incoming)
{
fixedItemToEdit.Amount = Math.Abs(fixedItemToEdit.Amount.Value);
fixedItemToEdit.Amount = Math.Abs(fixedItemToEdit.Amount!.Value);
}
else
{
fixedItemToEdit.Amount = -Math.Abs(fixedItemToEdit.Amount.Value);
fixedItemToEdit.Amount = -Math.Abs(fixedItemToEdit.Amount!.Value);
}
if (isNew)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Savings.SPA/Pages/FixedItems.razor
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ else
<td data-title="Date">@item.Date.ToString("dd/MM/yyyy")</td>
<td data-title="Amount" class="text-end">@((item.Amount ?? 0).ToString("N2"))</td>
<td data-title="Category">@item?.Category?.Icon?.ToString() @item?.Category?.Description?.ToString()</td>
<td data-title="Note">@item.Note</td>
<td data-title="Note">@item?.Note</td>
</tr>
}
</tbody>
Expand Down
17 changes: 7 additions & 10 deletions src/Savings.SPA/Pages/FixedItems.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@
using Radzen;
using Savings.Model;
using Savings.SPA.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Savings.SPA.Pages
{
public partial class FixedItems : ComponentBase
{

[Inject]
public ISavingsApi savingsAPI { get; set; }
public ISavingsApi savingsAPI { get; set; } = default!;

[Inject]
public DialogService dialogService { get; set; }
public DialogService dialogService { get; set; } = default!;

public Configuration CurrentConfiguration { get; set; }
public Configuration CurrentConfiguration { get; set; } = default!;

private FixedMoneyItem[] fixedMoneyItems;
private FixedMoneyItem[] fixedMoneyItems = default!;

public DateTime? FilterDateFrom { get; set; }

public DateTime? FilterDateTo { get; set; }

public long? FilterCategory { get; set; }

public MoneyCategory[] Categories { get; set; }
public MoneyCategory[] Categories { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
FilterDateFrom = DateTime.Now.Date.AddMonths(-2);
FilterDateTo = DateTime.Now.Date.AddDays(15);
Categories = await savingsAPI.GetMoneyCategories();
CurrentConfiguration = (await savingsAPI.GetConfigurations()).FirstOrDefault();
CurrentConfiguration = (await savingsAPI.GetConfigurations()).First();
await InitializeList();
}

Expand All @@ -46,7 +43,7 @@ async void Change(DateTime? value, string name)

async Task FilterCategoryChanged(ChangeEventArgs e)
{
var selectedString = e.Value.ToString();
var selectedString = e.Value?.ToString();
FilterCategory = string.IsNullOrWhiteSpace(selectedString) ? null : long.Parse(selectedString);

await InitializeList();
Expand Down
6 changes: 3 additions & 3 deletions src/Savings.SPA/Pages/History.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public partial class History : ComponentBase
{

[Inject]
public ISavingsApi savingsAPI { get; set; }
public ISavingsApi savingsAPI { get; set; } = default!;

private MaterializedMoneyItem[] materializedMoneyItems;
private MaterializedMoneyItem[]? materializedMoneyItems;

[Inject]
public DialogService dialogService { get; set; }
public DialogService dialogService { get; set; } = default!;
public DateTime? FilterDateFrom { get; set; }

public DateTime? FilterDateTo { get; set; }
Expand Down
Loading

0 comments on commit 5764e68

Please sign in to comment.