ProductList
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
using Bimix.Application.DTOModels;
|
||||||
|
using Bimix.Application.DTOModels.Common;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Bimix.Infrastructure.Data;
|
using Bimix.Infrastructure.Data;
|
||||||
using Bimix.Domain.Entities;
|
using Bimix.Domain.Entities;
|
||||||
@@ -12,8 +14,59 @@ public class ProductsController(BimixDbContext context) : ControllerBase
|
|||||||
private readonly BimixDbContext _context = context;
|
private readonly BimixDbContext _context = context;
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
|
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts([FromQuery] ProductFilterRequest request)
|
||||||
{
|
{
|
||||||
return await _context.Products.ToListAsync();
|
var query = _context.Products.AsQueryable();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||||
|
{
|
||||||
|
var searchTerm = request.Search.ToLower();
|
||||||
|
query = query.Where(x =>
|
||||||
|
x.Name.ToLower().Contains(searchTerm) ||
|
||||||
|
(x.Code != null && x.Code.ToLower().Contains(searchTerm)) ||
|
||||||
|
(x.Ean != null && x.Ean.ToLower().Contains(searchTerm))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Name.ToLower().Contains(request.Name.ToLower()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Code))
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Code != null && x.Code.ToLower().Contains(request.Code.ToLower()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Ean))
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Ean != null && x.Ean.ToLower().Contains(request.Ean.ToLower()));
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalCount = await query.CountAsync();
|
||||||
|
|
||||||
|
var items = await query
|
||||||
|
.OrderBy(x => x.Name)
|
||||||
|
.Skip((request.Page -1) * request.PageSize)
|
||||||
|
.Take(request.PageSize)
|
||||||
|
.Select(x => new ProductDto
|
||||||
|
{
|
||||||
|
Id = x.Id,
|
||||||
|
Name = x.Name,
|
||||||
|
Code = x.Code ?? string.Empty,
|
||||||
|
Ean = x.Ean ?? string.Empty,
|
||||||
|
StockAddresses = x.StockAddresses ?? string.Empty,
|
||||||
|
CreatedAt = x.CreatedAt,
|
||||||
|
UpdatedAt = x.UpdatedAt
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return Ok(new PagedResult<ProductDto>
|
||||||
|
{
|
||||||
|
Items = items,
|
||||||
|
TotalCount = totalCount,
|
||||||
|
Page = request.Page,
|
||||||
|
PageSize = request.PageSize,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "swagger",
|
"launchUrl": "swagger",
|
||||||
"applicationUrl": "https://localhost:7142;http://localhost:5142",
|
"applicationUrl": "http://localhost:7142;http://0.0.0.0:7142",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,6 @@
|
|||||||
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
|
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="DTO\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
|||||||
12
Bimix.Application/DTOModels/Common/PagedResult.cs
Normal file
12
Bimix.Application/DTOModels/Common/PagedResult.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Bimix.Application.DTOModels.Common;
|
||||||
|
|
||||||
|
public class PagedResult<T>
|
||||||
|
{
|
||||||
|
public List<T> Items { get; set; } = new();
|
||||||
|
public int TotalCount { get; set; }
|
||||||
|
public int PageSize { get; set; }
|
||||||
|
public int Page { get; set; }
|
||||||
|
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
|
||||||
|
public bool HasPreviousPage => Page > 1;
|
||||||
|
public bool HasNextPage => Page < TotalPages;
|
||||||
|
}
|
||||||
22
Bimix.Application/DTOModels/ProductDto.cs
Normal file
22
Bimix.Application/DTOModels/ProductDto.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
namespace Bimix.Application.DTOModels;
|
||||||
|
|
||||||
|
public class ProductDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public string Code { get; set; } = string.Empty;
|
||||||
|
public string Ean { get; set; } = string.Empty;
|
||||||
|
public string StockAddresses { get; set; } = string.Empty;
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
public DateTime UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProductFilterRequest
|
||||||
|
{
|
||||||
|
public string? Search { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public string? Code { get; set; }
|
||||||
|
public string? Ean { get; set; }
|
||||||
|
public int Page { get; set; } = 1;
|
||||||
|
public int PageSize { get; set; } = 20;
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Reflection.Metadata.Ecma335;
|
using System.Reflection.Metadata.Ecma335;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Web;
|
||||||
using Bimix.Domain.Entities;
|
using Bimix.Domain.Entities;
|
||||||
using Bimix.Infrastructure.Data;
|
using Bimix.Infrastructure.Data;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@@ -8,17 +10,23 @@ namespace Bimix.Infrastructure.Sync;
|
|||||||
|
|
||||||
public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfiguration configuration)
|
public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient = httpClient;
|
/// <summary>
|
||||||
private readonly BimixDbContext _db = db;
|
/// Dekoduje encje HTML w ciągu znaków (np. " na ")
|
||||||
private readonly IConfiguration _configuration = configuration;
|
/// </summary>
|
||||||
|
private string DecodeHtmlEntities(string text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
return text;
|
||||||
|
|
||||||
|
return HttpUtility.HtmlDecode(text);
|
||||||
|
}
|
||||||
public async Task RunAsync()
|
public async Task RunAsync()
|
||||||
{
|
{
|
||||||
var apiKey = _configuration["E5_CRM:ApiKey"];
|
var apiKey = configuration["E5_CRM:ApiKey"];
|
||||||
var syncState = _db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
|
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
|
||||||
|
|
||||||
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
|
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
|
||||||
var response = await _httpClient.GetStringAsync(url);
|
var response = await httpClient.GetStringAsync(url);
|
||||||
|
|
||||||
var products = JsonSerializer.Deserialize<List<JsonElement>>(response);
|
var products = JsonSerializer.Deserialize<List<JsonElement>>(response);
|
||||||
if (products == null) return;
|
if (products == null) return;
|
||||||
@@ -28,10 +36,10 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
|||||||
foreach (var p in products)
|
foreach (var p in products)
|
||||||
{
|
{
|
||||||
var idStr = p.GetProperty("id").GetString() ?? "";
|
var idStr = p.GetProperty("id").GetString() ?? "";
|
||||||
var name = p.GetProperty("name").GetString() ?? "";
|
var name = DecodeHtmlEntities(p.GetProperty("name").GetString() ?? "");
|
||||||
var code = p.GetProperty("code").GetString() ?? "";
|
var code = DecodeHtmlEntities(p.GetProperty("code").GetString() ?? "");
|
||||||
var stockAddresses = p.GetProperty("stock_addresses").GetString() ?? "";
|
var stockAddresses = DecodeHtmlEntities(p.GetProperty("stock_addresses").GetString() ?? "");
|
||||||
var ean = p.GetProperty("ean").GetString() ?? "";
|
var ean = DecodeHtmlEntities(p.GetProperty("ean").GetString() ?? "");
|
||||||
|
|
||||||
if (!Guid.TryParse(idStr, out Guid id))
|
if (!Guid.TryParse(idStr, out Guid id))
|
||||||
{
|
{
|
||||||
@@ -39,7 +47,7 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var existing = _db.Products.FirstOrDefault(x => x.Id == id);
|
var existing = db.Products.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
if (existing == null)
|
if (existing == null)
|
||||||
{
|
{
|
||||||
@@ -53,7 +61,7 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
|||||||
CreatedAt = DateTime.UtcNow,
|
CreatedAt = DateTime.UtcNow,
|
||||||
UpdatedAt = DateTime.UtcNow
|
UpdatedAt = DateTime.UtcNow
|
||||||
};
|
};
|
||||||
_db.Products.Add(product);
|
db.Products.Add(product);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -65,17 +73,17 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
|
|||||||
|
|
||||||
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||||
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
|
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
|
||||||
await _httpClient.GetAsync(updateUrl);
|
await httpClient.GetAsync(updateUrl);
|
||||||
}
|
}
|
||||||
syncState.LastSynced = now;
|
syncState.LastSynced = now;
|
||||||
if (_db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
|
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
|
||||||
{
|
{
|
||||||
_db.SyncStates.Add(syncState);
|
db.SyncStates.Add(syncState);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_db.SyncStates.Update(syncState);
|
db.SyncStates.Update(syncState);
|
||||||
}
|
}
|
||||||
await _db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +66,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)"/>
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1"/>
|
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1"/>
|
||||||
<PackageReference Include="MudBlazor" Version="8.8.0"/>
|
<PackageReference Include="MudBlazor" Version="8.8.0"/>
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Bimix.UI.Shared.Extensions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using MudBlazor.Services;
|
using MudBlazor.Services;
|
||||||
|
|
||||||
namespace Bimix.UI.Mobile;
|
namespace Bimix.UI.Mobile;
|
||||||
@@ -17,7 +18,8 @@ public static class MauiProgram
|
|||||||
builder.Services.AddMudServices();
|
builder.Services.AddMudServices();
|
||||||
|
|
||||||
var baseUrl = GetApiBaseUrl();
|
var baseUrl = GetApiBaseUrl();
|
||||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseUrl) });
|
builder.Services.AddSharedServices(baseUrl);
|
||||||
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
builder.Services.AddBlazorWebViewDeveloperTools();
|
builder.Services.AddBlazorWebViewDeveloperTools();
|
||||||
@@ -30,9 +32,9 @@ public static class MauiProgram
|
|||||||
{
|
{
|
||||||
#if IOS
|
#if IOS
|
||||||
// iOS symulator - użyj swojego IP
|
// iOS symulator - użyj swojego IP
|
||||||
return "http://192.168.1.100:5015/"; // Zastąp swoim IP
|
return "http://192.168.13.44:7142/"; // Zastąp swoim IP
|
||||||
#else
|
#else
|
||||||
return "https://localhost:7015/";
|
return "https://localhost:7142/";
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.17" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.17" />
|
||||||
<PackageReference Include="MudBlazor" Version="8.8.0" />
|
<PackageReference Include="MudBlazor" Version="8.8.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -21,6 +23,10 @@
|
|||||||
<ProjectReference Include="..\Bimix.Application\Bimix.Application.csproj" />
|
<ProjectReference Include="..\Bimix.Application\Bimix.Application.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<AdditionalFiles Include="Pages\ProductList.razor" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="wwwroot\" />
|
<Folder Include="wwwroot\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
@page "/products"
|
|
||||||
@using MudBlazor
|
|
||||||
|
|
||||||
<MudText Typo="Typo.h4">Produkty</MudText>
|
|
||||||
<p>Lista produktów zostanie tutaj zaimplementowana</p>
|
|
||||||
111
Bimix.UI.Shared/Components/ProductListComponent.razor
Normal file
111
Bimix.UI.Shared/Components/ProductListComponent.razor
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
@using MudBlazor.Internal
|
||||||
|
<MudText Typo="Typo.h4" Class="mb-4">Lista Produktów</MudText>
|
||||||
|
|
||||||
|
<MudExpansionPanels Class="mb-4">
|
||||||
|
<MudExpansionPanel Icon="@Icons.Material.Filled.FilterList"
|
||||||
|
Text="Filtry"
|
||||||
|
Expanded="true">
|
||||||
|
<MudGrid>
|
||||||
|
<MudItem xs="12" sm="6" md="4">
|
||||||
|
<MudTextField @bind-Value="filterRequest.Search"
|
||||||
|
Label="Szukaj"
|
||||||
|
Placeholder="Nazwa, Kod, EAN..."
|
||||||
|
Immediate="true"
|
||||||
|
DebounceInterval="500"
|
||||||
|
OnDebounceIntervalElapsed="SearchProducts"
|
||||||
|
Clearable="true"/>
|
||||||
|
|
||||||
|
</MudItem>
|
||||||
|
|
||||||
|
<MudItem xs="12" sm="6" md="4">
|
||||||
|
<MudTextField @bind-Value="filterRequest.Name"
|
||||||
|
Label="Nazwa produktu"
|
||||||
|
Immediate="true"
|
||||||
|
DebounceInterval="500"
|
||||||
|
OnDebounceIntervalElapsed="SearchProducts"
|
||||||
|
Clearable="true"/>
|
||||||
|
</MudItem>
|
||||||
|
|
||||||
|
<MudItem xs="12" sm="6" md="4">
|
||||||
|
<MudTextField @bind-Value="filterRequest.Code"
|
||||||
|
Label="Kod produktu"
|
||||||
|
Immediate="true"
|
||||||
|
DebounceInterval="500"
|
||||||
|
OnDebounceIntervalElapsed="SearchProducts"
|
||||||
|
Clearable="true"/>
|
||||||
|
</MudItem>
|
||||||
|
|
||||||
|
<MudItem xs="12" sm="6" md="4">
|
||||||
|
<MudTextField @bind-Value="filterRequest.Ean"
|
||||||
|
Label="EAN"
|
||||||
|
Immediate="true"
|
||||||
|
DebounceInterval="500"
|
||||||
|
OnDebounceIntervalElapsed="SearchProducts"
|
||||||
|
Clearable="true">
|
||||||
|
</MudTextField>
|
||||||
|
</MudItem>
|
||||||
|
|
||||||
|
<MudItem xs="12" sm="6" md="4">
|
||||||
|
<MudButton Variant="Variant.Outlined"
|
||||||
|
OnClick="ClearFilters"
|
||||||
|
StartIcon="Icons.Material.Filled.Clear">
|
||||||
|
Wyczyść filtry
|
||||||
|
</MudButton>
|
||||||
|
</MudItem>
|
||||||
|
</MudGrid>
|
||||||
|
</MudExpansionPanel>
|
||||||
|
</MudExpansionPanels>
|
||||||
|
|
||||||
|
<MudDivider Class="my-4"></MudDivider>
|
||||||
|
|
||||||
|
<MudTable Items="products.Items"
|
||||||
|
Dense="true"
|
||||||
|
Hover="true"
|
||||||
|
Loading="isLoading"
|
||||||
|
LoadingProgressColor="Color.Info">
|
||||||
|
<HeaderContent>
|
||||||
|
<MudTh>Nazwa</MudTh>
|
||||||
|
<MudTh>Kod</MudTh>
|
||||||
|
<MudTh>EAN</MudTh>
|
||||||
|
<MudTh>Akcje</MudTh>
|
||||||
|
</HeaderContent>
|
||||||
|
<RowTemplate>
|
||||||
|
<MudTd DataLabel="Nazwa">@context.Name</MudTd>
|
||||||
|
<MudTd DataLabel="Kod">@context.Code.Trim()</MudTd>
|
||||||
|
<MudTd DataLabel="EAN"></MudTd>
|
||||||
|
<MudTd DataLabel="Akcje">
|
||||||
|
<MudIconButton Icon="@Icons.Material.Filled.Edit"
|
||||||
|
Size="Size.Small"
|
||||||
|
OnClick="() => EditProduct(context.Id)"/>
|
||||||
|
<MudIconButton Icon="@Icons.Material.Filled.Delete"
|
||||||
|
Size="Size.Small"
|
||||||
|
Color="Color.Error"
|
||||||
|
OnClick="() => DeleteProduct(context.Id)"/>
|
||||||
|
</MudTd>
|
||||||
|
</RowTemplate>
|
||||||
|
<NoRecordsContent>
|
||||||
|
<MudText>Brak produktów do wyświetlenia</MudText>
|
||||||
|
</NoRecordsContent>
|
||||||
|
<LoadingContent>
|
||||||
|
Ładowanie...
|
||||||
|
</LoadingContent>
|
||||||
|
</MudTable>
|
||||||
|
|
||||||
|
@if (products.TotalCount > 0)
|
||||||
|
{
|
||||||
|
<MudGrid Class="mt-4" AlignItems="Center.Center">
|
||||||
|
<MudItem xs="12" sm="6">
|
||||||
|
<MudText Typo="Typo.body2">
|
||||||
|
Wyniki @((products.Page - 1) * products.PageSize + 1) - @Math.Min(products.Page * products.PageSize, products.TotalCount)
|
||||||
|
z @products.TotalCount
|
||||||
|
</MudText>
|
||||||
|
</MudItem>
|
||||||
|
<MudItem xs="12" sm="6" Class="d-flex justify-end">
|
||||||
|
<MudPagination Count="products.TotalPages"
|
||||||
|
Selected="products.Page"
|
||||||
|
SelectedChanged="OnPageChanged"
|
||||||
|
ShowFirstButton="true"
|
||||||
|
ShowLastButton="true"/>
|
||||||
|
</MudItem>
|
||||||
|
</MudGrid>
|
||||||
|
}
|
||||||
77
Bimix.UI.Shared/Components/ProductListComponent.razor.cs
Normal file
77
Bimix.UI.Shared/Components/ProductListComponent.razor.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using Bimix.Application.DTOModels;
|
||||||
|
using Bimix.Application.DTOModels.Common;
|
||||||
|
using Bimix.Domain.Entities;
|
||||||
|
using Bimix.UI.Shared.Services;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using Microsoft.AspNetCore.Components.Web;
|
||||||
|
using Microsoft.JSInterop;
|
||||||
|
|
||||||
|
namespace Bimix.UI.Shared.Components;
|
||||||
|
|
||||||
|
public partial class ProductListComponent : ComponentBase
|
||||||
|
{
|
||||||
|
[Inject] private ProductService ProductService { get; set; } = default!;
|
||||||
|
|
||||||
|
private PagedResult<ProductDto> products = new();
|
||||||
|
private ProductFilterRequest filterRequest = new();
|
||||||
|
private bool isLoading = false;
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
await LoadProducts();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task StartBarcodeScanner()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task LoadProducts()
|
||||||
|
{
|
||||||
|
isLoading = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
products = await ProductService.GetProductsAsync(filterRequest);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Loading products failed: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isLoading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SearchProducts()
|
||||||
|
{
|
||||||
|
filterRequest.Page = 1;
|
||||||
|
await LoadProducts();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OnPageChanged(int page)
|
||||||
|
{
|
||||||
|
filterRequest.Page = page;
|
||||||
|
await LoadProducts();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ClearFilters()
|
||||||
|
{
|
||||||
|
filterRequest = new ProductFilterRequest();
|
||||||
|
await LoadProducts();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EditProduct(Guid productId)
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
Console.WriteLine($"Edytuj produkt: {productId}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DeleteProduct(Guid productId)
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
Console.WriteLine($"Usuń produkt: {productId}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
17
Bimix.UI.Shared/Extensions/ServiceCollectionExtensions.cs
Normal file
17
Bimix.UI.Shared/Extensions/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Bimix.UI.Shared.Services;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Bimix.UI.Shared.Extensions;
|
||||||
|
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddSharedServices(this IServiceCollection services, string apiBaseUrl)
|
||||||
|
{
|
||||||
|
services.AddHttpClient<ProductService>(client =>
|
||||||
|
{
|
||||||
|
client.BaseAddress = new Uri(apiBaseUrl);
|
||||||
|
});
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
</MudDrawer>
|
</MudDrawer>
|
||||||
|
|
||||||
<MudMainContent>
|
<MudMainContent>
|
||||||
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="my-4">
|
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge" Class="my-4">
|
||||||
@Body
|
@Body
|
||||||
</MudContainer>
|
</MudContainer>
|
||||||
</MudMainContent>
|
</MudMainContent>
|
||||||
|
|||||||
7
Bimix.UI.Shared/Pages/ProductListPage.razor
Normal file
7
Bimix.UI.Shared/Pages/ProductListPage.razor
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
@page "/products"
|
||||||
|
|
||||||
|
<PageTitle>Produkty</PageTitle>
|
||||||
|
|
||||||
|
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge">
|
||||||
|
<ProductListComponent />
|
||||||
|
</MudContainer>
|
||||||
52
Bimix.UI.Shared/Services/ProductService.cs
Normal file
52
Bimix.UI.Shared/Services/ProductService.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using Bimix.Application.DTOModels;
|
||||||
|
using Bimix.Application.DTOModels.Common;
|
||||||
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
|
|
||||||
|
namespace Bimix.UI.Shared.Services;
|
||||||
|
|
||||||
|
public class ProductService(HttpClient httpClient)
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient = httpClient;
|
||||||
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
public async Task<PagedResult<ProductDto>> GetProductsAsync(ProductFilterRequest request)
|
||||||
|
{
|
||||||
|
var queryParams = new Dictionary<string, string?>
|
||||||
|
{
|
||||||
|
["page"] = request.Page.ToString(),
|
||||||
|
["pageSize"] = request.PageSize.ToString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||||
|
{
|
||||||
|
queryParams["search"] = request.Search;
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||||
|
{
|
||||||
|
queryParams["name"] = request.Name;
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Code))
|
||||||
|
{
|
||||||
|
queryParams["code"] = request.Code;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(request.Ean))
|
||||||
|
{
|
||||||
|
queryParams["ean"] = request.Ean;
|
||||||
|
}
|
||||||
|
|
||||||
|
var uri = QueryHelpers.AddQueryString("api/products", queryParams);
|
||||||
|
var response = await _httpClient.GetAsync(uri);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadAsStringAsync();
|
||||||
|
var result = JsonSerializer.Deserialize<PagedResult<ProductDto>>(json, _jsonOptions);
|
||||||
|
|
||||||
|
return result ?? new PagedResult<ProductDto>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Bimix.UI.Shared;
|
using Bimix.UI.Shared;
|
||||||
|
using Bimix.UI.Shared.Extensions;
|
||||||
using Bimix.UI.Web.Components;
|
using Bimix.UI.Web.Components;
|
||||||
using MudBlazor.Services;
|
using MudBlazor.Services;
|
||||||
|
|
||||||
@@ -7,10 +8,9 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
builder.Services.AddMudServices();
|
builder.Services.AddMudServices();
|
||||||
builder.Services.AddHttpClient("BimixAPI", client =>
|
|
||||||
{
|
builder.Services.AddSharedServices("http://localhost:7142");
|
||||||
client.BaseAddress = new Uri("https://localhost:7071");
|
|
||||||
});
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user