256 lines
7.3 KiB
C#
256 lines
7.3 KiB
C#
using _0_Framework.Application;
|
|
using Company.Domain.LawAgg;
|
|
using CompanyManagment.App.Contracts.Law;
|
|
using System;
|
|
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.Count == 0)
|
|
{
|
|
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();
|
|
}
|
|
|
|
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)
|
|
{
|
|
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);
|
|
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult UpsertLaw(EditLaw command)
|
|
{
|
|
var operation = new OperationResult();
|
|
|
|
// Validate items
|
|
if (command.Items == null || command.Items.Count == 0)
|
|
{
|
|
return operation.Failed("باید حداقل یک بند برای قانون باید ثبت شود");
|
|
}
|
|
|
|
// Check if law exists by type
|
|
var existingLaw = _lawRepository.Get().FirstOrDefault(x => x.Type == command.Type);
|
|
|
|
if (existingLaw == null)
|
|
{
|
|
// If law doesn't exist, create a new one
|
|
var law = new Law(command.Title, command.Type);
|
|
|
|
var orderNumber = 1;
|
|
foreach (var item in command.Items)
|
|
{
|
|
law.AddItem(item.Header, item.Details, orderNumber);
|
|
orderNumber++;
|
|
}
|
|
|
|
_lawRepository.Create(law);
|
|
_lawRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
// If law exists, update it
|
|
var orderNumber = 1;
|
|
var lawItems = command.Items.Select(x =>
|
|
{
|
|
var res = new LawItem(x.Header, x.Details, orderNumber);
|
|
orderNumber++;
|
|
return res;
|
|
}).ToList();
|
|
|
|
existingLaw.Edit(command.Title);
|
|
existingLaw.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 OperationResult ActivateByType(LawType type)
|
|
{
|
|
var operation = new OperationResult();
|
|
var law = _lawRepository.Get().FirstOrDefault(x => x.Type == type);
|
|
|
|
if (law == null)
|
|
{
|
|
// If law doesn't exist, create a new active one with default values
|
|
var newLaw = new Law(GetDefaultTitleForLawType(type), type);
|
|
newLaw.Activate();
|
|
_lawRepository.Create(newLaw);
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
law.Activate();
|
|
_lawRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult DeactivateByType(LawType type)
|
|
{
|
|
var operation = new OperationResult();
|
|
var law = _lawRepository.Get().FirstOrDefault(x => x.Type == type);
|
|
|
|
if (law == null)
|
|
return operation.Failed("قانون مورد نظر یافت نشد");
|
|
|
|
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,
|
|
Type = law.Type,
|
|
Items = law.Items.OrderBy(x => x.OrderNumber).Select(x => new LawItemViewModel
|
|
{
|
|
Header = x.Header,
|
|
Details = x.Details
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<List<LawViewModel>> GetList(LawSearchModel searchModel)
|
|
{
|
|
// Get filtered laws from database
|
|
return await _lawRepository.GetList(searchModel);
|
|
|
|
|
|
}
|
|
|
|
private string GetDefaultTitleForLawType(LawType lawType)
|
|
{
|
|
return lawType switch
|
|
{
|
|
LawType.Register => "قوانین ثبت نام",
|
|
_ => $"قوانین {lawType}"
|
|
};
|
|
}
|
|
|
|
public async Task<LawViewModel> GetLawWithItems(long id)
|
|
{
|
|
var law = await _lawRepository.GetWithItems(id);
|
|
if (law == null)
|
|
return null;
|
|
|
|
return new LawViewModel
|
|
{
|
|
Id = law.id,
|
|
Title = law.Title,
|
|
IsActive = law.IsActive,
|
|
CreatedAt = law.CreationDate,
|
|
Type = law.Type,
|
|
Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel
|
|
{
|
|
Header = x.Header,
|
|
Details = x.Details,
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<LawViewModel> GetLawByType(LawType type)
|
|
{
|
|
var lawViewModel = await _lawRepository.GetByType(type);
|
|
|
|
// If no law exists for this type, return a default empty law
|
|
if (lawViewModel == null)
|
|
{
|
|
return new LawViewModel
|
|
{
|
|
Id = 0,
|
|
Title = GetDefaultTitleForLawType(type),
|
|
IsActive = false,
|
|
CreatedAt = DateTime.Now,
|
|
Type = type,
|
|
Items = new List<LawItemViewModel>()
|
|
};
|
|
}
|
|
|
|
return lawViewModel;
|
|
}
|
|
} |