192 lines
6.3 KiB
C#
192 lines
6.3 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 IWorkerAdapter _workerAdapter;
|
|
private readonly IProductSetAdapter _productSetAdapter;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
|
|
public CommentAdapter(
|
|
ICommentBusinessLogicContract commentBL,
|
|
IWorkerAdapter workerAdapter,
|
|
IProductSetAdapter productSetAdapter,
|
|
ILogger<CommentAdapter> logger)
|
|
{
|
|
_commentBL = commentBL;
|
|
_workerAdapter = workerAdapter;
|
|
_productSetAdapter = productSetAdapter;
|
|
_logger = logger;
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<CommentBindingModel, CommentDataModel>();
|
|
cfg.CreateMap<CommentDataModel, CommentViewModel>()
|
|
.ForMember(dest => dest.AuthorId, opt => opt.MapFrom(src => src.UserId))
|
|
.ForMember(dest => dest.CommentDate, opt => opt.MapFrom(src => src.Date));
|
|
cfg.CreateMap<CommentViewModel, CommentDataModel>()
|
|
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.AuthorId))
|
|
.ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.CommentDate));
|
|
});
|
|
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<CommentViewModel>? GetList()
|
|
{
|
|
try
|
|
{
|
|
var comments = _commentBL.GetAllComments()
|
|
.Select(x => _mapper.Map<CommentViewModel>(x))
|
|
.ToList();
|
|
|
|
foreach (var comment in comments)
|
|
{
|
|
if (!string.IsNullOrEmpty(comment.AuthorId))
|
|
{
|
|
var user = _workerAdapter.GetElement(comment.AuthorId);
|
|
comment.UserLogin = user?.Login;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(comment.ProductSetId))
|
|
{
|
|
var set = _productSetAdapter.GetProductSetByData(comment.ProductSetId);
|
|
comment.ProductSetName = set?.SetName;
|
|
}
|
|
}
|
|
|
|
return comments;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in CommentAdapter.GetList");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public CommentViewModel? GetCommentById(string id)
|
|
{
|
|
try
|
|
{
|
|
var data = _commentBL.GetCommentByData(id);
|
|
if (data == null) return null;
|
|
|
|
var comment = _mapper.Map<CommentViewModel>(data);
|
|
|
|
if (!string.IsNullOrEmpty(comment.AuthorId))
|
|
{
|
|
var user = _workerAdapter.GetElement(comment.AuthorId);
|
|
comment.UserLogin = user?.Login;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(comment.ProductSetId))
|
|
{
|
|
var set = _productSetAdapter.GetProductSetByData(comment.ProductSetId);
|
|
comment.ProductSetName = set?.SetName;
|
|
}
|
|
|
|
return comment;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|