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:
12
DbFirst.Application/Catalogs/CatalogDto.cs
Normal file
12
DbFirst.Application/Catalogs/CatalogDto.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace DbFirst.Application.Catalogs;
|
||||
|
||||
public class CatalogDto
|
||||
{
|
||||
public int Guid { get; set; }
|
||||
public string CatTitle { get; set; } = null!;
|
||||
public string CatString { get; set; } = null!;
|
||||
public string AddedWho { get; set; } = null!;
|
||||
public DateTime AddedWhen { get; set; }
|
||||
public string? ChangedWho { get; set; }
|
||||
public DateTime? ChangedWhen { get; set; }
|
||||
}
|
||||
12
DbFirst.Application/Catalogs/CatalogProfile.cs
Normal file
12
DbFirst.Application/Catalogs/CatalogProfile.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
|
||||
namespace DbFirst.Application.Catalogs;
|
||||
|
||||
public class CatalogProfile : Profile
|
||||
{
|
||||
public CatalogProfile()
|
||||
{
|
||||
CreateMap<Catalog, CatalogDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
10
DbFirst.Application/Catalogs/ICatalogService.cs
Normal file
10
DbFirst.Application/Catalogs/ICatalogService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace DbFirst.Application.Catalogs;
|
||||
|
||||
public interface ICatalogService
|
||||
{
|
||||
Task<List<CatalogDto>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||
Task<CatalogDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<CatalogDto> CreateAsync(CatalogDto dto, CancellationToken cancellationToken = default);
|
||||
Task<bool> UpdateAsync(int id, CatalogDto dto, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
17
DbFirst.Application/DbFirst.Application.csproj
Normal file
17
DbFirst.Application/DbFirst.Application.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DbFirst.Domain\DbFirst.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user