Skip to content

Commit

Permalink
Solution - Fixed bug. Change IMessageBox (void -> Task). Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Kudri committed Feb 12, 2024
1 parent c1722a8 commit 9a89a52
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 129 deletions.
12 changes: 6 additions & 6 deletions WatchList.Core/Service/Component/IMessageBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ namespace WatchList.Core.Service.Component
{
public interface IMessageBox
{
public void ShowInfo(string message);
public Task ShowInfo(string message);

public void ShowWarning(string message);
public Task ShowWarning(string message);

public void ShowError(string message);
public Task ShowError(string message);

public bool ShowQuestion(string message);
public Task<bool> ShowQuestion(string message);

public bool ShowQuestionSaveItem(string message);
public Task<bool> ShowQuestionSaveItem(string message);

public DialogReplaceItemQuestion ShowDataReplaceQuestion(string titleItem);
public Task<DialogReplaceItemQuestion> ShowDataReplaceQuestion(string titleItem);
}
}
4 changes: 2 additions & 2 deletions WatchList.Core/Service/DataLoading/DownloadDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void AddItems(WatchItemCollection itemCollection)
}
}

private void UpdateItems(WatchItemCollection itemCollection)
private async Task UpdateItems(WatchItemCollection itemCollection)
{
var dialogResultReplaceItem = DialogReplaceItemQuestion.Unknown;

Expand All @@ -73,7 +73,7 @@ private void UpdateItems(WatchItemCollection itemCollection)
case QuestionResultEnum.Unknown:
case QuestionResultEnum.Yes:
case QuestionResultEnum.No:
dialogResultReplaceItem = _messageBox.ShowDataReplaceQuestion(item.Title);
dialogResultReplaceItem = await _messageBox.ShowDataReplaceQuestion(item.Title);
break;
}

Expand Down
8 changes: 4 additions & 4 deletions WatchList.Core/Service/WatchItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public WatchItemService(WatchItemRepository itemRepository, IMessageBox messageB

public void Remove(Guid id) => _repository.Remove(id);

public void Add(WatchItem item)
public async Task AddAsync(WatchItem item)
{
var selectItem = _repository.SelectDuplicateItems(item);
var countDuplicate = selectItem.Count;
Expand All @@ -34,21 +34,21 @@ public void Add(WatchItem item)
return;
}

if (_messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage))
if (await _messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage))
{
item.Id = selectItem[0];
Update(item);
}
}

public void Updata(WatchItem oldItem, WatchItem modifiedItem)
public async Task UpdateAsync(WatchItem oldItem, WatchItem modifiedItem)
{
var selectItem = _repository.SelectDuplicateItems(modifiedItem);
var countDuplicate = selectItem.Count;

if (countDuplicate == 1 && oldItem.Title != modifiedItem.Title)
{
if (_messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage))
if (await _messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage))
{
modifiedItem.Id = selectItem[0];
Remove(oldItem.Id);
Expand Down
15 changes: 11 additions & 4 deletions WatchList.MudBlazors/Dialog/WatchItemDialog.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components;
using MudBlazor;
using WatchList.Core.Model.ItemCinema;
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Service;
using WatchList.MudBlazors.Extension;
using WatchList.MudBlazors.Model;
Expand Down Expand Up @@ -51,7 +52,7 @@ private async Task Add()
}

var item = _watchItemModel.ToWatchItem();
WatchItemService.Add(item);
await WatchItemService.AddAsync(item);

MudDialog.Close();
}
Expand All @@ -68,14 +69,14 @@ private async Task Updata()

if (!ValidateFields(out var message))
{
ShowMessageWarning(message);
await ShowMessageWarning(message);
return;
}

var item = _watchItemModel.ToWatchItem();
if (!_oldWatchItem.Equals(item))
{
WatchItemService.Updata(_oldWatchItem, item);
await WatchItemService.UpdateAsync(_oldWatchItem, item);
}

MudDialog.Close();
Expand Down Expand Up @@ -116,7 +117,7 @@ private bool ValidateFields(out string message)
return false;
}

if (_watchItemModel.Grade <= 0)
if (_watchItemModel.Grade <= 0 && _watchItemModel.Status != StatusCinema.Planned)
{
message = $"Grade cinema above in zero.";
return false;
Expand All @@ -128,6 +129,12 @@ private bool ValidateFields(out string message)
return false;
}

if (_watchItemModel.Date == null && _watchItemModel.Status == StatusCinema.Viewed)
{
message = "Ener the viewing date.";
return false;
}

