Files
Microsoft.SelfService.Porta…/Repository/DeploymentBatchRepository.cs

148 lines
4.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.SelfService.Portal.Core.API.Context;
using Microsoft.SelfService.Portal.Core.API.Interfaces;
using Microsoft.SelfService.Portal.Core.API.Models;
namespace Microsoft.SelfService.Portal.Core.API.Repository
{
public class DeploymentBatchRepository : IDeploymentBatchInterface
{
private readonly DataContext _context;
public DeploymentBatchRepository(DataContext context)
{
_context = context;
}
public ICollection<DeploymentGroupModel> GetDeploymentBatches()
{
return _context.DeploymentGroups.ToList();
}
public DeploymentGroupModel? GetDeploymentBatchById(Guid id)
{
return _context.DeploymentGroups
.Include(t => t.Template)
.ThenInclude(tc => tc.TemplateCategory)
.ThenInclude(s => s.Service)
.Include(d => d.Deployments)
.ThenInclude(vm => vm.Target)
.Include(batch => batch.TemplateSelections)
.ThenInclude(selection => selection.TemplateVersion)
.Include(batch => batch.ParameterValues)
.Include(batch => batch.TargetAssignments)
.ThenInclude(target => target.Target)
.Where(dg => dg.Id == id).FirstOrDefault();
}
public bool AddDeploymentBatchById(DeploymentGroupModel deploymentBatch, ICollection<Guid>? targetIds)
{
if (deploymentBatch.Id == Guid.Empty)
{
deploymentBatch.Id = Guid.NewGuid();
}
var template = _context.Templates
.Include(existing => existing.TemplateCategory)
.ThenInclude(existing => existing.Service)
.FirstOrDefault(existing => existing.Id == deploymentBatch.TemplateId);
if (template == null)
{
return false;
}
if (deploymentBatch.DeploymentRuleId.HasValue)
{
var ruleExists = _context.DeploymentRules.Any(existing => existing.Id == deploymentBatch.DeploymentRuleId.Value);
if (!ruleExists)
{
return false;
}
}
var isCloudService = template.TemplateCategory.Service.IsCloudService;
var selectedTargetIds = (targetIds ?? Array.Empty<Guid>())
.Distinct()
.ToList();
if (!isCloudService && selectedTargetIds.Count == 0)
{
return false;
}
if (selectedTargetIds.Count > 0)
{
var existingTargetIds = _context.Targets
.Where(existing => selectedTargetIds.Contains(existing.Id))
.Select(existing => existing.Id)
.ToHashSet();
if (existingTargetIds.Count != selectedTargetIds.Count)
{
return false;
}
}
_context.Add(deploymentBatch);
if (!SaveChanges())
{
return false;
}
if (!isCloudService)
{
foreach (var targetId in selectedTargetIds)
{
_context.Deployments.Add(new DeploymentModel
{
DeploymentGroupId = deploymentBatch.Id,
TargetId = targetId,
Status = QueueJobStatus.Pending,
JSONData = "{}"
});
}
return SaveChanges();
}
return true;
}
public bool DeleteDeploymentBatchById(DeploymentGroupModel deploymentBatch)
{
var deployments = _context.Deployments
.Where(existing => existing.DeploymentGroupId == deploymentBatch.Id)
.ToList();
if (deployments.Count > 0)
{
_context.Deployments.RemoveRange(deployments);
}
_context.Remove(deploymentBatch);
return SaveChanges();
}
public bool EditDeploymentBatchById(DeploymentGroupModel deploymentBatch)
{
_context.Update(deploymentBatch);
return SaveChanges();
}
public bool CheckDeploymentBatchById(Guid id)
{
return _context.DeploymentGroups
.Any(d => d.Id == id);
}
public bool SaveChanges()
{
var saved = _context.SaveChanges();
return saved > 0 ? true : false;
}
}
}