Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPWebApplication/Adapters/CommentAdapter.cs

147 lines
4.5 KiB
C#

using AutoMapper;
using YAPContracts.AdapterContracts;
using YAPContracts.BindingModels;
using YAPContracts.BusinessLogicContracts;
using YAPContracts.DataModels;
using YAPContracts.ViewModels;
namespace YAPWebApplication.Adapters
{
public class CommentAdapter : ICommentAdapter
{
private readonly ICommentBusinessLogicContract _commentBL;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public CommentAdapter(ICommentBusinessLogicContract commentBL, ILogger<CommentAdapter> logger)
{
_commentBL = commentBL;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CommentBindingModel, CommentDataModel>();
cfg.CreateMap<CommentDataModel, CommentViewModel>();
cfg.CreateMap<CommentViewModel, CommentDataModel>();
});
_mapper = new Mapper(config);
}
public List<CommentViewModel>? GetList()
{
try
{
return _commentBL.GetAllComments()
.Select(x => _mapper.Map<CommentViewModel>(x))
.ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.GetList");
return null;
}
}
public CommentViewModel? GetCommentById(string id)
{
try
{
var data = _commentBL.GetCommentByData(id);
return data != null ? _mapper.Map<CommentViewModel>(data) : null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.GetCommentById");
return null;
}
}
public List<CommentViewModel>? GetCommentsByProductSetAndPeriod(string productSetId, DateTime fromDate, DateTime toDate)
{
try
{
return _commentBL.GetCommentsByProductSetByPeriod(productSetId, fromDate, toDate)
.Select(x => _mapper.Map<CommentViewModel>(x))
.ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.GetCommentsByProductSetAndPeriod");
return null;
}
}
public List<CommentViewModel>? GetCommentsByUserAndPeriod(string userId, DateTime fromDate, DateTime toDate)
{
try
{
return _commentBL.GetCommentsByUserByPeriod(userId, fromDate, toDate)
.Select(x => _mapper.Map<CommentViewModel>(x))
.ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.GetCommentsByUserAndPeriod");
return null;
}
}
public List<CommentViewModel>? GetCommentsByPeriod(DateTime fromDate, DateTime toDate)
{
try
{
return _commentBL.GetCommentsByPeriod(fromDate, toDate)
.Select(x => _mapper.Map<CommentViewModel>(x))
.ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.GetCommentsByPeriod");
return null;
}
}
public void Create(CommentBindingModel comment)
{
try
{
var data = _mapper.Map<CommentDataModel>(comment);
_commentBL.InsertComment(data);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.Create");
throw;
}
}
public void Update(CommentBindingModel comment)
{
try
{
var data = _mapper.Map<CommentDataModel>(comment);
_commentBL.UpdateComment(data);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.Update");
throw;
}
}
public void Delete(string id)
{
try
{
_commentBL.DeleteComment(id);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in CommentAdapter.Delete");
throw;
}
}
}
}