return true;
}
}
Expand Down
14 changes: 7 additions & 7 deletions WatchList.MudBlazors/Extension/DialogServiceShow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace WatchList.MudBlazors.Extension
{
public static class DialogServiceShow
{
public static bool DialogYesNoShow(this IDialogService dialogService, string title, string content)
public static async Task<bool> DialogYesNoShow(this IDialogService dialogService, string title, string content)
{
var parameters = new DialogParameters<DialogYesNo> { { x => x.Content, content } };
return DialogShow(parameters, dialogService, title, content);
return await DialogShowAsync(parameters, dialogService, title, content);
}

public static bool DialogOkCloseShow(this IDialogService dialogService, string title, string content)
public static async Task<bool> DialogOkCloseShowAsync(this IDialogService dialogService, string title, string content)
{
var parameters = new DialogParameters<DialogYesNo>
{
Expand All @@ -23,14 +23,14 @@ public static bool DialogOkCloseShow(this IDialogService dialogService, string t
}
}
};
return DialogShow(parameters, dialogService, title, content);
return await DialogShowAsync(parameters, dialogService, title, content);
}

private static bool DialogShow(DialogParameters<DialogYesNo> dialogParameters, IDialogService dialogService, string title, string content)
private static async Task<bool> DialogShowAsync(DialogParameters<DialogYesNo> dialogParameters, IDialogService dialogService, string title, string content)
{
var options = new DialogOptions { CloseOnEscapeKey = true };
var dialog = dialogService.Show<DialogYesNo>(title, dialogParameters, options);
var result = dialog.Result.Result;
var dialog = await dialogService.ShowAsync<DialogYesNo>(title, dialogParameters, options);
var result = await dialog.Result;
return result == null || !result.Cancelled;
}
}
Expand Down
14 changes: 7 additions & 7 deletions WatchList.MudBlazors/Message/MessageBoxDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public class MessageBoxDialog : IMessageBox
public MessageBoxDialog(IDialogService dialogService)
=> _dialogService = dialogService;

public DialogReplaceItemQuestion ShowDataReplaceQuestion(string titleItem)
=> DialogReplaceItemQuestion.Yes;
public Task<DialogReplaceItemQuestion> ShowDataReplaceQuestion(string titleItem)
=> Task.FromResult(DialogReplaceItemQuestion.Yes);

public void ShowError(string message)
public Task ShowError(string message)
=> _dialogService.ShowMessageBox("Error", message, yesText: "Ok");

public void ShowInfo(string message)
public Task ShowInfo(string message)
=> _dialogService.ShowMessageBox("Information", message, yesText: "Ok");

public bool ShowQuestion(string message)
public Task<bool> ShowQuestion(string message)
=> _dialogService.DialogYesNoShow("Question", message);

public bool ShowQuestionSaveItem(string message)
public Task<bool> ShowQuestionSaveItem(string message)
=> _dialogService.DialogYesNoShow("Question", message);

