Enforce minimum item requirement for law creation and editing; remove unused OrderNumber property from LawItemViewModel

This commit is contained in:
MahanCh
2025-09-09 11:55:01 +03:30
parent 3f1664a844
commit add5b8ef8e
2 changed files with 39 additions and 25 deletions

View File

@@ -16,7 +16,7 @@ namespace CompanyManagment.App.Contracts.Law
{
public string Header { get; set; }
public string Details { get; set; }
public int OrderNumber { get; set; }
// public int OrderNumber { get; set; }
}
public class CreateLaw

View File

@@ -19,24 +19,28 @@ namespace CompanyManagment.Application
public OperationResult Create(CreateLaw command)
{
var operation = new OperationResult();
if (_lawRepository.Exists(x=>x.Type == command.Type))
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())
var law = new Law(command.Title, command.Type);
if (command.Items == null || command.Items.Count == 0)
{
foreach (var item in command.Items)
{
law.AddItem(item.Header, item.Details, item.OrderNumber);
}
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();
}
@@ -44,14 +48,26 @@ namespace CompanyManagment.Application
{
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();
var lawItems = command.Items.Select(x=> new LawItem(x.Header, x.Details, x.OrderNumber)).ToList();
law.Edit(command.Title);
law.SetItem(lawItems);
law.SetItem(lawItems);
_lawRepository.SaveChanges();
return operation.Succcedded();
}
@@ -60,10 +76,10 @@ namespace CompanyManagment.Application
{
var operation = new OperationResult();
var law = _lawRepository.Get(id);
if (law == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
law.Activate();
_lawRepository.SaveChanges();
return operation.Succcedded();
@@ -73,10 +89,10 @@ namespace CompanyManagment.Application
{
var operation = new OperationResult();
var law = _lawRepository.Get(id);
if (law == null)
return operation.Failed(ApplicationMessages.RecordNotFound);
law.Deactivate();
_lawRepository.SaveChanges();
return operation.Succcedded();
@@ -89,11 +105,10 @@ namespace CompanyManagment.Application
{
Id = law.id,
Title = law.Title,
Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel
Items = law.Items.OrderBy(x => x.OrderNumber).Select(x => new LawItemViewModel
{
Header = x.Header,
Details = x.Details,
OrderNumber = x.OrderNumber
Details = x.Details
}).ToList()
};
}
@@ -112,18 +127,17 @@ namespace CompanyManagment.Application
public async Task<LawViewModel> GetLawWithItems(long id)
{
var law =await _lawRepository.GetWithItems(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
Items = law.Items.OrderBy(x=>x.OrderNumber).Select(x => new LawItemViewModel
{
Header = x.Header,
Details = x.Details,
OrderNumber = x.OrderNumber
}).ToList()
};
}
@@ -133,4 +147,4 @@ namespace CompanyManagment.Application
return await _lawRepository.GetByType(type);
}
}
}
}