using AutoMapper; using MagicCarpetContracts.DataModels; using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.StoragesContracts; using MagicCarpetDatabase.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MagicCarpetDatabase.Implementations; internal class EmployeeStorageContract : IEmployeeStorageContract { private readonly MagicCarpetDbContext _dbContext; private readonly Mapper _mapper; public EmployeeStorageContract(MagicCarpetDbContext dbContext) { _dbContext = dbContext; var config = new MapperConfiguration(cfg => { cfg.CreateMap(); cfg.CreateMap(); }); _mapper = new Mapper(config); } public List GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null) { try { var query = _dbContext.Employees.AsQueryable(); if (onlyActive) { query = query.Where(x => !x.IsDeleted); } if (postId is not null) { query = query.Where(x => x.PostId == postId); } if (fromBirthDate is not null && toBirthDate is not null) { query = query.Where(x => x.BirthDate >= fromBirthDate && x.BirthDate <= toBirthDate); } if (fromEmploymentDate is not null && toEmploymentDate is not null) { query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate); } return [.. query.Select(x => _mapper.Map(x))]; } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public EmployeeDataModel? GetElementById(string id) { try { return _mapper.Map(GetEmployeeById(id)); } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public EmployeeDataModel? GetElementByFIO(string fio) { try { return _mapper.Map(_dbContext.Employees.FirstOrDefault(x => x.FIO == fio)); } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public EmployeeDataModel? GetElementByEmail(string email) { try { return _mapper.Map(_dbContext.Employees.FirstOrDefault(x => x.Email == email)); } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public void AddElement(EmployeeDataModel workerDataModel) { try { _dbContext.Employees.Add(_mapper.Map(workerDataModel)); _dbContext.SaveChanges(); } catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict") { _dbContext.ChangeTracker.Clear(); throw new ElementExistsException("Id", workerDataModel.Id); } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public void UpdElement(EmployeeDataModel workerDataModel) { try { var element = GetEmployeeById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id); _dbContext.Employees.Update(_mapper.Map(workerDataModel, element)); _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 = GetEmployeeById(id) ?? throw new ElementNotFoundException(id); element.IsDeleted = true; _dbContext.SaveChanges(); } catch (ElementNotFoundException) { _dbContext.ChangeTracker.Clear(); throw; } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } private Employee? GetEmployeeById(string id) => _dbContext.Employees.FirstOrDefault(x => x.Id == id && !x.IsDeleted); }