Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -30,10 +30,70 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
.Where(dg => dg.Id == Id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool AddDeploymentGroupById(DeploymentGroupModel deploymentgroup)
|
||||
public bool AddDeploymentGroupById(DeploymentGroupModel deploymentgroup, ICollection<Guid>? virtualMachineIds)
|
||||
{
|
||||
if (deploymentgroup.Id == Guid.Empty)
|
||||
{
|
||||
deploymentgroup.Id = Guid.NewGuid();
|
||||
}
|
||||
|
||||
var template = _context.Templates
|
||||
.Include(existing => existing.TemplateCategory)
|
||||
.ThenInclude(existing => existing.Service)
|
||||
.FirstOrDefault(existing => existing.Id == deploymentgroup.TemplateId);
|
||||
|
||||
if (template == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var isCloudService = template.TemplateCategory.Service.IsCloudService;
|
||||
var selectedVirtualMachineIds = (virtualMachineIds ?? Array.Empty<Guid>())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
if (!isCloudService && selectedVirtualMachineIds.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (selectedVirtualMachineIds.Count > 0)
|
||||
{
|
||||
var existingVirtualMachineIds = _context.VirtualMachines
|
||||
.Where(existing => selectedVirtualMachineIds.Contains(existing.Id))
|
||||
.Select(existing => existing.Id)
|
||||
.ToHashSet();
|
||||
|
||||
if (existingVirtualMachineIds.Count != selectedVirtualMachineIds.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_context.Add(deploymentgroup);
|
||||
return SaveChanges();
|
||||
|
||||
if (!SaveChanges())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isCloudService)
|
||||
{
|
||||
foreach (var virtualMachineId in selectedVirtualMachineIds)
|
||||
{
|
||||
_context.Deployments.Add(new DeploymentModel
|
||||
{
|
||||
DeploymentGroupId = deploymentgroup.Id,
|
||||
VirtualMachineId = virtualMachineId,
|
||||
Status = QueueJobStatus.Pending,
|
||||
JSONData = "{}"
|
||||
});
|
||||
}
|
||||
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DeleteDeploymentGroupById(DeploymentGroupModel deploymentgroup)
|
||||
|
||||
Reference in New Issue
Block a user