|
|
|
|
@@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|