public void ShowWarning(string message)
public Task ShowWarning(string message)
=> _dialogService.ShowMessageBox("Warning", message, yesText: "Ok");
}
}
5 changes: 3 additions & 2 deletions WatchList.MudBlazors/Model/WatchItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace WatchList.MudBlazors.Model
public class WatchItemModel
{
private const int FirstValue = 1;
private const int Zero = 0;

Check warning on line 10 in WatchList.MudBlazors/Model/WatchItemModel.cs

View workflow job for this annotation

GitHub Actions / Lint

Private member 'WatchItemModel.Zero' is unused

private Guid _id = new Guid();
private Guid _id = Guid.NewGuid();
private string _title = string.Empty;
private TypeCinema _type = TypeCinema.Movie;
private int _sequel = FirstValue;
private StatusCinema _status = StatusCinema.Planned;
private DateTime? _date = null;
private int? _grade = FirstValue;
private int? _grade = null;

public WatchItemModel()
{
Expand Down
24 changes: 12 additions & 12 deletions WatchList.MudBlazors/Pages/WatchCinemaTable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@
</ColGroup>

<HeaderContent>
<MudTd Style="text-align:center; font-size:12px">Title</MudTd>
<MudTd Style="text-align:center; font-size:12px">Season/Part</MudTd>
<MudTd Style="text-align:center; font-size:12px">Status</MudTd>
<MudTd Style="text-align:center; font-size:12px">Data</MudTd>
<MudTd Style="text-align:center; font-size:12px">Grade</MudTd>
<MudTd Style="text-align:center; font-size:12px">Type</MudTd>
<MudTd Style="text-align:center; font-size:14px">Title</MudTd>
<MudTd Style="text-align:center; font-size:14px">Season/Part</MudTd>
<MudTd Style="text-align:center; font-size:14px">Status</MudTd>
<MudTd Style="text-align:center; font-size:14px">Data</MudTd>
<MudTd Style="text-align:center; font-size:14px">Grade</MudTd>
<MudTd Style="text-align:center; font-size:14px">Type</MudTd>
</HeaderContent>

<RowTemplate>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Title">@context.Title</MudTh>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Season/Part">@context.Sequel</MudTh>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Status">@context.Status</MudTh>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Data">@context.Date</MudTh>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Grade">@context.Grade</MudTh>
<MudTh Style="text-align:center; font-size:12px" DataLabel="Type">@context.Type</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Title">@context.Title</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Season/Part">@context.Sequel</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Status">@context.Status</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Data">@context.Date?.ToString("d")</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Grade">@context.Grade</MudTh>
<MudTh Style="text-align:center; font-size:14px" DataLabel="Type">@context.Type</MudTh>
</RowTemplate>

<PagerContent>
Expand Down
9 changes: 5 additions & 4 deletions WatchList.MudBlazors/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@

StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);

var path = "logs";

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddMudServices();

builder.Services.AddSingleton(new DbContextFactoryMigrator("app.db"));
builder.Services.AddScoped(e => e.GetRequiredService<DbContextFactoryMigrator>().Create());
builder.Services.AddSingleton(e => new AggregateLogging()
builder.Services.AddScoped(e => new AggregateLogging()
{
new ConsoleLogger(LogLevel.Trace),
new FileLogger(LogLevel.Trace, path),
//new FileLogger(LogLevel.Trace, path),
});
builder.Logging.AddConsole();
builder.Services.AddLogging();
builder.Services.AddScoped(e => new WatchItemRepository(e.GetRequiredService<WatchCinemaDbContext>(), e.GetRequiredService<AggregateLogging>()));
builder.Services.AddScoped<IMessageBox, MessageBoxDialog>();
builder.Services.AddScoped<WatchItemService>();
builder.Logging.AddConsole();

var app = builder.Build();

Expand Down
2 changes: 2 additions & 0 deletions WatchList.MudBlazors/WatchList.MudBlazors.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Bromix.MudBlazor.MaterialDesignIcons" Version="7.4.47.56" />
<PackageReference Include="MudBlazor" Version="6.15.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file modified WatchList.MudBlazors/app.db
Binary file not shown.
Binary file modified WatchList.MudBlazors/app.db-shm
Binary file not shown.
Binary file modified WatchList.MudBlazors/app.db-wal
Binary file not shown.
47 changes: 44 additions & 3 deletions WatchList.MudBlazors/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"LogLevel": { // No provider, LogLevel applies to all the enabled providers.
"Default": "Error",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Warning"
},
"Debug": { // Debug provider.
"LogLevel": {
"Default": "Information" // Overrides preceding LogLevel:Default setting.
}
},
"Console": {
"IncludeScopes": true,
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Information"
}
},
"EventSource": {
"LogLevel": {
"Microsoft": "Information"
}
},
"EventLog": {
"LogLevel": {
"Microsoft": "Information"
}
},
"AzureAppServicesFile": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Warning"
}
},
"AzureAppServicesBlob": {
"IncludeScopes": true,
"LogLevel": {
"Microsoft": "Information"
}
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},
"AllowedHosts": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public class AddDuplicateItemTest

[Theory]
[MemberData(nameof(ListOfElementsWithDuplicateElement))]
public void Add_With_Replace_Duplicate_Element(List<WatchItem> items, WatchItem addItem, List<WatchItem> expectItems)
public async Task Add_With_Replace_Duplicate_ElementAsync(List<WatchItem> items, WatchItem addItem, List<WatchItem> expectItems)
{
// Arrange
var dbContext = new TestAppDbContextFactory().Create();
var itemRepository = new WatchItemRepository(dbContext, new TestLogger());
var messageBox = new Mock<IMessageBox>();
messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true);
messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).ReturnsAsync(true);
var service = new WatchItemService(itemRepository, messageBox.Object);
dbContext.AddRange(items);
dbContext.SaveChanges();
service.Add(addItem);
await service.AddAsync(addItem);

// Act
var actualItems = dbContext.WatchItem.ToList();
Expand Down
Loading

0 comments on commit 9a89a52

Please sign in to comment.