forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
Compare commits
14 Commits
lab02_Buis
...
lab03_Stor
| Author | SHA1 | Date | |
|---|---|---|---|
| 45a4bb91ae | |||
| a43f4b2ac1 | |||
| f8c393fcc9 | |||
| b9bb08da63 | |||
| 7b15c9c56e | |||
| a83fdd892d | |||
| 86aefae2d4 | |||
| 087a21da11 | |||
| c12efe7f92 | |||
| 18df50575b | |||
| 56af2f0e0a | |||
| 66fe491cf6 | |||
| 599475e22f | |||
| 651c170ce2 |
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
|
|||||||
namespace MagicCarpetContracts.DataModels;
|
namespace MagicCarpetContracts.DataModels;
|
||||||
|
|
||||||
public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType,
|
public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType,
|
||||||
double discount, bool isCancel, List<SaleTourDataModel> tours) : IValidation
|
double discount, bool isCancel, List<SaleTourDataModel> saleTours) : IValidation
|
||||||
{
|
{
|
||||||
public string Id { get; private set; } = id;
|
public string Id { get; private set; } = id;
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ public class SaleDataModel(string id, string employeeId, string? clientId, doubl
|
|||||||
|
|
||||||
public bool IsCancel { get; private set; } = isCancel;
|
public bool IsCancel { get; private set; } = isCancel;
|
||||||
|
|
||||||
public List<SaleTourDataModel> Tours { get; private set; } = tours;
|
public List<SaleTourDataModel> Tours { get; private set; } = saleTours;
|
||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MagicCarpetContracts.DataModels;
|
namespace MagicCarpetContracts.DataModels;
|
||||||
|
|
||||||
public class SaleTourDataModel(string saleId, string cocktailId, int count) : IValidation
|
public class SaleTourDataModel(string saleId, string tourId, int count) : IValidation
|
||||||
{
|
{
|
||||||
public string SaleId { get; private set; } = saleId;
|
public string SaleId { get; private set; } = saleId;
|
||||||
|
|
||||||
public string TourId { get; private set; } = cocktailId;
|
public string TourId { get; private set; } = tourId;
|
||||||
|
|
||||||
public int Count { get; private set; } = count;
|
public int Count { get; private set; } = count;
|
||||||
|
|
||||||
|
|||||||
@@ -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})") { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetContracts.Infrastructure;
|
||||||
|
|
||||||
|
public interface IConfigurationDatabase
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
}
|
||||||
@@ -16,5 +16,4 @@ public interface ITourStorageContract
|
|||||||
void AddElement(TourDataModel tourDataModel);
|
void AddElement(TourDataModel tourDataModel);
|
||||||
void UpdElement(TourDataModel tourDataModel);
|
void UpdElement(TourDataModel tourDataModel);
|
||||||
void DelElement(string id);
|
void DelElement(string id);
|
||||||
void ResElement(string id);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,144 @@
|
|||||||
|
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.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<ClientDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Clients.Select(x => _mapper.Map<ClientDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ClientDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ClientDataModel>(GetClientById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ClientDataModel? GetElementByFIO(string fio)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ClientDataModel>(_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<ClientDataModel>(_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<Client>(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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
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.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<EmployeeDataModel> 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<EmployeeDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<EmployeeDataModel>(GetEmployeeById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeDataModel? GetElementByFIO(string fio)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<EmployeeDataModel>(_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<EmployeeDataModel>(_dbContext.Employees.FirstOrDefault(x => x.Email == email));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(EmployeeDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Employees.Add(_mapper.Map<Employee>(employeeDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", employeeDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(EmployeeDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetEmployeeById(employeeDataModel.Id) ?? throw new ElementNotFoundException(employeeDataModel.Id);
|
||||||
|
_dbContext.Employees.Update(_mapper.Map(employeeDataModel, 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);
|
||||||
|
}
|
||||||
@@ -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<Post, PostDataModel>()
|
||||||
|
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
||||||
|
cfg.CreateMap<PostDataModel, Post>()
|
||||||
|
.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<PostDataModel> GetList(bool onlyActual = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Posts.AsQueryable();
|
||||||
|
if (onlyActual)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.IsActual);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostDataModel> GetPostWithHistory(string postId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<PostDataModel>(_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<PostDataModel>(_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<Post>(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<Post>(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();
|
||||||
|
}
|
||||||
@@ -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<Salary, SalaryDataModel>();
|
||||||
|
cfg.CreateMap<SalaryDataModel, Salary>()
|
||||||
|
.ForMember(dest => dest.EmployeeSalary, opt => opt.MapFrom(src => src.Salary));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SalaryDataModel> 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<SalaryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SalaryDataModel salaryDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetContracts.StoragesContracts;
|
||||||
|
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 : ISaleStorageContract
|
||||||
|
{
|
||||||
|
private readonly MagicCarpetDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public SaleStorageContract(MagicCarpetDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<SaleTour, SaleTourDataModel>();
|
||||||
|
cfg.CreateMap<SaleTourDataModel, SaleTour>();
|
||||||
|
cfg.CreateMap<Sale, SaleDataModel>();
|
||||||
|
cfg.CreateMap<SaleDataModel, Sale>()
|
||||||
|
.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<SaleDataModel> 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<SaleDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SaleDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<SaleDataModel>(GetSaleById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SaleDataModel saleDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Sales.Add(_mapper.Map<Sale>(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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
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<Tour, TourDataModel>()
|
||||||
|
.ConstructUsing(src => new TourDataModel(src.Id, src.TourName, src.TourCountry, src.Price, src.Type));
|
||||||
|
cfg.CreateMap<TourDataModel, Tour>();
|
||||||
|
cfg.CreateMap<TourHistory, TourHistoryDataModel>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<TourDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Tours.Select(x => _mapper.Map<TourDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TourHistoryDataModel> GetHistoryByTourId(string tourId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.TourHistories.Where(x => x.TourId == tourId).OrderByDescending(x => x.ChangeDate).Select(x => _mapper.Map<TourHistoryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<TourDataModel>(GetTourById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<TourDataModel>(_dbContext.Tours.FirstOrDefault(x => x.TourName == name));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(TourDataModel tourDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Tours.Add(_mapper.Map<Tour>(tourDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", tourDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(TourDataModel tourDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetTourById(tourDataModel.Id) ?? throw new ElementNotFoundException(tourDataModel.Id);
|
||||||
|
_dbContext.Tours.Update(_mapper.Map(tourDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Tours.Remove(element);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="MagicCarpetTests" />
|
||||||
|
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using MagicCarpetContracts.Infrastructure;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase;
|
||||||
|
|
||||||
|
internal class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase) : DbContext
|
||||||
|
{
|
||||||
|
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
|
||||||
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Client>().HasIndex(x => x.PhoneNumber).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<Tour>().HasIndex(x => x.TourName).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<Post>()
|
||||||
|
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||||
|
.IsUnique()
|
||||||
|
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||||
|
|
||||||
|
modelBuilder.Entity<Post>()
|
||||||
|
.HasIndex(e => new { e.PostId, e.IsActual })
|
||||||
|
.IsUnique()
|
||||||
|
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||||
|
|
||||||
|
modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId });
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<Client> Clients { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Post> Posts { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Tour> Tours { get; set; }
|
||||||
|
|
||||||
|
public DbSet<TourHistory> TourHistories { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Salary> Salaries { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Sale> Sales { get; set; }
|
||||||
|
|
||||||
|
public DbSet<SaleTour> SaleTours { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Employee> Employees { get; set; }
|
||||||
|
}
|
||||||
24
MagicCarpetProject/MagicCarpetDatabase/Models/Client.cs
Normal file
24
MagicCarpetProject/MagicCarpetDatabase/Models/Client.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
[AutoMap(typeof(ClientDataModel), ReverseMap = true)]
|
||||||
|
internal class Client
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
|
||||||
|
public required string FIO { get; set; }
|
||||||
|
|
||||||
|
public required string PhoneNumber { get; set; }
|
||||||
|
|
||||||
|
public double DiscountSize { get; set; }
|
||||||
|
[ForeignKey("ClientId")]
|
||||||
|
public List<Sale>? Sales { get; set; }
|
||||||
|
}
|
||||||
32
MagicCarpetProject/MagicCarpetDatabase/Models/Employee.cs
Normal file
32
MagicCarpetProject/MagicCarpetDatabase/Models/Employee.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
[AutoMap(typeof(EmployeeDataModel), ReverseMap = true)]
|
||||||
|
internal class Employee
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
|
||||||
|
public required string FIO { get; set; }
|
||||||
|
|
||||||
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
public string PostId { get; set; }
|
||||||
|
|
||||||
|
public DateTime BirthDate { get; set; }
|
||||||
|
|
||||||
|
public DateTime EmploymentDate { get; set; }
|
||||||
|
|
||||||
|
public bool IsDeleted { get; set; }
|
||||||
|
[ForeignKey("EmployeeId")]
|
||||||
|
public List<Salary>? Salaries { get; set; }
|
||||||
|
[ForeignKey("EmployeeId")]
|
||||||
|
public List<Sale>? Sales { get; set; }
|
||||||
|
}
|
||||||
20
MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs
Normal file
20
MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class Post
|
||||||
|
{
|
||||||
|
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; }
|
||||||
|
public double Salary { get; set; }
|
||||||
|
public bool IsActual { get; set; }
|
||||||
|
public DateTime ChangeDate { get; set; }
|
||||||
|
}
|
||||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/Salary.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/Salary.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class Salary
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
public required string EmployeeId { get; set; }
|
||||||
|
public DateTime SalaryDate { get; set; }
|
||||||
|
public double EmployeeSalary { get; set; }
|
||||||
|
public Employee? Employee { get; set; }
|
||||||
|
}
|
||||||
32
MagicCarpetProject/MagicCarpetDatabase/Models/Sale.cs
Normal file
32
MagicCarpetProject/MagicCarpetDatabase/Models/Sale.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class Sale
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
public required string EmployeeId { get; set; }
|
||||||
|
|
||||||
|
public string? ClientId { get; set; }
|
||||||
|
|
||||||
|
public DateTime SaleDate { get; set; }
|
||||||
|
public double Sum { get; set; }
|
||||||
|
|
||||||
|
public DiscountType DiscountType { get; set; }
|
||||||
|
|
||||||
|
public double Discount { get; set; }
|
||||||
|
|
||||||
|
public bool IsCancel { get; set; }
|
||||||
|
public Employee? Employee { get; set; }
|
||||||
|
public Client? Client { get; set; }
|
||||||
|
[ForeignKey("SaleId")]
|
||||||
|
public List<SaleTour>? SaleTours { get; set; }
|
||||||
|
}
|
||||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/SaleTour.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/SaleTour.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class SaleTour
|
||||||
|
{
|
||||||
|
public required string SaleId { get; set; }
|
||||||
|
|
||||||
|
public required string TourId { get; set; }
|
||||||
|
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
23
MagicCarpetProject/MagicCarpetDatabase/Models/Tour.cs
Normal file
23
MagicCarpetProject/MagicCarpetDatabase/Models/Tour.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class Tour
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string TourName { get; set; }
|
||||||
|
public string? TourCountry { get; set; }
|
||||||
|
public double Price { get; set; }
|
||||||
|
public required TourType Type { get; set; }
|
||||||
|
[ForeignKey("TourId")]
|
||||||
|
public List<SaleTour>? SaleTours { get; set; }
|
||||||
|
[ForeignKey("TourId")]
|
||||||
|
public List<TourHistory>? TourHistories { get; set; }
|
||||||
|
}
|
||||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetDatabase.Models;
|
||||||
|
|
||||||
|
internal class TourHistory
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
public required string TourId { get; set; }
|
||||||
|
public double OldPrice { get; set; }
|
||||||
|
public DateTime ChangeDate { get; set; }
|
||||||
|
public Tour? Tour { get; set; }
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetTests", "MagicCa
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetBusinessLogic", "MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj", "{688F9182-851F-4CF8-97CD-9B6F1E43D758}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetBusinessLogic", "MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj", "{688F9182-851F-4CF8-97CD-9B6F1E43D758}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetDatabase", "MagicCarpetDatabase\MagicCarpetDatabase.csproj", "{C69B43B2-8680-42D7-A111-306E2E8225FE}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -27,6 +29,10 @@ Global
|
|||||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.Build.0 = Release|Any CPU
|
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MagicCarpetContracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.Infrastructure;
|
||||||
|
|
||||||
|
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||||
|
{
|
||||||
|
public string ConnectionString => "Host=127.0.0.1;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;";
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj" />
|
<ProjectReference Include="..\MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
||||||
|
<ProjectReference Include="..\MagicCarpetDatabase\MagicCarpetDatabase.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using MagicCarpetTests.Infrastructure;
|
||||||
|
using MagicCarpetDatabase;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
internal abstract class BaseStorageContractTest
|
||||||
|
{
|
||||||
|
protected MagicCarpetDbContext MagicCarpetDbContext { get; private set; }
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext = new MagicCarpetDbContext(new ConfigurationDatabaseTest());
|
||||||
|
|
||||||
|
MagicCarpetDbContext.Database.EnsureDeleted();
|
||||||
|
MagicCarpetDbContext.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
[OneTimeTearDown]
|
||||||
|
public void OneTimeTearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.EnsureDeleted();
|
||||||
|
MagicCarpetDbContext.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetContracts.StoragesContracts;
|
||||||
|
using MagicCarpetDatabase.Implementations;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class ClientStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private ClientStorageContarct _clientStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_clientStorageContract = new ClientStorageContarct(MagicCarpetDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+5-555-555-55-55");
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+6-666-666-66-66");
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+7-777-777-77-77");
|
||||||
|
var list = _clientStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(x => x.Id == client.Id), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _clientStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_clientStorageContract.GetElementById(client.Id), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _clientStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_clientStorageContract.GetElementByFIO(client.FIO), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _clientStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByPhoneNumber_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_clientStorageContract.GetElementByPhoneNumber(client.PhoneNumber), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByPhoneNumber_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _clientStorageContract.GetElementByPhoneNumber("+8-888-888-88-88"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var client = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
_clientStorageContract.AddElement(client);
|
||||||
|
AssertElement(GetClientFromDatabase(client.Id), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||||
|
InsertClientToDatabaseAndReturn(client.Id);
|
||||||
|
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||||
|
{
|
||||||
|
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
|
||||||
|
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||||
|
InsertClientToDatabaseAndReturn(client.Id);
|
||||||
|
_clientStorageContract.UpdElement(client);
|
||||||
|
AssertElement(GetClientFromDatabase(client.Id), client);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _clientStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||||
|
{
|
||||||
|
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||||
|
InsertClientToDatabaseAndReturn(client.Id, phoneNumber: "+7-777-777-77-77");
|
||||||
|
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
|
||||||
|
Assert.That(() => _clientStorageContract.UpdElement(client), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
_clientStorageContract.DelElement(client.Id);
|
||||||
|
var element = GetClientFromDatabase(client.Id);
|
||||||
|
Assert.That(element, Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenHaveSalesByThisBuyer_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
var employeeId = Guid.NewGuid().ToString();
|
||||||
|
MagicCarpetDbContext.Employees.Add(new Employee() { Id = employeeId, FIO = "test", PostId = Guid.NewGuid().ToString(), Email = "abc@gmail.com" });
|
||||||
|
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||||
|
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
var salesBeforeDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
|
||||||
|
_clientStorageContract.DelElement(client.Id);
|
||||||
|
var element = GetClientFromDatabase(client.Id);
|
||||||
|
var salesAfterDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(element, Is.Null);
|
||||||
|
Assert.That(salesBeforeDelete, Has.Length.EqualTo(2));
|
||||||
|
Assert.That(salesAfterDelete, Is.Empty);
|
||||||
|
Assert.That(MagicCarpetDbContext.Sales.Count(), Is.EqualTo(2));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _clientStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Client InsertClientToDatabaseAndReturn(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
||||||
|
{
|
||||||
|
var client = new Client() { Id = id, FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize };
|
||||||
|
MagicCarpetDbContext.Clients.Add(client);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(ClientDataModel? actual, Client expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||||
|
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
|
||||||
|
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClientDataModel CreateModel(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
||||||
|
=> new(id, fio, phoneNumber, discountSize);
|
||||||
|
|
||||||
|
private Client? GetClientFromDatabase(string id) => MagicCarpetDbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Client? actual, ClientDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||||
|
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
|
||||||
|
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,233 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetDatabase.Implementations;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
class EmployeeStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private EmployeeStorageContract _employeeStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_employeeStorageContract = new EmployeeStorageContract(MagicCarpetDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\"CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com");
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com");
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||||
|
var list = _employeeStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(), employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _employeeStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByPostId_Test()
|
||||||
|
{
|
||||||
|
var postId = Guid.NewGuid().ToString();
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId);
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId);
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||||
|
var list = _employeeStorageContract.GetList(postId: postId);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.PostId == postId));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByBirthDate_Test()
|
||||||
|
{
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||||
|
var list = _employeeStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByEmploymentDate_Test()
|
||||||
|
{
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||||
|
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByAllParameters_Test()
|
||||||
|
{
|
||||||
|
var postId = Guid.NewGuid().ToString();
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _employeeStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1),
|
||||||
|
fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_employeeStorageContract.GetElementById(employee.Id), employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _employeeStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_employeeStorageContract.GetElementByFIO(employee.FIO), employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _employeeStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
_employeeStorageContract.AddElement(employee);
|
||||||
|
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
||||||
|
Assert.That(() => _employeeStorageContract.AddElement(employee), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var employee = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
||||||
|
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
||||||
|
_employeeStorageContract.UpdElement(employee);
|
||||||
|
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _employeeStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
||||||
|
Assert.That(() => _employeeStorageContract.UpdElement(employee), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
_employeeStorageContract.DelElement(employee.Id);
|
||||||
|
var element = GetEmployeeFromDatabase(employee.Id);
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.IsDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _employeeStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
||||||
|
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee InsertEmployeeToDatabaseAndReturn(string id, string fio = "test", string email = "abc@mail.ru",string ? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
||||||
|
{
|
||||||
|
var employee = new Employee() { Id = id, FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
||||||
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(EmployeeDataModel? actual, Employee expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||||
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||||
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
||||||
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||||
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static EmployeeDataModel CreateModel(string id, string fio = "fio", string email = "abc@mail.ru", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false) =>
|
||||||
|
new(id, fio, email, postId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
||||||
|
|
||||||
|
private Employee? GetEmployeeFromDatabase(string id) => MagicCarpetDbContext.Employees.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Employee? actual, EmployeeDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||||
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||||
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
||||||
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||||
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetDatabase.Implementations;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SalaryStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private SalaryStorageContract _salaryStorageContract;
|
||||||
|
private Employee _employee;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract = new SalaryStorageContract(MagicCarpetDbContext);
|
||||||
|
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var salary = InsertSalaryToDatabaseAndReturn(_employee.Id, employeeSalary: 100);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(), salary);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_OnlyInDatePeriod_Test()
|
||||||
|
{
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByEmployee_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(employee.Id);
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByEmployeeOnlyInDatePeriod_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var salary = CreateModel(_employee.Id);
|
||||||
|
_salaryStorageContract.AddElement(salary);
|
||||||
|
AssertElement(GetSalaryFromDatabaseByEmployeeId(_employee.Id), salary);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee InsertEmployeeToDatabaseAndReturn(string employeeFIO = "fio", string employeeEmail = "abc@mail.ru")
|
||||||
|
{
|
||||||
|
var employee = new Employee() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = employeeFIO, Email = employeeEmail, IsDeleted = false };
|
||||||
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Salary InsertSalaryToDatabaseAndReturn(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
|
||||||
|
{
|
||||||
|
var salary = new Salary() { EmployeeId = employeeId, EmployeeSalary = employeeSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
||||||
|
MagicCarpetDbContext.Salaries.Add(salary);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(SalaryDataModel? actual, Salary expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||||
|
Assert.That(actual.Salary, Is.EqualTo(expected.EmployeeSalary));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SalaryDataModel CreateModel(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
|
||||||
|
=> new(employeeId, salaryDate ?? DateTime.UtcNow, employeeSalary);
|
||||||
|
|
||||||
|
private Salary? GetSalaryFromDatabaseByEmployeeId(string id) => MagicCarpetDbContext.Salaries.FirstOrDefault(x => x.EmployeeId == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Salary? actual, SalaryDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||||
|
Assert.That(actual.EmployeeSalary, Is.EqualTo(expected.Salary));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,306 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetDatabase.Implementations;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using static NUnit.Framework.Internal.OSPlatform;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SaleStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private SaleStorageContract _saleStorageContract;
|
||||||
|
private Client _client;
|
||||||
|
private Employee _employee;
|
||||||
|
private Tour _tour;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);
|
||||||
|
_client = InsertClientToDatabaseAndReturn();
|
||||||
|
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||||
|
_tour = InsertTourToDatabaseAndReturn();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 10)]);
|
||||||
|
var list = _saleStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(x => x.Id == sale.Id), sale);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _saleStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByPeriod_Test()
|
||||||
|
{
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||||
|
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByEmployeeId_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(employee.Id, null, tours: [(_tour.Id, 1)]);
|
||||||
|
var list = _saleStorageContract.GetList(employeeId: _employee.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByClientId_Test()
|
||||||
|
{
|
||||||
|
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 1)]);
|
||||||
|
var list = _saleStorageContract.GetList(clientId: _client.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.ClientId == _client.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByTourId_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn("Other name");
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1), (tour.Id, 4)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1), (_tour.Id, 1)]);
|
||||||
|
var list = _saleStorageContract.GetList(tourId: _tour.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
Assert.That(list.All(x => x.Tours.Any(y => y.TourId == _tour.Id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByAllParameters_Test()
|
||||||
|
{
|
||||||
|
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
|
||||||
|
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
|
||||||
|
var tour = InsertTourToDatabaseAndReturn("Other name");
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(tour.Id, 1)]);
|
||||||
|
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||||
|
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, tourId: tour.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||||
|
Assert.That(() => _saleStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||||
|
{
|
||||||
|
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
|
||||||
|
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]);
|
||||||
|
_saleStorageContract.AddElement(sale);
|
||||||
|
AssertElement(GetSaleFromDatabaseById(sale.Id), sale);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||||
|
{
|
||||||
|
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, true, [_tour.Id]);
|
||||||
|
Assert.That(() => _saleStorageContract.AddElement(sale), Throws.Nothing);
|
||||||
|
AssertElement(GetSaleFromDatabaseById(sale.Id), CreateModel(sale.Id, _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: false);
|
||||||
|
_saleStorageContract.DelElement(sale.Id);
|
||||||
|
var element = GetSaleFromDatabaseById(sale.Id);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element!.IsCancel);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _saleStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenRecordWasCanceled_Test()
|
||||||
|
{
|
||||||
|
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
|
||||||
|
Assert.That(() => _saleStorageContract.DelElement(sale.Id), Throws.TypeOf<ElementDeletedException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Client InsertClientToDatabaseAndReturn(string fio = "test", string phoneNumber = "+7-777-777-77-77")
|
||||||
|
{
|
||||||
|
var client = new Client() { Id = Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = 10 };
|
||||||
|
MagicCarpetDbContext.Clients.Add(client);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test", string employeeEmail = "abc@gmail.com")
|
||||||
|
{
|
||||||
|
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, Email = employeeEmail, PostId = Guid.NewGuid().ToString() };
|
||||||
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Sightseeing, double price = 1)
|
||||||
|
{
|
||||||
|
var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, Type = tourType, Price = price };
|
||||||
|
MagicCarpetDbContext.Tours.Add(tour);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return tour;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Sale InsertSaleToDatabaseAndReturn(string employeeId, string? clientId, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int)>? tours = null)
|
||||||
|
{
|
||||||
|
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] };
|
||||||
|
if (tours is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in tours)
|
||||||
|
{
|
||||||
|
sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MagicCarpetDbContext.Sales.Add(sale);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return sale;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(SaleDataModel? actual, Sale expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||||
|
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
|
||||||
|
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
|
||||||
|
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
|
||||||
|
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (expected.SaleTours is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Tours, Is.Not.Null);
|
||||||
|
Assert.That(actual.Tours, Has.Count.EqualTo(expected.SaleTours.Count));
|
||||||
|
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.SaleTours[i].TourId));
|
||||||
|
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.SaleTours[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Tours, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SaleDataModel CreateModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType, double discount, bool isCancel, List<string> tourIds)
|
||||||
|
{
|
||||||
|
var tours = tourIds.Select(x => new SaleTourDataModel(id, x, 1)).ToList();
|
||||||
|
return new(id, employeeId, clientId, sum, discountType, discount, isCancel, tours);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Sale? GetSaleFromDatabaseById(string id) => MagicCarpetDbContext.Sales.Include(x => x.SaleTours).FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Sale? actual, SaleDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||||
|
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
|
||||||
|
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
|
||||||
|
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
|
||||||
|
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (expected.Tours is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.SaleTours, Is.Not.Null);
|
||||||
|
Assert.That(actual.SaleTours, Has.Count.EqualTo(expected.Tours.Count));
|
||||||
|
for (int i = 0; i < actual.SaleTours.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.SaleTours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||||
|
Assert.That(actual.SaleTours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.SaleTours, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
using MagicCarpetContracts.DataModels;
|
||||||
|
using MagicCarpetContracts.Enums;
|
||||||
|
using MagicCarpetContracts.Exceptions;
|
||||||
|
using MagicCarpetDatabase.Implementations;
|
||||||
|
using MagicCarpetDatabase.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Moq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using static NUnit.Framework.Internal.OSPlatform;
|
||||||
|
|
||||||
|
namespace MagicCarpetTests.StoragesContracts;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class TourStorageContractTests : BaseStorageContractTest
|
||||||
|
{
|
||||||
|
private TourStorageContract _tourStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_tourStorageContract = new TourStorageContract(MagicCarpetDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||||
|
var list = _tourStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(x => x.Id == tour.Id), tour);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _tourStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetHistoryByTourId_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _tourStorageContract.GetHistoryByTourId(tour.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetHistoryByTourId_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _tourStorageContract.GetHistoryByTourId(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_tourStorageContract.GetElementById(tour.Id), tour);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _tourStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_tourStorageContract.GetElementByName(tour.TourName), tour);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _tourStorageContract.GetElementByName("name"), Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
_tourStorageContract.AddElement(tour);
|
||||||
|
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name unique");
|
||||||
|
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||||
|
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var tour = CreateModel(Guid.NewGuid().ToString(), "new name", "country");
|
||||||
|
InsertTourToDatabaseAndReturn(tour.Id);
|
||||||
|
_tourStorageContract.UpdElement(tour);
|
||||||
|
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _tourStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||||
|
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name");
|
||||||
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||||
|
Assert.That(() => _tourStorageContract.UpdElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
_tourStorageContract.DelElement(tour.Id);
|
||||||
|
var element = GetTourFromDatabaseById(tour.Id);
|
||||||
|
Assert.That(element, Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _tourStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tour InsertTourToDatabaseAndReturn(string id, string tourName = "test", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
|
||||||
|
{
|
||||||
|
var tour = new Tour() { Id = id, TourName = tourName, TourCountry = tourCountry, Type = tourType, Price = price };
|
||||||
|
MagicCarpetDbContext.Tours.Add(tour);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return tour;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TourHistory InsertTourHistoryToDatabaseAndReturn(string tourId, double price, DateTime changeDate)
|
||||||
|
{
|
||||||
|
var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate };
|
||||||
|
MagicCarpetDbContext.TourHistories.Add(tourHistory);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return tourHistory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(TourDataModel? actual, Tour expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
||||||
|
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
|
||||||
|
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TourDataModel CreateModel(string id, string tourName = "test", string tourCountry = "country", TourType type = TourType.Beach, double price = 1)
|
||||||
|
=> new(id, tourName, tourCountry, price, type);
|
||||||
|
|
||||||
|
private Tour? GetTourFromDatabaseById(string id) => MagicCarpetDbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Tour? actual, TourDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
||||||
|
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
|
||||||
|
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user