- Split `CatalogDto` into `CatalogReadDto` and `CatalogWriteDto` for clear separation of read/write operations in both backend and frontend. - Updated API controllers, services, and AutoMapper profiles to use new DTOs; ensured audit fields are set in service layer. - Enabled CORS in the API project to support Blazor WASM frontend. - Added new Blazor WebAssembly project (`DbFirst.BlazorWasm`) with catalog management UI, API client, Bootstrap v5.1.0 styling, and configuration-driven API base URL. - Included `bootstrap.min.css` and its source map for frontend styling and easier debugging. - Updated solution file to include new project and support multiple build configurations. - Result: improved API design, clean DTO separation, and a modern interactive frontend for catalog management.
50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using System.Net.Http.Json;
|
|
using DbFirst.BlazorWasm.Models;
|
|
|
|
namespace DbFirst.BlazorWasm.Services;
|
|
|
|
public class CatalogApiClient
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private const string Endpoint = "api/catalogs";
|
|
|
|
public CatalogApiClient(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
public async Task<List<CatalogReadDto>> GetAllAsync()
|
|
{
|
|
var result = await _httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint);
|
|
return result ?? new List<CatalogReadDto>();
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> GetByIdAsync(int id)
|
|
{
|
|
return await _httpClient.GetFromJsonAsync<CatalogReadDto>($"{Endpoint}/{id}");
|
|
}
|
|
|
|
public async Task<CatalogReadDto?> CreateAsync(CatalogWriteDto dto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync(Endpoint, dto);
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<CatalogReadDto>();
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(int id, CatalogWriteDto dto)
|
|
{
|
|
var response = await _httpClient.PutAsJsonAsync($"{Endpoint}/{id}", dto);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id)
|
|
{
|
|
var response = await _httpClient.DeleteAsync($"{Endpoint}/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
}
|