using System.Threading.Tasks;
using GxPress.Repository.Interface.Navigation;
using GxPress.Service.Interface.Navigation;
using System.Transactions;
using GxPress.Common.Tools;
using System.Linq;
using System.Collections.Generic;
using GxPress.Request.Navigation;
using AutoMapper;
namespace GxPress.Service.Implement.Navigation
{
public class MiddleLableService : IMiddleLableService
{
private readonly IMiddleLableRepository middleLableRepository;
private readonly IMiddleLableNexusRepository middleLableNexusRepository;
private readonly IMapper _mapper;
public MiddleLableService(IMiddleLableRepository middleLableRepository, IMiddleLableNexusRepository middleLableNexusRepository, IMapper mapper)
{
this.middleLableRepository = middleLableRepository;
this.middleLableNexusRepository = middleLableNexusRepository;
_mapper = mapper;
}
///
/// 添加中间件页面
///
///
///
public async Task InsertAsync(MiddleLableInRequest request)
{
try
{
using (var transactionScope = new System.Transactions.TransactionScope())
{
var model = _mapper.Map(request);
var middleLableId = await middleLableRepository.InsertAsync(model);
var ids = StringUtils.StringCollectionToStringList(request.LabelId);
var idInts = new List();
foreach (var item in ids)
idInts.Add(int.Parse(item));
await middleLableNexusRepository.InsertAsync(middleLableId, idInts);
transactionScope.Complete();
}
}
catch
{
return false;
}
return true;
}
///
/// 删除
///
///
///
public async Task DeleteAsync(int id)
{
try
{
using (var transactionScope = new System.Transactions.TransactionScope())
{
await middleLableRepository.DeleteAsync(id);
await middleLableNexusRepository.DeleteByMiddleLableIdAsync(id);
transactionScope.Complete();
}
}
catch
{
return false;
}
return true;
}
public async Task UpdateAsync(MiddleLableUpRequest request)
{
try
{
using (var transactionScope = new System.Transactions.TransactionScope())
{
await middleLableRepository.UpdateAsync(request);
if (!string.IsNullOrEmpty(request.LabelId))
await middleLableNexusRepository.UpdateAsync(request.Id, request.LabelId);
transactionScope.Complete();
}
}
catch
{
return false;
}
return true;
}
}
}