Контракты
This commit is contained in:
parent
a724b1979b
commit
643a5b7280
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PuferFishContracts.Exceptions
|
||||||
|
{
|
||||||
|
public class ElementDeletedException : Exception
|
||||||
|
{
|
||||||
|
public ElementDeletedException(string id) : base($"Cannot modify a deleted item(id: { id})") { }
|
||||||
|
}
|
||||||
|
}
|
@ -9,8 +9,7 @@ namespace PuferFishContracts.StoragesContracts;
|
|||||||
|
|
||||||
public interface IProductStorageContract
|
public interface IProductStorageContract
|
||||||
{
|
{
|
||||||
List<ProductDataModel> GetList(bool onlyActive = true, string?
|
List<ProductDataModel> GetList(bool onlyActive = true);
|
||||||
manufacturerId = null);
|
|
||||||
List<ProductHistoryDataModel> GetHistoryByProductId(string productId);
|
List<ProductHistoryDataModel> GetHistoryByProductId(string productId);
|
||||||
ProductDataModel? GetElementById(string id);
|
ProductDataModel? GetElementById(string id);
|
||||||
ProductDataModel? GetElementByName(string name);
|
ProductDataModel? GetElementByName(string name);
|
||||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using PuferFishContracts.DataModels;
|
using PuferFishContracts.DataModels;
|
||||||
@ -14,9 +15,9 @@ namespace PuferFishDataBase.Implementations;
|
|||||||
|
|
||||||
internal class BuyerStorageContract : IBuyerStorageContract
|
internal class BuyerStorageContract : IBuyerStorageContract
|
||||||
{
|
{
|
||||||
private readonly CatHasPawsDbContext _dbContext;
|
private readonly PuferFishDbContext _dbContext;
|
||||||
private readonly Mapper _mapper;
|
private readonly Mapper _mapper;
|
||||||
public BuyerStorageContract(CatHasPawsDbContext dbContext)
|
public BuyerStorageContract(PuferFishDbContext dbContext)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
var config = new MapperConfiguration(cfg =>
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using PuferFishContracts.DataModels;
|
||||||
|
using PuferFishContracts.Exceptions;
|
||||||
|
using PuferFishContracts.StoragesContracts;
|
||||||
|
using PuferFishDataBase.Models;
|
||||||
|
|
||||||
|
namespace PuferFishDataBase.Implementations;
|
||||||
|
|
||||||
|
internal class PointsStorageContract : IPointsStorageContract
|
||||||
|
{
|
||||||
|
private readonly PuferFishDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public PointsStorageContract(PuferFishDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Points, PointsDataModel>();
|
||||||
|
cfg.CreateMap<PointsDataModel, Points>()
|
||||||
|
.ForMember(dest => dest.BuyerPoints, opt =>
|
||||||
|
opt.MapFrom(src => src.Points));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<PointsDataModel> GetList(DateTime startDate, DateTime endDate, string? buyerId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Pointss.Where(x => x.PointsDate >=
|
||||||
|
startDate && x.PointsDate <= endDate);
|
||||||
|
if (buyerId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.BuyerId == buyerId);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<PointsDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddElement(PointsDataModel salaryDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Pointss.Add(_mapper.Map<Points>(salaryDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,203 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using PuferFishContracts.DataModels;
|
||||||
|
using PuferFishContracts.Exceptions;
|
||||||
|
using PuferFishContracts.StoragesContracts;
|
||||||
|
using PuferFishDataBase.Models;
|
||||||
|
|
||||||
|
namespace PuferFishDataBase.Implementations;
|
||||||
|
|
||||||
|
internal class PostStorageContract : IPostStorageContract
|
||||||
|
{
|
||||||
|
private readonly PuferFishDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
public PostStorageContract(PuferFishDbContext 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,183 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using PuferFishContracts.DataModels;
|
||||||
|
using PuferFishContracts.Exceptions;
|
||||||
|
using PuferFishContracts.StoragesContracts;
|
||||||
|
using PuferFishDataBase.Models;
|
||||||
|
|
||||||
|
namespace PuferFishDataBase.Implementations;
|
||||||
|
|
||||||
|
internal class ProductStorageContract : IProductStorageContract
|
||||||
|
{
|
||||||
|
private readonly PuferFishDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
public ProductStorageContract(PuferFishDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Product, ProductDataModel>();
|
||||||
|
cfg.CreateMap<ProductDataModel, Product>()
|
||||||
|
.ForMember(x => x.IsDeleted, x => x.MapFrom(src =>
|
||||||
|
false));
|
||||||
|
cfg.CreateMap<ProductHistory, ProductHistoryDataModel>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<ProductDataModel> GetList(bool onlyActive = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Products.AsQueryable();
|
||||||
|
if (onlyActive)
|
||||||
|
{
|
||||||
|
query = query.Where(x => !x.IsDeleted);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<ProductDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<ProductHistoryDataModel> GetHistoryByProductId(string
|
||||||
|
productId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.ProductHistories.Where(x => x.ProductId
|
||||||
|
== productId).OrderByDescending(x => x.ChangeDate).Select(x => _mapper.Map<ProductHistoryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ProductDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ProductDataModel>(GetProductById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public ProductDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return
|
||||||
|
_mapper.Map<ProductDataModel>(_dbContext.Products.FirstOrDefault(x => x.ProductName == name && !x.IsDeleted));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddElement(ProductDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Products.Add(_mapper.Map<Product>(productDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name ==
|
||||||
|
"ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", productDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is
|
||||||
|
PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("ProductName",
|
||||||
|
productDataModel.ProductName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdElement(ProductDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetProductById(productDataModel.Id) ??
|
||||||
|
throw new ElementNotFoundException(productDataModel.Id);
|
||||||
|
if (element.Price != productDataModel.Price)
|
||||||
|
{
|
||||||
|
_dbContext.ProductHistories.Add(new
|
||||||
|
ProductHistory()
|
||||||
|
{ ProductId = element.Id, OldPrice = element.Price });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
_dbContext.Products.Update(_mapper.Map(productDataModel,
|
||||||
|
element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is
|
||||||
|
PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("ProductName",
|
||||||
|
productDataModel.ProductName);
|
||||||
|
}
|
||||||
|
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 = GetProductById(id) ?? throw new
|
||||||
|
ElementNotFoundException(id);
|
||||||
|
element.IsDeleted = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private Product? GetProductById(string id) =>
|
||||||
|
_dbContext.Products.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PuferFishContracts.DataModels;
|
||||||
|
using PuferFishContracts.Exceptions;
|
||||||
|
using PuferFishContracts.StoragesContracts;
|
||||||
|
using PuferFishDataBase.Models;
|
||||||
|
|
||||||
|
namespace PuferFishDataBase.Implementations;
|
||||||
|
|
||||||
|
internal class SaleStorageContract : ISaleStorageContract
|
||||||
|
{
|
||||||
|
private readonly PuferFishDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
public SaleStorageContract(PuferFishDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<SaleProduct, SaleProductDataModel>();
|
||||||
|
cfg.CreateMap<SaleProductDataModel, SaleProduct>();
|
||||||
|
cfg.CreateMap<Sale, SaleDataModel>();
|
||||||
|
cfg.CreateMap<SaleDataModel, Sale>()
|
||||||
|
.ForMember(x => x.IsCancel, x => x.MapFrom(src =>
|
||||||
|
false))
|
||||||
|
.ForMember(x => x.SaleProducts, x => x.MapFrom(src =>
|
||||||
|
src.Products));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null,
|
||||||
|
string? buyerId = null, string? productId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Sales.Include(x =>
|
||||||
|
x.SaleProducts).AsQueryable();
|
||||||
|
if (startDate is not null && endDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.SaleDate >= startDate &&
|
||||||
|
x.SaleDate < endDate);
|
||||||
|
}
|
||||||
|
if (workerId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.WorkerId == workerId);
|
||||||
|
}
|
||||||
|
if (buyerId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.BuyerId == buyerId);
|
||||||
|
}
|
||||||
|
if (productId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.SaleProducts!.Any(y =>
|
||||||
|
y.ProductId == productId));
|
||||||
|
}
|
||||||
|
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,152 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using PuferFishContracts.DataModels;
|
||||||
|
using PuferFishContracts.Exceptions;
|
||||||
|
using PuferFishContracts.StoragesContracts;
|
||||||
|
using PuferFishDataBase.Models;
|
||||||
|
|
||||||
|
namespace PuferFishDataBase.Implementations;
|
||||||
|
|
||||||
|
internal class WorkerStorageContract : IWorkerStorageContract
|
||||||
|
{
|
||||||
|
private readonly PuferFishDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public WorkerStorageContract(PuferFishDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Worker, WorkerDataModel>();
|
||||||
|
cfg.CreateMap<WorkerDataModel, Worker>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
public List<WorkerDataModel> GetList(bool onlyActive = true, string? postId
|
||||||
|
= null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime?
|
||||||
|
fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Workers.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<WorkerDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public WorkerDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public WorkerDataModel? GetElementByFIO(string fio)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return
|
||||||
|
_mapper.Map<WorkerDataModel>(_dbContext.Workers.FirstOrDefault(x => x.FIO ==
|
||||||
|
fio));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddElement(WorkerDataModel workerDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Workers.Add(_mapper.Map<Worker>(workerDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name ==
|
||||||
|
"ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", workerDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdElement(WorkerDataModel workerDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWorkerById(workerDataModel.Id) ?? throw new
|
||||||
|
ElementNotFoundException(workerDataModel.Id);
|
||||||
|
_dbContext.Workers.Update(_mapper.Map(workerDataModel,
|
||||||
|
element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWorkerById(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 Worker? GetWorkerById(string id) =>
|
||||||
|
_dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user