Небольшой фикс

This commit is contained in:
2025-04-18 18:34:35 +04:00
parent 90ba84d636
commit b19bb03581
3 changed files with 1 additions and 126 deletions

View File

@@ -1,124 +0,0 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.StorageContracts;
using TwoFromTheCasketDatabase.Models;
namespace TwoFromTheCasketDatabase.Implementations;
public class WorkerComplitedWorkStorageContract : IWorkerComplitedWorkStorageContract
{
private readonly TwoFromTheCasketDbContext _dbContext;
private readonly Mapper _mapper;
public WorkerComplitedWorkStorageContract(TwoFromTheCasketDbContext context)
{
_dbContext = context;
var conf = new MapperConfiguration(cfg =>
{
cfg.CreateMap<WorkerComplitedWork, WorkerComplitedWorkDataModel>();
cfg.CreateMap<WorkerComplitedWorkDataModel, WorkerComplitedWork>();
});
_mapper = new Mapper(conf);
}
public List<WorkerComplitedWorkDataModel> GetList(string? workerId = null, string? complitedWorkId = null)
{
try
{
var query = _dbContext.WorkerComplitedWorks.AsQueryable();
if (!string.IsNullOrEmpty(workerId))
query = query.Where(x => x.WorkerId == workerId);
if (!string.IsNullOrEmpty(complitedWorkId))
query = query.Where(x => x.ComplitedWorkId == complitedWorkId);
return [.. query.Select(x => _mapper.Map<WorkerComplitedWorkDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public WorkerComplitedWorkDataModel? GetElementById(string id)
{
try
{
var element = _dbContext.WorkerComplitedWorks.FirstOrDefault(x => x.ComplitedWorkId == id);
return element != null ? _mapper.Map<WorkerComplitedWorkDataModel>(element) : null;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void AddElement(WorkerComplitedWorkDataModel workerComplitedWorkDataModel)
{
try
{
_dbContext.WorkerComplitedWorks.Add(_mapper.Map<WorkerComplitedWork>(workerComplitedWorkDataModel));
_dbContext.SaveChanges();
}
catch (DbUpdateException ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void UpdElement(WorkerComplitedWorkDataModel workerComplitedWorkDataModel)
{
try
{
var existing = _dbContext.WorkerComplitedWorks.FirstOrDefault(x =>
x.WorkerId == workerComplitedWorkDataModel.WorkerId && x.ComplitedWorkId == workerComplitedWorkDataModel.ComplitedWorkId)
?? throw new ElementNotFoundException($"{workerComplitedWorkDataModel.WorkerId}-{workerComplitedWorkDataModel.ComplitedWorkId}");
existing.NumberOfWorkingHours = workerComplitedWorkDataModel.NumberOfWorkingHours;
_dbContext.SaveChanges();
}
catch (ElementNotFoundException)
{
_dbContext.ChangeTracker.Clear();
throw;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void DelElement(string id)
{
try
{
var element = _dbContext.WorkerComplitedWorks.FirstOrDefault(x => x.ComplitedWorkId == id)
?? throw new ElementNotFoundException(id);
_dbContext.WorkerComplitedWorks.Remove(element);
_dbContext.SaveChanges();
}
catch (ElementNotFoundException)
{
_dbContext.ChangeTracker.Clear();
throw;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
}

View File

@@ -9,5 +9,5 @@ namespace TwoFromTheCasketTests.Infrastructure;
public class ConfigurationDatabaseTest : IConfigurationDatabase
{
public string ConnectionString => "Host=localhost;Port=5432;Database=TwoFromTheCasketDb;Username=postgres;Password=1234";
public string ConnectionString => "Host=localhost;Port=5432;Database=TwoFromTheCasketDb2;Username=postgres;Password=123456";
}

View File

@@ -60,7 +60,6 @@ public class Program
builder.Services.AddTransient<IComplitedWorkBusinessLogicContract, ComplitedWorkBusinessLogicContract>();
builder.Services.AddTransient<IRoomBusinessLogicContract, RoomBusinessLogicContract>();
builder.Services.AddTransient<IRoomHistoryBusinessLogicContract, RoomHistoryBusinessLogicContract>();
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
builder.Services.AddTransient<ISpecializationBusinessLogicContract, SpecializationBusinessLogicContract>();
builder.Services.AddTransient<IWorkerBusinessLogicContract, WorkerBusinessLogicContract>();