Skip to content

Commit 79793f0

Browse files
Added a view small helpers.
1 parent cf7c48c commit 79793f0

File tree

9 files changed

+72
-5
lines changed

9 files changed

+72
-5
lines changed

src/DotNetElements.AppFramework.Abstractions/Entity/EntityBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public abstract class Entity<TKey> : Entity, IEntity<TKey>
99
{
1010
public TKey Id { get; protected set; } = default!;
1111

12+
public bool WithId(TKey id) => Id.Equals(id);
13+
1214
internal void SetId(TKey id)
1315
{
1416
// todo check if this is needed

src/DotNetElements.AppFramework.Abstractions/Entity/EntityHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using DotNetElements.AppFramework.Abstractions.Model;
23

34
namespace DotNetElements.AppFramework.Abstractions.Entity;
45

@@ -61,7 +62,8 @@ public static void UpdateRelatedEntities<TEntity, TKey>(List<TEntity> oldCollect
6162
where TKey : notnull, IEquatable<TKey>
6263
{
6364
oldCollection.RemoveAll(existingEntity => !newIdsCollection.Any(newId => newId.Equals(existingEntity.Id)));
64-
var addedIds = newIdsCollection.Where(newId => !oldCollection.Any(existingEntity => existingEntity.Id.Equals(newId)));
65+
66+
IEnumerable<TKey> addedIds = newIdsCollection.Where(newId => !oldCollection.Any(existingEntity => existingEntity.Id.Equals(newId)));
6567

6668
foreach (TKey newId in addedIds)
6769
oldCollection.Add(entityUpdateHelper.AttachById<TEntity, TKey>(newId));

src/DotNetElements.AppFramework.Abstractions/Model/ModelBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace DotNetElements.AppFramework.Abstractions.Model;
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.Serialization;
3+
4+
namespace DotNetElements.AppFramework.Abstractions.Model;
25

36
public interface IModel<TKey> : IHasKey<TKey>
47
where TKey : notnull, IEquatable<TKey>;
@@ -32,6 +35,12 @@ public abstract class EditModel<TModel, TKey> : IEditModel<TModel, TKey>
3235
where TKey : notnull, IEquatable<TKey>
3336
{
3437
public required TKey Id { get; init; }
38+
39+
protected static TSelf CreateNew<TSelf>()
40+
where TSelf : EditModel<TModel, TKey>
41+
{
42+
return Activator.CreateInstance<TSelf>();
43+
}
3544
}
3645

3746
public abstract class VersionedEditModel<TModel, TKey> : EditModel<TModel, TKey>, IHasVersion
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@using DotNetElements.AppFramework.MudBlazorExtensions.Util
2+
3+
@* todo implement option to show retry button and action to execute on retry *@
4+
5+
@if (LoadingState is LoadingState.Loading)
6+
{
7+
<MudProgressLinear Indeterminate="true" Color="Color.Primary" Class="my-2" />
8+
}
9+
else if (LoadingState is LoadingState.Error)
10+
{
11+
<MudAlert Severity="Severity.Error" Class="my-2">
12+
An error occurred while loading the content.
13+
</MudAlert>
14+
}
15+
else if (LoadingState is LoadingState.Success)
16+
{
17+
@ChildContent
18+
}
19+
20+
@code
21+
{
22+
[Parameter, EditorRequired]
23+
public RenderFragment ChildContent { get; set; } = default!;
24+
25+
[Parameter, EditorRequired]
26+
public LoadingState LoadingState { get; set; }
27+
}

src/DotNetElements.AppFramework.MudBlazorExtensions/Extensions/DialogServiceExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ public static async Task<bool> ShowConfirmDeleteDialog(this IDialogService dialo
5555

5656
return result?.Data is true;
5757
}
58+
59+
// todo add overload with list of error messages
60+
public static Task ShowValidationErrorMessage(this IDialogService dialogService)
61+
{
62+
return dialogService.ShowMessageBox("Validation Error", "Please fix all validation errors.");
63+
}
5864
}

src/DotNetElements.AppFramework.MudBlazorExtensions/Extensions/SnackbarExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public static void NotifyFailureFetchData(this ISnackbar snackbar)
3030
snackbar.NotifyFailure(DefaultMessageFailureFetch);
3131
}
3232

33+
public static void NotifyMissingQueryParameter(this ISnackbar snackbar, params Span<string?> parameterNames)
34+
{
35+
snackbar.NotifyFailure($"Missing query parameters: {string.Join(", ", parameterNames)}");
36+
}
37+
3338
public static void NotifySuccessCreateEntry(this ISnackbar snackbar)
3439
{
3540
snackbar.NotifySuccess(DefaultMessageSuccessCreate);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace DotNetElements.AppFramework.MudBlazorExtensions.Util;
2+
3+
public enum LoadingState
4+
{
5+
None,
6+
Loading,
7+
Success,
8+
Error
9+
}

src/DotNetElements.AppFramework.MudBlazorExtensions/Util/ModelValidationWrapper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ public bool Validate()
2727
return IsValid;
2828
}
2929

30-
public bool UpdateIsModified()
30+
public bool UpdateIsModified(bool isExternalModified = false)
3131
{
32+
if (isExternalModified)
33+
{
34+
IsModified = true;
35+
36+
return IsModified;
37+
}
38+
3239
IsModified = EditContext.IsModified();
3340

3441
return IsModified;

src/DotNetElements.AppFramework/EfCore/ModuleService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ protected ModuleService(TDbContext dbContext, ICurrentUserProvider currentUserPr
2222
}
2323

2424
// this should be high level method
25-
protected async Task<int> AttachAndSaveChangesAsync<TEntity>(TEntity entity)
25+
protected async Task AttachAndSaveChangesAsync<TEntity>(TEntity entity)
2626
where TEntity : class
2727
{
2828
DbContext.Set<TEntity>().Attach(entity);
2929

30-
return await DbContext.SaveChangesAsync();
30+
await DbContext.SaveChangesAsync();
3131
}
3232

3333
// this should be high level method

0 commit comments

Comments
 (0)