diff --git a/MagicCarpetProject/MagicCarpetContracts/Exceptions/ElementDeletedException.cs b/MagicCarpetProject/MagicCarpetContracts/Exceptions/ElementDeletedException.cs new file mode 100644 index 0000000..e06a7ed --- /dev/null +++ b/MagicCarpetProject/MagicCarpetContracts/Exceptions/ElementDeletedException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Exceptions; + +public class ElementDeletedException : Exception +{ + public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { } +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/ClientStorageContarct.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/ClientStorageContarct.cs new file mode 100644 index 0000000..cc824d5 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/ClientStorageContarct.cs @@ -0,0 +1,145 @@ +using AutoMapper; +using MagicCarpetContracts.DataModels; +using MagicCarpetContracts.Exceptions; +using MagicCarpetContracts.StoragesContracts; +using MagicCarpetDatabase.Models; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetDatabase.Implementations; + +internal class ClientStorageContarct : IClientStorageContract +{ + private readonly MagicCarpetDbContext _dbContext; + private readonly Mapper _mapper; + public ClientStorageContarct(MagicCarpetDbContext magicCarpetDbContext) + { + _dbContext = magicCarpetDbContext; + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + }); + _mapper = new Mapper(config); + } + public List GetList() + { + try + { + return [.. _dbContext.Clients.Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public ClientDataModel? GetElementById(string id) + { + try + { + return _mapper.Map(GetClientById(id)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public ClientDataModel? GetElementByFIO(string fio) + { + try + { + return _mapper.Map(_dbContext.Clients.FirstOrDefault(x => x.FIO == fio)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public ClientDataModel? GetElementByPhoneNumber(string phoneNumber) + { + try + { + return _mapper.Map(_dbContext.Clients.FirstOrDefault(x => x.PhoneNumber == phoneNumber)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public void AddElement(ClientDataModel clientDataModel) + { + try + { + _dbContext.Clients.Add(_mapper.Map(clientDataModel)); + _dbContext.SaveChanges(); + } + catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict") + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("Id", clientDataModel.Id); + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public void UpdElement(ClientDataModel clientDataModel) + { + try + { + var element = GetClientById(clientDataModel.Id) ?? throw new ElementNotFoundException(clientDataModel.Id); + _dbContext.Clients.Update(_mapper.Map(clientDataModel, element)); + _dbContext.SaveChanges(); + } + catch (ElementNotFoundException) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public void DelElement(string id) + { + try + { + var element = GetClientById(id) ?? throw new ElementNotFoundException(id); + _dbContext.Clients.Remove(element); + _dbContext.SaveChanges(); + } + catch (ElementNotFoundException) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + private Client? GetClientById(string id) => _dbContext.Clients.FirstOrDefault(x => x.Id == id); +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/EmployeeStorageContract.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/EmployeeStorageContract.cs new file mode 100644 index 0000000..f9f7cf4 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/EmployeeStorageContract.cs @@ -0,0 +1,161 @@ +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); + + +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/PostStorageContract.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/PostStorageContract.cs new file mode 100644 index 0000000..dc0ae24 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/PostStorageContract.cs @@ -0,0 +1,196 @@ +using AutoMapper; +using MagicCarpetContracts.DataModels; +using MagicCarpetContracts.Exceptions; +using MagicCarpetContracts.StoragesContracts; +using MagicCarpetDatabase.Models; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetDatabase.Implementations; + +internal class PostStorageContract : IPostStorageContract +{ + private readonly MagicCarpetDbContext _dbContext; + private readonly Mapper _mapper; + + public PostStorageContract(MagicCarpetDbContext dbContext) + { + _dbContext = dbContext; + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap() + .ForMember(x => x.Id, x => x.MapFrom(src => src.PostId)); + cfg.CreateMap() + .ForMember(x => x.Id, x => x.Ignore()) + .ForMember(x => x.PostId, x => x.MapFrom(src => src.Id)) + .ForMember(x => x.IsActual, x => x.MapFrom(src => true)) + .ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow)); + }); + _mapper = new Mapper(config); + } + + public List GetList(bool onlyActual = true) + { + try + { + var query = _dbContext.Posts.AsQueryable(); + if (onlyActual) + { + query = query.Where(x => x.IsActual); + } + return [.. query.Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public List GetPostWithHistory(string postId) + { + try + { + return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public PostDataModel? GetElementById(string id) + { + try + { + return _mapper.Map(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public PostDataModel? GetElementByName(string name) + { + try + { + return _mapper.Map(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void AddElement(PostDataModel postDataModel) + { + try + { + _dbContext.Posts.Add(_mapper.Map(postDataModel)); + _dbContext.SaveChanges(); + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("PostName", postDataModel.PostName); + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("PostId", postDataModel.Id); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void UpdElement(PostDataModel postDataModel) + { + try + { + var transaction = _dbContext.Database.BeginTransaction(); + try + { + var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id); + if (!element.IsActual) + { + throw new ElementDeletedException(postDataModel.Id); + } + element.IsActual = false; + _dbContext.SaveChanges(); + var newElement = _mapper.Map(postDataModel); + _dbContext.Posts.Add(newElement); + _dbContext.SaveChanges(); + transaction.Commit(); + } + catch + { + transaction.Rollback(); + throw; + } + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("PostName", postDataModel.PostName); + } + catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void DelElement(string id) + { + try + { + var element = GetPostById(id) ?? throw new ElementNotFoundException(id); + if (!element.IsActual) + { + throw new ElementDeletedException(id); + } + element.IsActual = false; + _dbContext.SaveChanges(); + } + catch + { + _dbContext.ChangeTracker.Clear(); + throw; + } + } + + public void ResElement(string id) + { + try + { + var element = GetPostById(id) ?? throw new ElementNotFoundException(id); + element.IsActual = true; + _dbContext.SaveChanges(); + } + catch + { + _dbContext.ChangeTracker.Clear(); + throw; + } + } + + private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id) + .OrderByDescending(x => x.ChangeDate).FirstOrDefault(); +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/SalaryStorageContract.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/SalaryStorageContract.cs new file mode 100644 index 0000000..8ead617 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/SalaryStorageContract.cs @@ -0,0 +1,62 @@ +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 SalaryStorageContract : ISalaryStorageContract +{ + private readonly MagicCarpetDbContext _dbContext; + private readonly Mapper _mapper; + + public SalaryStorageContract(MagicCarpetDbContext dbContext) + { + _dbContext = dbContext; + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap() + .ForMember(dest => dest.EmployeeSalary, opt => opt.MapFrom(src => src.Salary)); + }); + _mapper = new Mapper(config); + } + + public List GetList(DateTime startDate, DateTime endDate, string? employeeId = null) + { + try + { + var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate); + if (employeeId is not null) + { + query = query.Where(x => x.EmployeeId == employeeId); + } + return [.. query.Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void AddElement(SalaryDataModel salaryDataModel) + { + try + { + _dbContext.Salaries.Add(_mapper.Map(salaryDataModel)); + _dbContext.SaveChanges(); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/SaleStorageContract.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/SaleStorageContract.cs new file mode 100644 index 0000000..1828355 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/SaleStorageContract.cs @@ -0,0 +1,116 @@ +using AutoMapper; +using MagicCarpetContracts.DataModels; +using MagicCarpetContracts.Exceptions; +using MagicCarpetDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetDatabase.Implementations; + +internal class SaleStorageContract +{ + private readonly MagicCarpetDbContext _dbContext; + private readonly Mapper _mapper; + + public SaleStorageContract(MagicCarpetDbContext dbContext) + { + _dbContext = dbContext; + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap() + .ForMember(x => x.IsCancel, x => x.MapFrom(src => false)) + .ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours)); + }); + _mapper = new Mapper(config); + } + + public List GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? clientId = null, string? tourId = null) + { + try + { + var query = _dbContext.Sales.Include(x => x.SaleTours).AsQueryable(); + if (startDate is not null && endDate is not null) + { + query = query.Where(x => x.SaleDate >= startDate && x.SaleDate < endDate); + } + if (employeeId is not null) + { + query = query.Where(x => x.EmployeeId == employeeId); + } + if (clientId is not null) + { + query = query.Where(x => x.ClientId == clientId); + } + if (tourId is not null) + { + query = query.Where(x => x.SaleTours!.Any(y => y.TourId == tourId)); + } + return [.. query.Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public SaleDataModel? GetElementById(string id) + { + try + { + return _mapper.Map(GetSaleById(id)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void AddElement(SaleDataModel saleDataModel) + { + try + { + _dbContext.Sales.Add(_mapper.Map(saleDataModel)); + _dbContext.SaveChanges(); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void DelElement(string id) + { + try + { + var element = GetSaleById(id) ?? throw new ElementNotFoundException(id); + if (element.IsCancel) + { + throw new ElementDeletedException(id); + } + element.IsCancel = true; + _dbContext.SaveChanges(); + } + catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + private Sale? GetSaleById(string id) => _dbContext.Sales.FirstOrDefault(x => x.Id == id); +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/Implementations/TourStorageContract.cs b/MagicCarpetProject/MagicCarpetDatabase/Implementations/TourStorageContract.cs new file mode 100644 index 0000000..656c19c --- /dev/null +++ b/MagicCarpetProject/MagicCarpetDatabase/Implementations/TourStorageContract.cs @@ -0,0 +1,173 @@ +using AutoMapper; +using MagicCarpetContracts.DataModels; +using MagicCarpetContracts.Exceptions; +using MagicCarpetContracts.StoragesContracts; +using MagicCarpetDatabase.Models; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetDatabase.Implementations; + +internal class TourStorageContract : ITourStorageContract +{ + private readonly MagicCarpetDbContext _dbContext; + private readonly Mapper _mapper; + + public TourStorageContract(MagicCarpetDbContext dbContext) + { + _dbContext = dbContext; + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + }); + _mapper = new Mapper(config); + } + public List GetList() + { + try + { + var query = _dbContext.Tours.AsQueryable(); + return [.. query.Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public List GetHistoryByTourId(string productId) + { + try + { + return [.. _dbContext.TourHistories.Where(x => x.TourId == productId).OrderByDescending(x => x.ChangeDate) + .Select(x => _mapper.Map(x))]; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public TourDataModel? GetElementById(string id) + { + try + { + return _mapper.Map(GetTourById(id)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public TourDataModel? GetElementByName(string name) + { + try + { + return _mapper.Map(_dbContext.Tours.FirstOrDefault(x => x.TourName == name)); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void AddElement(TourDataModel productDataModel) + { + try + { + _dbContext.Tours.Add(_mapper.Map(productDataModel)); + _dbContext.SaveChanges(); + } + catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict") + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("Id", productDataModel.Id); + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("TourName", productDataModel.TourName); + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void UpdElement(TourDataModel productDataModel) + { + try + { + var transaction = _dbContext.Database.BeginTransaction(); + try + { + var element = GetTourById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id); + if (element.Price != productDataModel.Price) + { + _dbContext.TourHistories.Add(new TourHistory() { TourId = element.Id, OldPrice = element.Price }); + _dbContext.SaveChanges(); + } + _dbContext.Tours.Update(_mapper.Map(productDataModel, element)); + _dbContext.SaveChanges(); + transaction.Commit(); + } + catch + { + transaction.Rollback(); + throw; + } + } + catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" }) + { + _dbContext.ChangeTracker.Clear(); + throw new ElementExistsException("TourName", productDataModel.TourName); + } + catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + + public void DelElement(string id) + { + try + { + _dbContext.SaveChanges(); + } + catch (ElementNotFoundException ex) + { + _dbContext.ChangeTracker.Clear(); + throw; + } + catch (Exception ex) + { + _dbContext.ChangeTracker.Clear(); + throw new StorageException(ex); + } + } + public void ResElement(string id) + { + throw new NotImplementedException(); + } + + private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id); +} diff --git a/MagicCarpetProject/MagicCarpetDatabase/MagicCarpetDatabase.csproj b/MagicCarpetProject/MagicCarpetDatabase/MagicCarpetDatabase.csproj index f373481..8e44f86 100644 --- a/MagicCarpetProject/MagicCarpetDatabase/MagicCarpetDatabase.csproj +++ b/MagicCarpetProject/MagicCarpetDatabase/MagicCarpetDatabase.csproj @@ -7,6 +7,7 @@ + diff --git a/MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs b/MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs index 0d781f1..21b401f 100644 --- a/MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs +++ b/MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs @@ -10,7 +10,7 @@ namespace MagicCarpetDatabase.Models; internal class Post { - public required string Id { get; set; } + public required string Id { get; set; } = Guid.NewGuid().ToString(); public required string PostId { get; set; } public required string PostName { get; set; } public PostType PostType { get; set; } diff --git a/MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs b/MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs index 1cf6f6c..477d333 100644 --- a/MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs +++ b/MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs @@ -8,7 +8,7 @@ namespace MagicCarpetDatabase.Models; internal class TourHistory { - public required string Id { get; set; } = Guid.NewGuid().ToString(); + public string Id { get; set; } = Guid.NewGuid().ToString(); public required string TourId { get; set; } public double OldPrice { get; set; } public DateTime ChangeDate { get; set; }