diff --git a/CompanyManagment.Application/LawApplication.cs b/CompanyManagment.Application/LawApplication.cs index b3bd904f..2bff1518 100644 --- a/CompanyManagment.Application/LawApplication.cs +++ b/CompanyManagment.Application/LawApplication.cs @@ -5,146 +5,145 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace CompanyManagment.Application +namespace CompanyManagment.Application; + +public class LawApplication : ILawApplication { - public class LawApplication : ILawApplication + private readonly ILawRepository _lawRepository; + + public LawApplication(ILawRepository lawRepository) { - private readonly ILawRepository _lawRepository; + _lawRepository = lawRepository; + } - public LawApplication(ILawRepository lawRepository) + public OperationResult Create(CreateLaw command) + { + var operation = new OperationResult(); + if (_lawRepository.Exists(x => x.Type == command.Type)) { - _lawRepository = lawRepository; + return operation.Failed("این قانون قبلا ثبت شده است"); } - public OperationResult Create(CreateLaw command) + var law = new Law(command.Title, command.Type); + if (command.Items == null || command.Items.Count == 0) { - 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.Count == 0) - { - return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود"); - } + return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود"); + } - var orderNumber = 1; - foreach (var item in command.Items) - { - law.AddItem(item.Header, item.Details, orderNumber); - orderNumber++; - } - - - _lawRepository.Create(law); - _lawRepository.SaveChanges(); - - return operation.Succcedded(); + var orderNumber = 1; + foreach (var item in command.Items) + { + law.AddItem(item.Header, item.Details, orderNumber); + orderNumber++; } - public OperationResult Edit(EditLaw command) + + _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); + + if (command.Items == null || command.Items.Count == 0) { - var operation = new OperationResult(); - var law = _lawRepository.Get(command.Id); + return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود"); + } + var orderNumber = 1; + var lawItems = command.Items.Select(x => + { + var res= new LawItem(x.Header, x.Details, orderNumber); + orderNumber++; + return res; + }).ToList(); + + law.Edit(command.Title); + + law.SetItem(lawItems); - if (law == null) - return operation.Failed(ApplicationMessages.RecordNotFound); + _lawRepository.SaveChanges(); + return operation.Succcedded(); + } - if (command.Items == null || command.Items.Count == 0) + 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 { - return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود"); - } - var orderNumber = 1; - var lawItems = command.Items.Select(x => + Header = x.Header, + Details = x.Details + }).ToList() + }; + } + + public List GetList() + { + return _lawRepository.Get() + .Select(x => new LawViewModel { - var res= new LawItem(x.Header, x.Details, orderNumber); - orderNumber++; - return res; + Id = x.id, + Title = x.Title, + IsActive = x.IsActive, + CreatedAt = x.CreationDate }).ToList(); - - law.Edit(command.Title); - - law.SetItem(lawItems); + } - _lawRepository.SaveChanges(); - return operation.Succcedded(); - } - - public OperationResult Activate(long id) + public async Task GetLawWithItems(long id) + { + var law = await _lawRepository.GetWithItems(id); + return new LawViewModel { - 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, + IsActive = law.IsActive, + CreatedAt = law.CreationDate, + Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel { - Id = law.id, - Title = law.Title, - Items = law.Items.OrderBy(x => x.OrderNumber).Select(x => new LawItemViewModel - { - Header = x.Header, - Details = x.Details - }).ToList() - }; - } + Header = x.Header, + Details = x.Details, + }).ToList() + }; + } - public List GetList() - { - return _lawRepository.Get() - .Select(x => new LawViewModel - { - Id = x.id, - Title = x.Title, - IsActive = x.IsActive, - CreatedAt = x.CreationDate - }).ToList(); - } - - public async Task 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.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel - { - Header = x.Header, - Details = x.Details, - }).ToList() - }; - } - - public async Task GetLawByType(LawType type) - { - return await _lawRepository.GetByType(type); - } + public async Task GetLawByType(LawType type) + { + return await _lawRepository.GetByType(type); } } \ No newline at end of file