Initial .NET 8 Web API with EF Core Db-First for Catalogs
Set up multi-project solution with DbFirst API, Application, Domain, and Infrastructure layers. Implemented database-first EF Core for the Catalog entity, including domain, DTOs, repository, service, and controller. Configured AutoMapper, DI, Swagger, and project settings. Added .gitattributes and initial configuration files.
This commit is contained in:
47
DbFirst.Application/Catalogs/CatalogService.cs
Normal file
47
DbFirst.Application/Catalogs/CatalogService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
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<CatalogDto>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var domainItems = await _repository.GetAllAsync(cancellationToken);
|
||||
return _mapper.Map<List<CatalogDto>>(domainItems);
|
||||
}
|
||||
|
||||
public async Task<CatalogDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var domainItem = await _repository.GetByIdAsync(id, cancellationToken);
|
||||
return domainItem == null ? null : _mapper.Map<CatalogDto>(domainItem);
|
||||
}
|
||||
|
||||
public async Task<CatalogDto> CreateAsync(CatalogDto dto, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var domainItem = _mapper.Map<Catalog>(dto);
|
||||
var created = await _repository.AddAsync(domainItem, cancellationToken);
|
||||
return _mapper.Map<CatalogDto>(created);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(int id, CatalogDto dto, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var domainItem = _mapper.Map<Catalog>(dto);
|
||||
return await _repository.UpdateAsync(id, domainItem, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _repository.DeleteAsync(id, cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user