149 lines
4.1 KiB
C#
149 lines
4.1 KiB
C#
using AutoMapper;
|
|
using GerstITS.Common;
|
|
using GerstITS.Data;
|
|
using GerstITS.Examples.Data.Entities;
|
|
using GerstITS.Search;
|
|
using GerstITS.System.Customizations;
|
|
|
|
namespace GerstITS.Examples.Logic.Customers;
|
|
|
|
internal sealed class CustomerProvider : ICustomerProvider
|
|
{
|
|
#region Fields
|
|
|
|
private readonly IRepository _repository;
|
|
private readonly IMapper _mapper;
|
|
private readonly ISearchEngine _searchEngine;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public CustomerProvider(IRepository repository,
|
|
IMapper mapper,
|
|
ISearchEngine searchEngine)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
_searchEngine = searchEngine;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ICustomerProvider
|
|
|
|
public Customer GetById(int id)
|
|
{
|
|
return GetById(id, false);
|
|
}
|
|
|
|
public Customer GetById(int id, bool includeInactive)
|
|
{
|
|
var entity = _repository.Query<CustomerEntity>(includeInactive)
|
|
.FirstOrDefault(i => i.Id == id)
|
|
.ThrowsAnExceptionIfNotExisting();
|
|
|
|
return ToCustomizedModel(entity);
|
|
}
|
|
|
|
public SearchResult<Customer> Search(CustomerSearchFilter filter)
|
|
{
|
|
var searchResult = _searchEngine.Search<Customer, CustomerSearchFilter>(filter);
|
|
|
|
searchResult.Result = searchResult.Result
|
|
.Select(i => i.ApplyCustomizations())
|
|
.ToList();
|
|
|
|
return searchResult;
|
|
}
|
|
|
|
public Customer Create(Customer customer)
|
|
{
|
|
var entity = _mapper.Map<CustomerEntity>(customer);
|
|
|
|
_repository.Save(entity);
|
|
|
|
return ToCustomizedModel(entity);
|
|
}
|
|
|
|
public Customer Update(Customer customer)
|
|
{
|
|
var entity = _repository.Query<CustomerEntity>()
|
|
.FirstOrDefault(i => i.Id == customer.Id)
|
|
.ThrowsAnExceptionIfNotExisting();
|
|
|
|
_mapper.Map(customer, entity);
|
|
|
|
_repository.Save(entity);
|
|
|
|
return ToCustomizedModel(entity);
|
|
}
|
|
|
|
public bool Delete(int id)
|
|
{
|
|
return Delete(id, false);
|
|
}
|
|
|
|
public bool Delete(int id, bool forceDelete)
|
|
{
|
|
var entity = _repository.Query<CustomerEntity>(forceDelete)
|
|
.FirstOrDefault(i => i.Id == id)
|
|
.ThrowsAnExceptionIfNotExisting();
|
|
|
|
_repository.Delete(entity, forceDelete);
|
|
|
|
return true;
|
|
}
|
|
|
|
public BulkResult Import(IEnumerable<Customer> customers)
|
|
{
|
|
var importableCustomers = customers.Is(x => x.IsNullOrEmpty())
|
|
.ThenThrows<ArgumentException>()
|
|
.WithMessage("At least one customer is required for an import.");
|
|
|
|
var entities = importableCustomers.Select(_mapper.Map<CustomerEntity>)
|
|
.ToList();
|
|
|
|
return _repository.BulkSave(entities);
|
|
}
|
|
|
|
public IEnumerable<CustomerNote> GetNotes(int customerId)
|
|
{
|
|
return _repository.Query<CustomerNoteEntity>()
|
|
.Where(i => i.CustomerId == customerId)
|
|
.OrderBy(i => i.ValidFrom)
|
|
.ToList()
|
|
.Select(_mapper.Map<CustomerNote>)
|
|
.ToList();
|
|
}
|
|
|
|
public CustomerNote AddNote(int customerId, string text)
|
|
{
|
|
_repository.Query<CustomerEntity>()
|
|
.FirstOrDefault(i => i.Id == customerId)
|
|
.ThrowsAnExceptionIfNotExisting();
|
|
|
|
var entity = new CustomerNoteEntity
|
|
{
|
|
CustomerId = customerId,
|
|
Text = text
|
|
};
|
|
|
|
_repository.Save(entity);
|
|
|
|
return _mapper.Map<CustomerNote>(entity);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
private Customer ToCustomizedModel(CustomerEntity entity)
|
|
{
|
|
return _mapper.Map<Customer>(entity)
|
|
.ApplyCustomizations();
|
|
}
|
|
|
|
#endregion
|
|
}
|