Files
DbFirst/DbFirst.Application/Catalogs/CatalogService.cs
OlgunR 14e1cbc3b6 Refactor DTOs, add Blazor WASM frontend, enable CORS
- 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.
2026-01-13 10:15:01 +01:00

88 lines
3.2 KiB
C#

using AutoMapper;
using DbFirst.Domain.DomainEntities;
using DbFirst.Domain.Repositories;
namespace DbFirst.Application.Catalogs;
public class CatalogService : ICatalogService
{
private readonly ICatalogRepository _repository;
private readonly IMapper _mapper;
public CatalogService(ICatalogRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default)
{
var domainItems = await _repository.GetAllAsync(cancellationToken);
return _mapper.Map<List<CatalogReadDto>>(domainItems);
}
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
var domainItem = await _repository.GetByIdAsync(id, cancellationToken);
return domainItem == null ? null : _mapper.Map<CatalogReadDto>(domainItem);
}
public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
{
var domainItem = _mapper.Map<Catalog>(dto);
domainItem.AddedWho = "system";
domainItem.AddedWhen = DateTime.UtcNow;
domainItem.ChangedWho = null;
domainItem.ChangedWhen = null;
var created = await _repository.AddAsync(domainItem, cancellationToken);
return _mapper.Map<CatalogReadDto>(created);
}
public async Task<bool> UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
{
var existing = await _repository.GetByIdAsync(id, cancellationToken);
if (existing == null)
{
return false;
}
var domainItem = _mapper.Map<Catalog>(dto);
domainItem.Guid = id;
domainItem.AddedWho = existing.AddedWho;
domainItem.AddedWhen = existing.AddedWhen;
domainItem.ChangedWho = "system";
domainItem.ChangedWhen = DateTime.UtcNow;
return await _repository.UpdateAsync(id, domainItem, cancellationToken);
}
public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
{
var existing = await _repository.GetByIdAsync(id, cancellationToken);
if (existing == null)
{
return null;
}
var domainItem = _mapper.Map<Catalog>(dto);
domainItem.Guid = id;
domainItem.AddedWho = existing.AddedWho;
domainItem.AddedWhen = existing.AddedWhen;
domainItem.ChangedWho = "system";
domainItem.ChangedWhen = DateTime.UtcNow;
var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken);
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
}
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
{
return await _repository.DeleteAsync(id, cancellationToken);
}
public async Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default)
{
return await _repository.DeleteWithStoredProcedureAsync(id, cancellationToken);
}
}