using AutoMapper; using DbFirst.Domain.Repositories; using DbFirst.Domain.Entities; namespace DbFirst.Application.Catalogs; //TODO: create generic service to reduce code duplication /* Copilot's Response: A generic CRUD base service adds little value in your case: Pros: • Less boilerplate for simple entities without special logic. • Uniform CRUD signatures. Cons/Practical here: • Domain logic differs per entity(unique title check, setting audit fields, forbidding title changes, stored procs with output GUID). • Generic services tend to be diluted by virtual methods/hooks for special cases—ending up with per-entity overrides and little real gain. • With stored procedures and output parameters, the pattern doesn’t fit cleanly because operations aren’t symmetric (separate procs for insert/update/delete). Conclusion: For this solution a generic service would be more overhead than benefit. If you later have multiple very similar entities without special logic, you could consider a lightweight generic interface/base; for now, the specialized service implementation is cleaner. */ //TODO: implement CQRS pattern with MediatR 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> GetAllAsync(CancellationToken cancellationToken = default) { var items = await _repository.GetAllAsync(cancellationToken); return _mapper.Map>(items); } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { var item = await _repository.GetByIdAsync(id, cancellationToken); return item == null ? null : _mapper.Map(item); } public async Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) { var existing = await _repository.GetByTitleAsync(dto.CatTitle, cancellationToken); if (existing != null) { return null; } var entity = _mapper.Map(dto); entity.AddedWho = "system"; entity.AddedWhen = DateTime.UtcNow; entity.ChangedWho = "system"; entity.ChangedWhen = DateTime.UtcNow; var created = await _repository.InsertAsync(entity, cancellationToken); return _mapper.Map(created); } public async Task UpdateAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default) { var existing = await _repository.GetByIdAsync(id, cancellationToken); if (existing == null) { return null; } var entity = _mapper.Map(dto); entity.Guid = id; entity.CatTitle = existing.CatTitle; entity.AddedWho = existing.AddedWho; entity.AddedWhen = existing.AddedWhen; entity.ChangedWho = "system"; entity.ChangedWhen = DateTime.UtcNow; var updated = await _repository.UpdateAsync(id, entity, cancellationToken); return updated == null ? null : _mapper.Map(updated); } public async Task DeleteAsync(int id, CancellationToken cancellationToken = default) { return await _repository.DeleteAsync(id, cancellationToken); } }