Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user