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

119 lines
3.7 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 ServiceRepository : IServiceInterface
{
private readonly DataContext _context;
public ServiceRepository(DataContext context)
{
_context = context;
}
public ICollection<ServiceModel> GetServices()
{
return _context.Services.ToList();
}
public ServiceModel GetServiceById(Guid Id)
{
return _context.Services
.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(service => service.Id == Id);
}
public ServiceModel GetServiceByName(string Name)
{
return _context.Services.FirstOrDefault(service => service.Name == Name);
}
public bool CheckServiceByName(string 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;
}
}
}