Skip to content

Commit

Permalink
Merge pull request #15 from neozhu/Mapperly
Browse files Browse the repository at this point in the history
Riok.Mapperly instead of Mapster
  • Loading branch information
neozhu authored Jul 29, 2024
2 parents 6673e70 + 3f26cf0 commit cc52be5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/BlazorPocket.Shared/BlazorPocket.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<MapperlyAbstractionsScope>runtime</MapperlyAbstractionsScope>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Mapster" Version="7.4.1-pre01" />
<PackageReference Include="Riok.Mapperly" Version="3.6.0" ExcludeAssets="runtime" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="9.0.0-preview.6.24328.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0-preview.6.24328.4" />
<ProjectReference Include="..\PocketBaseClient.BlazorPocket\PocketBaseClient.BlazorPocket.csproj" />
Expand Down
42 changes: 39 additions & 3 deletions src/BlazorPocket.Shared/Dto/ProductDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BlazorPocket.Shared.Resources;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using static PocketBaseClient.BlazorPocket.Models.Product;

namespace BlazorPocket.Shared.Dto;
/// <summary>
Expand Down Expand Up @@ -54,7 +54,7 @@ public class ProductDto
/// </summary>
[Display(Name = nameof(SharedResources.Unit), ResourceType = typeof(SharedResources))]
[Required(ErrorMessageResourceType = typeof(SharedResources), ErrorMessageResourceName = nameof(SharedResources.UnitRequired))]
public UnitEnum Unit { get; set; }
public UnitDto Unit { get; set; }

/// <summary>
/// Gets or sets the price of the product.
Expand All @@ -69,7 +69,43 @@ public class ProductDto
/// </summary>
[Display(Name = nameof(SharedResources.Currency), ResourceType = typeof(SharedResources))]
[Required(ErrorMessageResourceType = typeof(SharedResources), ErrorMessageResourceName = nameof(SharedResources.CurrencyRequired))]
public CurrencyEnum? Currency { get; set; }
public CurrencyDto? Currency { get; set; }

#endregion Field Properties



public enum CurrencyDto
{
[Description("USD")]
USD,

[Description("EUR")]
EUR,

[Description("CNY")]
CNY,


}

public enum UnitDto
{
[Description("Packages")]
Packages,

[Description("Box")]
Box,

[Description("Pallet")]
Pallet,

[Description("Carton")]
Carton,

[Description("Drum")]
Drum,


}
}
22 changes: 22 additions & 0 deletions src/BlazorPocket.Shared/Services/ProjectMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BlazorPocket.Shared.Dto;
using PocketBaseClient.BlazorPocket.Models;
using Riok.Mapperly.Abstractions;


namespace BlazorPocket.Shared.Services;

[Mapper]
public static partial class ProjectMapper
{
public static partial ProductDto MapProduct(Product product);
[MapperIgnoreSource(nameof(ProductDto.Id))]
[MapperIgnoreSource(nameof(ProductDto.Created))]
[MapperIgnoreSource(nameof(ProductDto.Updated))]
public static partial Product MapProductDto(ProductDto productDto);
[MapperIgnoreSource(nameof(ProductDto.Id))]
[MapperIgnoreSource(nameof(ProductDto.Created))]
[MapperIgnoreSource(nameof(ProductDto.Updated))]
public static partial void ApplyUpdate(ProductDto productDto,Product product);
public static partial IEnumerable<ProductDto> ProjectToDto(IEnumerable<Product> q);
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using PocketBaseClient.BlazorPocket.Models
@using static BlazorPocket.Shared.Dto.ProductDto
@using static PocketBaseClient.BlazorPocket.Models.Product

<MudDialog>
Expand All @@ -19,15 +20,15 @@
@bind-Value="Model.Description" For="@(() => Model.Description)" />
</MudItem>
<MudItem xs="4">
<MudEnumSelect TEnum="UnitEnum" Label="@Model.GetDisplayName(x=>x.Unit)"
<MudEnumSelect TEnum="UnitDto" Label="@Model.GetDisplayName(x=>x.Unit)"
@bind-Value="Model.Unit" For="@(() => Model.Unit)"></MudEnumSelect>
</MudItem>
<MudItem xs="4">
<MudNumericField Label="@Model.GetDisplayName(x=>x.Price)"
@bind-Value="Model.Price" For="@(() => Model.Price)" />
</MudItem>
<MudItem xs="4">
<MudEnumSelect TEnum="Nullable<CurrencyEnum>" Label="@Model.GetDisplayName(x=>x.Currency)"
<MudEnumSelect TEnum="Nullable<CurrencyDto>" Label="@Model.GetDisplayName(x=>x.Currency)"
@bind-Value="Model.Currency" For="@(() => Model.Currency)"
>
</MudEnumSelect>
Expand Down
9 changes: 6 additions & 3 deletions src/BlazorPocket.WebAssembly/Pages/Products/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
</PropertyColumn>
<PropertyColumn Property="x => x.Created" Title="@dto.GetDisplayName(x=>x.Created)" Format="F" />
</Columns>
<PagerContent>
<MudDataGridPager T="ProductDto" />
</PagerContent>
</MudDataGrid>


Expand All @@ -64,7 +67,7 @@
selectedItems = new();
loading = true;
var result = await query.Filter(x => x.Name.Contains(keyword).Or(x.Description.Contains(keyword))).GetPagedItemsAsync(state.Page, state.PageSize);
return new GridData<ProductDto> { TotalItems = result.TotalItems, Items = result.Items.Adapt<List<ProductDto>>() ?? Enumerable.Empty<ProductDto>() };
return new GridData<ProductDto> { TotalItems = result.TotalItems, Items = ProjectMapper.ProjectToDto(result.Items ?? Enumerable.Empty<Product>()) };
}
finally
{
Expand Down Expand Up @@ -94,7 +97,7 @@
await ShowEditDialog(dto, L["Create a product"],
async (p) =>
{
var product = p.Adapt<Product>();
var product = ProjectMapper.MapProductDto(p);
await product.SaveAsync();
await dataGrid.ReloadServerData();
}
Expand All @@ -109,7 +112,7 @@
var product = await query.GetByIdAsync(p?.Id);
if (product is not null)
{
p.Adapt(product);
ProjectMapper.ApplyUpdate(p,product);
await product.SaveAsync();
await dataGrid.ReloadServerData();
}
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorPocket.WebAssembly/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@using BlazorPocket.WebAssembly.Layout
@using BlazorPocket.WebAssembly.Components
@using Microsoft.Extensions.Localization
@using Mapster
@using PocketBaseClient.BlazorPocket.Models
@using BlazorPocket.WebAssembly.Services.JsInterop
@inject IJSRuntime JS
Expand All @@ -30,3 +29,4 @@
@inject IStringLocalizer<App> L
@inject ILogger<App> Logger


0 comments on commit cc52be5

Please sign in to comment.