Files
Backend-Api/CompanyManagment.Application/LawApplication.cs

137 lines
4.2 KiB
C#

using _0_Framework.Application;
using Company.Domain.LawAgg;
using CompanyManagment.App.Contracts.Law;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CompanyManagment.Application
{
public class LawApplication : ILawApplication
{
private readonly ILawRepository _lawRepository;
public LawApplication(ILawRepository lawRepository)
{
_lawRepository = lawRepository;
}
public OperationResult Create(CreateLaw command)
{
var operation = new OperationResult();
if (_lawRepository.Exists(x=>x.Type == command.Type))
{
return operation.Failed("این قانون قبلا ثبت شده است");
}
var law = new Law(command.Title,command.Type);
if (command.Items != null && command.Items.Any())
{
foreach (var item in command.Items)
{
law.AddItem(item.Header, item.Details, item.OrderNumber);
}
}
_lawRepository.Create(law);
_lawRepository.SaveChanges();
return operation.Succcedded();
}
public OperationResult Edit(EditLaw command)
{
var operation = new OperationResult();
var law = _lawRepository.Get(command.Id);
if (law == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
var lawItems = command.Items.Select(x=> new LawItem(x.Header, x.Details, x.OrderNumber)).ToList();
law.Edit(command.Title);
law.SetItem(lawItems);
_lawRepository.SaveChanges();
return operation.Succcedded();
}
public OperationResult Activate(long id)
{
var operation = new OperationResult();
var law = _lawRepository.Get(id);
if (law == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
law.Activate();
_lawRepository.SaveChanges();
return operation.Succcedded();
}
public OperationResult Deactivate(long id)
{
var operation = new OperationResult();
var law = _lawRepository.Get(id);
if (law == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
law.Deactivate();
_lawRepository.SaveChanges();
return operation.Succcedded();
}
public EditLaw GetDetails(long id)
{
var law = _lawRepository.Get(id);
return new EditLaw
{
Id = law.id,
Title = law.Title,
Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel
{
Header = x.Header,
Details = x.Details,
OrderNumber = x.OrderNumber
}).ToList()
};
}
public List<LawViewModel> GetList()
{
return _lawRepository.Get()
.Select(x => new LawViewModel
{
Id = x.id,
Title = x.Title,
IsActive = x.IsActive,
CreatedAt = x.CreationDate
}).ToList();
}
public async Task<LawViewModel> GetLawWithItems(long id)
{
var law =await _lawRepository.GetWithItems(id);
return new LawViewModel
{
Id = law.id,
Title = law.Title,
IsActive = law.IsActive,
CreatedAt = law.CreationDate,
Items = law.Items.Select(x => new LawItemViewModel
{
Header = x.Header,
Details = x.Details,
OrderNumber = x.OrderNumber
}).ToList()
};
}
public async Task<LawViewModel> GetLawByType(LawType type)
{
return await _lawRepository.GetByType(type);
}
}
}