177 lines
5.7 KiB
C#
177 lines
5.7 KiB
C#
using AutoMapper;
|
|
using CandyHouseBase.DataModels;
|
|
using CandyHouseBase.Exceptions;
|
|
using CandyHouseBase.Interfaces.StoragesContracts;
|
|
using CandyHouseDataBase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Npgsql;
|
|
using System.Collections.Generic;
|
|
|
|
namespace CandyHouseDataBase.Implementations;
|
|
|
|
internal class IngredientStorageContract : IIngredientStorageContact
|
|
{
|
|
private readonly CandyHouseDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public IngredientStorageContract(CandyHouseDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Ingredient, IngredientDataModel>()
|
|
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
|
|
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
|
|
.ForMember(dest => dest.Unit, opt => opt.MapFrom(src => src.Unit))
|
|
.ForMember(dest => dest.Cost, opt => opt.MapFrom(src => src.Cost));
|
|
cfg.CreateMap<IngredientDataModel, Ingredient>()
|
|
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
|
|
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
|
|
.ForMember(dest => dest.Unit, opt => opt.MapFrom(src => src.Unit))
|
|
.ForMember(dest => dest.Cost, opt => opt.MapFrom(src => src.Cost));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<IngredientDataModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
return _dbContext.Ingredients
|
|
.Select(x => _mapper.Map<IngredientDataModel>(x))
|
|
.ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public IngredientDataModel GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
var entity = GetIngredientById(id) ?? throw new ElementNotFoundException(id);
|
|
return _mapper.Map<IngredientDataModel>(entity);
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public IngredientDataModel GetElementByName(string name)
|
|
{
|
|
try
|
|
{
|
|
var entity = GetIngredientByName(name) ?? throw new ElementNotFoundException(name);
|
|
return _mapper.Map<IngredientDataModel>(entity);
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void AddElement(IngredientDataModel ingredient)
|
|
{
|
|
try
|
|
{
|
|
ingredient.Validate();
|
|
var entity = _mapper.Map<Ingredient>(ingredient);
|
|
_dbContext.Ingredients.Add(entity);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException
|
|
{
|
|
ConstraintName: "IX_Ingredients_Name"
|
|
})
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Name", ingredient.Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void UpdateElement(IngredientDataModel ingredient)
|
|
{
|
|
try
|
|
{
|
|
ingredient.Validate();
|
|
var entity = GetIngredientById(ingredient.Id) ?? throw new ElementNotFoundException(ingredient.Id);
|
|
_dbContext.Ingredients.Update(_mapper.Map(ingredient, entity));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException
|
|
{
|
|
ConstraintName: "IX_Ingredients_Name"
|
|
})
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("Name", ingredient.Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void DeleteElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var entity = GetIngredientById(id) ?? throw new ElementNotFoundException(id);
|
|
_dbContext.Ingredients.Remove(entity);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
private Ingredient? GetIngredientById(string id) =>
|
|
_dbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
|
|
|
private Ingredient? GetIngredientByName(string name) =>
|
|
_dbContext.Ingredients.FirstOrDefault(x => x.Name == name);
|
|
} |