-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some updated to mudblazor crud components and ModelBase.
- Loading branch information
1 parent
5b2145c
commit 3d63a85
Showing
11 changed files
with
302 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace DotNetElements.Web.MudBlazor; | ||
|
||
public class CrudOptions<TModel> | ||
{ | ||
public string BaseEndpointUri { get; private init; } | ||
|
||
private string getAllEndpoint = null!; | ||
public string GetAllEndpoint | ||
{ | ||
get => getAllEndpoint; | ||
init | ||
{ | ||
getAllEndpoint = $"{BaseEndpointUri.TrimEnd('/')}"; | ||
|
||
if (!string.IsNullOrEmpty(value)) | ||
getAllEndpoint += $"/{value.TrimStart('/')}"; | ||
} | ||
} | ||
|
||
private string getAllWithDetailsEndpoint = null!; | ||
public string GetAllWithDetailsEndpoint | ||
{ | ||
get => getAllWithDetailsEndpoint; | ||
init | ||
{ | ||
getAllWithDetailsEndpoint = $"{BaseEndpointUri.TrimEnd('/')}"; | ||
|
||
if (!string.IsNullOrEmpty(value)) | ||
getAllWithDetailsEndpoint += $"/{value.TrimStart('/')}"; | ||
} | ||
} | ||
|
||
public string GetDetailsEndpoint(string id) => $"{BaseEndpointUri.TrimEnd('/')}/{id}/details"; | ||
|
||
public CrudOptions(string baseEndpointUri) | ||
{ | ||
BaseEndpointUri = baseEndpointUri; | ||
GetAllEndpoint = ""; | ||
GetAllWithDetailsEndpoint = ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
namespace DotNetElements.Web.MudBlazor; | ||
|
||
public interface ICrudService<TKey, TModel, TDetails, TEditModel> | ||
where TKey : notnull, IEquatable<TKey> | ||
where TModel : IModel<TKey> | ||
where TDetails : ModelDetails | ||
where TEditModel : IMapFromModel<TEditModel, TModel>, ICreateNew<TEditModel> | ||
{ | ||
Task<Result<TModel>> CreateEntryAsync(TEditModel editModel); | ||
Task<Result> DeleteEntryAsync(TModel model); | ||
Task<Result<List<TModel>>> GetAllEntriesAsync(); | ||
Task<Result<List<ModelWithDetails<TModel, TDetails>>>> GetAllEntriesWithDetailsAsync(); | ||
Task<Result> GetEntryDetailsAsync(ModelWithDetails<TModel, TDetails> modelWithDetails); | ||
Task<Result<TModel>> UpdateEntryAsync(TEditModel editModel); | ||
} | ||
|
||
public class CrudService<TKey, TModel, TDetails, TEditModel> : ICrudService<TKey, TModel, TDetails, TEditModel> where TKey : notnull, IEquatable<TKey> | ||
where TModel : IModel<TKey> | ||
where TDetails : ModelDetails | ||
where TEditModel : IMapFromModel<TEditModel, TModel>, ICreateNew<TEditModel> | ||
{ | ||
protected readonly ISnackbar Snackbar; | ||
protected readonly HttpClient HttpClient; | ||
protected readonly CrudOptions<TModel> Options; | ||
|
||
public CrudService(ISnackbar snackbar, HttpClient httpClient, CrudOptions<TModel> options) | ||
{ | ||
Snackbar = snackbar; | ||
HttpClient = httpClient; | ||
Options = options; | ||
} | ||
|
||
public virtual async Task<Result<TModel>> CreateEntryAsync(TEditModel editModel) | ||
{ | ||
Result<TModel> result = await HttpClient.PutAsJsonWithResultAsync<TEditModel, TModel>(Options.BaseEndpointUri, editModel); | ||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (result.IsOk) | ||
{ | ||
Snackbar.Add("Entry saved", Severity.Success); | ||
} | ||
else | ||
{ | ||
Snackbar.Add("Failed to save entry", Severity.Error); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public virtual async Task<Result<TModel>> UpdateEntryAsync(TEditModel editModel) | ||
{ | ||
Result<TModel> result = await HttpClient.PostAsJsonWithResultAsync<TEditModel, TModel>(Options.BaseEndpointUri, editModel); | ||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (result.IsOk) | ||
{ | ||
Snackbar.Add("Changes saved", Severity.Success); | ||
} | ||
else | ||
{ | ||
Snackbar.Add("Failed to save changes", Severity.Error); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public virtual async Task<Result> DeleteEntryAsync(TModel model) | ||
{ | ||
Result result = await HttpClient.DeleteWithResultAsync(Options.BaseEndpointUri, model); | ||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (result.IsOk) | ||
{ | ||
Snackbar.Add("Entry deleted", Severity.Success); | ||
} | ||
else | ||
{ | ||
Snackbar.Add("Failed to delete entry", Severity.Error); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public virtual async Task<Result> GetEntryDetailsAsync(ModelWithDetails<TModel, TDetails> modelWithDetails) | ||
{ | ||
Result<TDetails> detailsResult = await HttpClient.GetFromJsonWithResultAsync<TDetails>(Options.GetDetailsEndpoint(modelWithDetails.Value.Id.ToString())); | ||
Check warning on line 92 in src/DotNetElements.Web.MudBlazor/CrudService.cs GitHub Actions / build-and-test-ubuntu-latest
|
||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (detailsResult.IsFail) | ||
{ | ||
Snackbar.Add($"Failed to fetch details.\n{detailsResult.ErrorMessage}", Severity.Error); | ||
return detailsResult; | ||
} | ||
|
||
modelWithDetails.Details = detailsResult.Value; | ||
|
||
return detailsResult; | ||
} | ||
|
||
public virtual async Task<Result<List<TModel>>> GetAllEntriesAsync() | ||
{ | ||
Result<List<TModel>> result = await HttpClient.GetFromJsonWithResultAsync<List<TModel>>(Options.GetAllEndpoint); | ||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (result.IsFail) | ||
{ | ||
Snackbar.Add("Failed to fetch entries from server", Severity.Error); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public virtual async Task<Result<List<ModelWithDetails<TModel, TDetails>>>> GetAllEntriesWithDetailsAsync() | ||
{ | ||
Result<List<ModelWithDetails<TModel, TDetails>>> result = await HttpClient.GetModelWithDetailsListFromJsonAsync<TModel, TDetails>(Options.GetAllEndpoint); | ||
|
||
// todo add logging | ||
// todo wrap Snackbar call in bool option NotifyUser | ||
// todo add function OnDeleteSuccess | ||
if (result.IsFail) | ||
{ | ||
Snackbar.Add("Failed to fetch entries from server", Severity.Error); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/DotNetElements.Web.MudBlazor/DneDataAnnotationsValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace DotNetElements.Web.MudBlazor; | ||
|
||
public class DneDataAnnotationsValidator : DataAnnotationsValidator | ||
{ | ||
#if DEBUG | ||
[CascadingParameter] EditContext? DebugEditContext { get; set; } | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
if (DebugEditContext is null) | ||
{ | ||
throw new InvalidOperationException($"{nameof(DneDataAnnotationsValidator)} requires a cascading " + | ||
$"parameter of type {nameof(EditContext)}. For example, you can use {nameof(DneDataAnnotationsValidator)} " + | ||
$"inside an EditForm."); | ||
} | ||
|
||
DebugEditContext.OnValidationRequested += DebugEditContext_OnValidationRequested; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing && DebugEditContext is not null) | ||
DebugEditContext.OnValidationRequested -= DebugEditContext_OnValidationRequested; | ||
} | ||
|
||
private void DebugEditContext_OnValidationRequested(object? sender, ValidationRequestedEventArgs e) | ||
{ | ||
DebugEditContext.LogDebugInfo(); // Debug only | ||
} | ||
#endif | ||
} |
Oops, something went wrong.