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)
|
||||
|
||||
@@ -51,8 +51,14 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
public ICollection<TemplateModel> GetAvailableTemplatesByEnvironmentId(Guid Id)
|
||||
{
|
||||
var environment = _context.Environments.Where(e => e.Id == Id).FirstOrDefault();
|
||||
var isCloudEnvironment = environment != null
|
||||
&& environment.EnvironmentType != EnvironmentTypes.OnPrem;
|
||||
|
||||
return _context.Templates.Where(t => t.CloudTemplate == environment.CloudEnabled).ToList();
|
||||
return _context.Templates
|
||||
.Include(t => t.TemplateCategory)
|
||||
.ThenInclude(tc => tc.Service)
|
||||
.Where(t => t.TemplateCategory.Service.IsCloudService == isCloudEnvironment)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public bool CheckEnvironmentById(Guid Id)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
||||
using Microsoft.SelfService.Portal.Core.API.Models;
|
||||
|
||||
@@ -15,34 +16,103 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
|
||||
public ICollection<ServiceModel> GetServices()
|
||||
{
|
||||
return _context.Services
|
||||
.ToList();
|
||||
return _context.Services.ToList();
|
||||
}
|
||||
|
||||
public ServiceModel GetServiceById(Guid Id)
|
||||
{
|
||||
return _context.Services
|
||||
.Where(s=>s.Id == Id)
|
||||
.FirstOrDefault();
|
||||
.Include(service => service.RoleDefinitions)
|
||||
.FirstOrDefault(service => service.Id == Id);
|
||||
}
|
||||
|
||||
public bool AddServiceById(ServiceModel service)
|
||||
{
|
||||
_context.Add(service);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool EditServiceById(ServiceModel service)
|
||||
{
|
||||
_context.Update(service);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool DeleteServiceById(ServiceModel service)
|
||||
{
|
||||
_context.Remove(service);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool CheckServiceById(Guid Id)
|
||||
{
|
||||
return _context.Services
|
||||
.Any(s =>s.Id == Id);
|
||||
return _context.Services.Any(service => service.Id == Id);
|
||||
}
|
||||
|
||||
public ServiceModel GetServiceByName(string Name)
|
||||
{
|
||||
return _context.Services
|
||||
.Where(s => s.Name == Name)
|
||||
.FirstOrDefault();
|
||||
return _context.Services.FirstOrDefault(service => service.Name == Name);
|
||||
}
|
||||
|
||||
public bool CheckServiceByName(string Name)
|
||||
{
|
||||
return _context.Services
|
||||
.Any(s => s.Name == Name);
|
||||
return _context.Services.Any(service => service.Name == Name);
|
||||
}
|
||||
|
||||
public ICollection<ServiceRoleDefinitionModel> GetRoleDefinitionsByServiceId(Guid serviceId)
|
||||
{
|
||||
return _context.ServiceRoleDefinitions
|
||||
.Where(roleDefinition => roleDefinition.ServiceId == serviceId)
|
||||
.OrderBy(roleDefinition => roleDefinition.Name)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ServiceRoleDefinitionModel GetRoleDefinitionById(Guid roleDefinitionId)
|
||||
{
|
||||
return _context.ServiceRoleDefinitions
|
||||
.FirstOrDefault(roleDefinition => roleDefinition.Id == roleDefinitionId);
|
||||
}
|
||||
|
||||
public bool AddRoleDefinition(ServiceRoleDefinitionModel roleDefinition)
|
||||
{
|
||||
_context.ServiceRoleDefinitions.Add(roleDefinition);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool EditRoleDefinition(ServiceRoleDefinitionModel roleDefinition)
|
||||
{
|
||||
_context.ServiceRoleDefinitions.Update(roleDefinition);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool DeleteRoleDefinition(ServiceRoleDefinitionModel roleDefinition)
|
||||
{
|
||||
_context.ServiceRoleDefinitions.Remove(roleDefinition);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool CheckRoleDefinitionById(Guid roleDefinitionId)
|
||||
{
|
||||
return _context.ServiceRoleDefinitions.Any(roleDefinition => roleDefinition.Id == roleDefinitionId);
|
||||
}
|
||||
|
||||
public bool CheckRoleDefinitionKey(Guid serviceId, string key, Guid? excludeRoleDefinitionId = null)
|
||||
{
|
||||
var query = _context.ServiceRoleDefinitions
|
||||
.Where(roleDefinition => roleDefinition.ServiceId == serviceId && roleDefinition.Key == key);
|
||||
|
||||
if (excludeRoleDefinitionId.HasValue)
|
||||
{
|
||||
query = query.Where(roleDefinition => roleDefinition.Id != excludeRoleDefinitionId.Value);
|
||||
}
|
||||
|
||||
return query.Any();
|
||||
}
|
||||
|
||||
public bool SaveChanges()
|
||||
{
|
||||
var saved = _context.SaveChanges();
|
||||
return saved > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
76
Repository/TemplateCategoryRepository.cs
Normal file
76
Repository/TemplateCategoryRepository.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
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 TemplateCategoryRepository : ITemplateCategoryInterface
|
||||
{
|
||||
private readonly DataContext _context;
|
||||
|
||||
public TemplateCategoryRepository(DataContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public ICollection<TemplateCategoryModel> GetTemplateCategories()
|
||||
{
|
||||
return _context.TemplateCategories
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public TemplateCategoryModel GetTemplateCategoryById(Guid Id)
|
||||
{
|
||||
return _context.TemplateCategories
|
||||
.AsNoTracking()
|
||||
.Where(tc => tc.Id == Id)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool AddTemplateCategoryById(TemplateCategoryModel templateCategory)
|
||||
{
|
||||
_context.Add(templateCategory);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool EditTemplateCategoryById(TemplateCategoryModel templateCategory)
|
||||
{
|
||||
_context.Update(templateCategory);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool DeleteTemplateCategoryById(TemplateCategoryModel templateCategory)
|
||||
{
|
||||
_context.Remove(templateCategory);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool CheckTemplateCategoryById(Guid Id)
|
||||
{
|
||||
return _context.TemplateCategories
|
||||
.Any(tc => tc.Id == Id);
|
||||
}
|
||||
|
||||
public TemplateCategoryModel GetTemplateCategoryByName(string Name)
|
||||
{
|
||||
return _context.TemplateCategories
|
||||
.AsNoTracking()
|
||||
.Where(tc => tc.Name == Name)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool CheckTemplateCategoryByName(string Name)
|
||||
{
|
||||
return _context.TemplateCategories
|
||||
.Any(tc => tc.Name == Name);
|
||||
}
|
||||
|
||||
public bool SaveChanges()
|
||||
{
|
||||
var saved = _context.SaveChanges();
|
||||
return saved > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
||||
using Microsoft.SelfService.Portal.Core.API.Models;
|
||||
|
||||
@@ -16,20 +17,60 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
public ICollection<TemplateModel> GetTemplates()
|
||||
{
|
||||
return _context.Templates
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public TemplateModel GetTemplateById(Guid Id)
|
||||
{
|
||||
return _context.Templates
|
||||
.AsNoTracking()
|
||||
.Where(t => t.Id == Id)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool AddTemplateById(TemplateModel template)
|
||||
{
|
||||
_context.Add(template);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool EditTemplateById(TemplateModel template)
|
||||
{
|
||||
_context.Update(template);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool DeleteTemplateById(TemplateModel template)
|
||||
{
|
||||
_context.Remove(template);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool CheckTemplateById(Guid Id)
|
||||
{
|
||||
return _context.Templates
|
||||
.Any(t => t.Id == Id);
|
||||
}
|
||||
}
|
||||
|
||||
public TemplateModel GetTemplateByName(string Name)
|
||||
{
|
||||
return _context.Templates
|
||||
.AsNoTracking()
|
||||
.Where(t => t.Name == Name)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool CheckTemplateByName(string Name)
|
||||
{
|
||||
return _context.Templates
|
||||
.Any(t => t.Name == Name);
|
||||
}
|
||||
|
||||
public bool SaveChanges()
|
||||
{
|
||||
var saved = _context.SaveChanges();
|
||||
return saved > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
public ICollection<VirtualMachineModel> GetVirtualMachines()
|
||||
{
|
||||
return _context.VirtualMachines
|
||||
.Include(virtualMachine => virtualMachine.Domain)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -29,6 +30,31 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool AddVirtualMachineById(VirtualMachineModel virtualMachine)
|
||||
{
|
||||
_context.Add(virtualMachine);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool EditVirtualMachineById(VirtualMachineModel virtualMachine)
|
||||
{
|
||||
_context.Update(virtualMachine);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public bool DeleteVirtualMachineById(VirtualMachineModel virtualMachine)
|
||||
{
|
||||
_context.Remove(virtualMachine);
|
||||
return SaveChanges();
|
||||
}
|
||||
|
||||
public VirtualMachineModel GetVirtualMachineByName(string Name)
|
||||
{
|
||||
return _context.VirtualMachines
|
||||
.Where(v => v.Name == Name)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public bool CheckVirtualMachineById(Guid Id)
|
||||
{
|
||||
return _context.VirtualMachines
|
||||
@@ -40,5 +66,11 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
return _context.VirtualMachines
|
||||
.Any(v => v.Name == Name);
|
||||
}
|
||||
|
||||
public bool SaveChanges()
|
||||
{
|
||||
var saved = _context.SaveChanges();
|
||||
return saved > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user