From 3b761d3142de18ff9131b1b5a8805654d07da7f4 Mon Sep 17 00:00:00 2001 From: Yong Zhi Yuan Date: Mon, 24 Jan 2022 19:21:27 +0800 Subject: [PATCH] Prefer async/await over returning Task --- .../Calendar/Components/CreateUpdateForm.razor.cs | 2 +- Client/Services/Synchronizer/CompleteTaskCommand.cs | 2 +- Client/Services/Synchronizer/CreateImageCommand.cs | 2 +- Client/Services/Synchronizer/CreateIssueCommand.cs | 12 ++++++++---- Client/Services/Synchronizer/DeleteImageCommand.cs | 2 +- Client/Services/Synchronizer/DeleteIssueCommand.cs | 12 ++++++++---- Client/Services/Synchronizer/UpdateImageCommand.cs | 2 +- Client/Services/Synchronizer/UpdateIssueCommand.cs | 12 ++++++++---- Client/Shared/BaseTopBar.razor.cs | 2 +- 9 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Client/Pages/Calendar/Components/CreateUpdateForm.razor.cs b/Client/Pages/Calendar/Components/CreateUpdateForm.razor.cs index 3c6faceb..c236f287 100644 --- a/Client/Pages/Calendar/Components/CreateUpdateForm.razor.cs +++ b/Client/Pages/Calendar/Components/CreateUpdateForm.razor.cs @@ -66,5 +66,5 @@ private async Task ToggleEndPicker() // can't extract w/ ToggleStartPicker() as } } - private Task Remove(IssueModel toRemove) => RemovedChanged.InvokeAsync(toRemove); + private async Task Remove(IssueModel toRemove) => await RemovedChanged.InvokeAsync(toRemove); } diff --git a/Client/Services/Synchronizer/CompleteTaskCommand.cs b/Client/Services/Synchronizer/CompleteTaskCommand.cs index f2128213..bf66b6a0 100644 --- a/Client/Services/Synchronizer/CompleteTaskCommand.cs +++ b/Client/Services/Synchronizer/CompleteTaskCommand.cs @@ -10,5 +10,5 @@ public class CompleteTaskCommand : ICommand public CompleteTaskCommand(IJSRuntime js, CreateCompletedTaskModel model) => (_js, _model) = (js, model); - public Task Execute() => _js.InvokeVoidAsync("completeTask", _model).AsTask(); + public async Task Execute() => await _js.InvokeVoidAsync("completeTask", _model).AsTask(); } diff --git a/Client/Services/Synchronizer/CreateImageCommand.cs b/Client/Services/Synchronizer/CreateImageCommand.cs index 6bd0015e..b86f23b6 100644 --- a/Client/Services/Synchronizer/CreateImageCommand.cs +++ b/Client/Services/Synchronizer/CreateImageCommand.cs @@ -10,5 +10,5 @@ public class CreateImageCommand : ICommand public CreateImageCommand(IJSRuntime js, ImageModel model) => (_js, _model) = (js, model); - public Task Execute() => _js.InvokeVoidAsync("createImage", _model).AsTask(); + public async Task Execute() => await _js.InvokeVoidAsync("createImage", _model).AsTask(); } diff --git a/Client/Services/Synchronizer/CreateIssueCommand.cs b/Client/Services/Synchronizer/CreateIssueCommand.cs index adf2f0ab..f5b49ef3 100644 --- a/Client/Services/Synchronizer/CreateIssueCommand.cs +++ b/Client/Services/Synchronizer/CreateIssueCommand.cs @@ -1,14 +1,18 @@ +using Couple.Client.Data; using Couple.Client.Model.Issue; -using Microsoft.JSInterop; namespace Couple.Client.Services.Synchronizer; public class CreateIssueCommand : ICommand { - private readonly IJSRuntime _js; + private readonly AppDbContext _dbContext; private readonly IssueModel _model; - public CreateIssueCommand(IJSRuntime js, IssueModel model) => (_js, _model) = (js, model); + public CreateIssueCommand(AppDbContext dbContext, IssueModel model) => (_dbContext, _model) = (dbContext, model); - public Task Execute() => _js.InvokeVoidAsync("createIssue", _model).AsTask(); + public async Task Execute() + { + _dbContext.Issues.Add(_model); + await _dbContext.SaveChangesAsync(); + } } diff --git a/Client/Services/Synchronizer/DeleteImageCommand.cs b/Client/Services/Synchronizer/DeleteImageCommand.cs index 93339f0f..22e3942f 100644 --- a/Client/Services/Synchronizer/DeleteImageCommand.cs +++ b/Client/Services/Synchronizer/DeleteImageCommand.cs @@ -9,5 +9,5 @@ public class DeleteImageCommand : ICommand public DeleteImageCommand(IJSRuntime js, Guid guid) => (_js, _guid) = (js, guid); - public Task Execute() => _js.InvokeVoidAsync("deleteImage", _guid).AsTask(); + public async Task Execute() => await _js.InvokeVoidAsync("deleteImage", _guid).AsTask(); } diff --git a/Client/Services/Synchronizer/DeleteIssueCommand.cs b/Client/Services/Synchronizer/DeleteIssueCommand.cs index 4a757308..184a51e5 100644 --- a/Client/Services/Synchronizer/DeleteIssueCommand.cs +++ b/Client/Services/Synchronizer/DeleteIssueCommand.cs @@ -1,13 +1,17 @@ -using Microsoft.JSInterop; +using Couple.Client.Data; namespace Couple.Client.Services.Synchronizer; public class DeleteIssueCommand : ICommand { + private readonly AppDbContext _dbContext; private readonly Guid _guid; - private readonly IJSRuntime _js; - public DeleteIssueCommand(IJSRuntime js, Guid guid) => (_js, _guid) = (js, guid); + public DeleteIssueCommand(AppDbContext dbContext, Guid guid) => (_dbContext, _guid) = (dbContext, guid); - public Task Execute() => _js.InvokeVoidAsync("deleteIssue", _guid).AsTask(); + public async Task Execute() + { + _dbContext.Issues.Remove(new() { Id = _guid }); + await _dbContext.SaveChangesAsync(); + } } diff --git a/Client/Services/Synchronizer/UpdateImageCommand.cs b/Client/Services/Synchronizer/UpdateImageCommand.cs index 886d7f47..2fc3860d 100644 --- a/Client/Services/Synchronizer/UpdateImageCommand.cs +++ b/Client/Services/Synchronizer/UpdateImageCommand.cs @@ -10,5 +10,5 @@ public class UpdateImageCommand : ICommand public UpdateImageCommand(IJSRuntime js, ImageModel model) => (_js, _model) = (js, model); - public Task Execute() => _js.InvokeVoidAsync("updateImage", _model).AsTask(); + public async Task Execute() => await _js.InvokeVoidAsync("updateImage", _model).AsTask(); } diff --git a/Client/Services/Synchronizer/UpdateIssueCommand.cs b/Client/Services/Synchronizer/UpdateIssueCommand.cs index 80c9dcae..c75fe4fd 100644 --- a/Client/Services/Synchronizer/UpdateIssueCommand.cs +++ b/Client/Services/Synchronizer/UpdateIssueCommand.cs @@ -1,14 +1,18 @@ +using Couple.Client.Data; using Couple.Client.Model.Issue; -using Microsoft.JSInterop; namespace Couple.Client.Services.Synchronizer; public class UpdateIssueCommand : ICommand { - private readonly IJSRuntime _js; + private readonly AppDbContext _dbContext; private readonly IssueModel _model; - public UpdateIssueCommand(IJSRuntime js, IssueModel model) => (_js, _model) = (js, model); + public UpdateIssueCommand(AppDbContext dbContext, IssueModel model) => (_dbContext, _model) = (dbContext, model); - public Task Execute() => _js.InvokeVoidAsync("updateIssue", _model).AsTask(); + public async Task Execute() + { + _dbContext.Issues.Update(_model); + await _dbContext.SaveChangesAsync(); + } } diff --git a/Client/Shared/BaseTopBar.razor.cs b/Client/Shared/BaseTopBar.razor.cs index 2751b1b7..625e6aa4 100644 --- a/Client/Shared/BaseTopBar.razor.cs +++ b/Client/Shared/BaseTopBar.razor.cs @@ -11,5 +11,5 @@ public partial class BaseTopBar [Inject] private Synchronizer Synchronizer { get; init; } - private Task Synchronize() => Synchronizer.SynchronizeAsync(); + private async Task Synchronize() => await Synchronizer.SynchronizeAsync(); }