Refactor to use VwmyCatalog as primary catalog entity
Replaced all usage of the Catalog entity with VwmyCatalog across application, domain, and infrastructure layers. Updated AutoMapper profiles, repository interfaces, and service logic to use VwmyCatalog. Removed the obsolete Catalog entity and related mapping profiles. Moved VwmyCatalog to the Domain.Entities namespace and cleaned up project structure. This change simplifies the domain model and eliminates redundant entity definitions.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Domain.Entities;
|
||||
|
||||
namespace DbFirst.Application.Catalogs;
|
||||
|
||||
@@ -7,7 +7,7 @@ public class CatalogProfile : Profile
|
||||
{
|
||||
public CatalogProfile()
|
||||
{
|
||||
CreateMap<Catalog, CatalogReadDto>().ReverseMap();
|
||||
CreateMap<CatalogWriteDto, Catalog>();
|
||||
CreateMap<VwmyCatalog, CatalogReadDto>().ReverseMap();
|
||||
CreateMap<CatalogWriteDto, VwmyCatalog>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using DbFirst.Domain.Entities;
|
||||
|
||||
namespace DbFirst.Application.Catalogs;
|
||||
|
||||
@@ -17,25 +17,25 @@ public class CatalogService : ICatalogService
|
||||
|
||||
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var domainItems = await _repository.GetAllAsync(cancellationToken);
|
||||
return _mapper.Map<List<CatalogReadDto>>(domainItems);
|
||||
var items = await _repository.GetAllAsync(cancellationToken);
|
||||
return _mapper.Map<List<CatalogReadDto>>(items);
|
||||
}
|
||||
|
||||
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);
|
||||
var item = await _repository.GetByIdAsync(id, cancellationToken);
|
||||
return item == null ? null : _mapper.Map<CatalogReadDto>(item);
|
||||
}
|
||||
|
||||
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 = "system";
|
||||
domainItem.ChangedWhen = DateTime.UtcNow;
|
||||
var entity = _mapper.Map<VwmyCatalog>(dto);
|
||||
entity.AddedWho = "system";
|
||||
entity.AddedWhen = DateTime.UtcNow;
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
|
||||
var created = await _repository.AddAsync(domainItem, cancellationToken);
|
||||
var created = await _repository.AddAsync(entity, cancellationToken);
|
||||
return _mapper.Map<CatalogReadDto>(created);
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ public class CatalogService : ICatalogService
|
||||
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);
|
||||
var entity = _mapper.Map<VwmyCatalog>(dto);
|
||||
entity.Guid = id;
|
||||
entity.AddedWho = existing.AddedWho;
|
||||
entity.AddedWhen = existing.AddedWhen;
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
return await _repository.UpdateAsync(id, entity, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
|
||||
@@ -64,14 +64,14 @@ public class CatalogService : ICatalogService
|
||||
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 entity = _mapper.Map<VwmyCatalog>(dto);
|
||||
entity.Guid = id;
|
||||
entity.AddedWho = existing.AddedWho;
|
||||
entity.AddedWhen = existing.AddedWhen;
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
|
||||
var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken);
|
||||
var updated = await _repository.UpdateWithStoredProcedureAsync(entity, cancellationToken);
|
||||
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DomainEntities\" />
|
||||
<Folder Include="Repositories\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace DbFirst.Domain.DomainEntities;
|
||||
|
||||
public class Catalog
|
||||
{
|
||||
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; }
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DbFirst.Infrastructure.ScaffoldEntities;
|
||||
namespace DbFirst.Domain.Entities;
|
||||
|
||||
public partial class VwmyCatalog
|
||||
{
|
||||
@@ -1,14 +1,14 @@
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Domain.Entities;
|
||||
|
||||
namespace DbFirst.Domain.Repositories;
|
||||
|
||||
public interface ICatalogRepository
|
||||
{
|
||||
Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||
Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||
Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using DbFirst.Infrastructure.ScaffoldEntities;
|
||||
using DbFirst.Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DbFirst.Infrastructure;
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Infrastructure.ScaffoldEntities;
|
||||
|
||||
namespace DbFirst.Infrastructure.Mappings;
|
||||
|
||||
public class CatalogInfrastructureProfile : Profile
|
||||
{
|
||||
public CatalogInfrastructureProfile()
|
||||
{
|
||||
CreateMap<VwmyCatalog, Catalog>();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.DomainEntities;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using DbFirst.Infrastructure.ScaffoldEntities;
|
||||
using DbFirst.Domain.Entities;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Data;
|
||||
@@ -11,27 +9,23 @@ namespace DbFirst.Infrastructure.Repositories;
|
||||
public class CatalogRepository : ICatalogRepository
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public CatalogRepository(ApplicationDbContext db, IMapper mapper)
|
||||
public CatalogRepository(ApplicationDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
public async Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
||||
return _mapper.Map<List<Catalog>>(viewRows);
|
||||
return await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||
public async Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
|
||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
||||
public async Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
|
||||
if (created == null)
|
||||
@@ -42,7 +36,7 @@ public class CatalogRepository : ICatalogRepository
|
||||
return created;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default)
|
||||
public async Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
|
||||
if (!existing)
|
||||
@@ -55,7 +49,7 @@ public class CatalogRepository : ICatalogRepository
|
||||
return updated != null;
|
||||
}
|
||||
|
||||
public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
||||
public async Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (catalog.Guid == 0)
|
||||
{
|
||||
@@ -93,7 +87,7 @@ public class CatalogRepository : ICatalogRepository
|
||||
return await DeleteAsync(id, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Catalog?> UpsertWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken)
|
||||
private async Task<VwmyCatalog?> UpsertWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken)
|
||||
{
|
||||
var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
|
||||
{
|
||||
@@ -116,7 +110,6 @@ public class CatalogRepository : ICatalogRepository
|
||||
}
|
||||
|
||||
var guid = (int)guidParam.Value;
|
||||
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
||||
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
|
||||
return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user