public static Models.ValidationResult ValidateComputer(Models.Computer computer, string type)
{
var validationResult = new Models.ValidationResult {IsValid = false};
if (type == "new" || type == "update")
{
if (string.IsNullOrEmpty(computer.Name) || computer.Name.Any(c => c == ' '))
{
validationResult.Message = "Computer Name Is Not Valid";
return validationResult;
}
if (string.IsNullOrEmpty(computer.Mac) ||
!computer.Mac.All(c => char.IsLetterOrDigit(c) || c == ':' || c == '-')
&& (computer.Mac.Length == 12 && !computer.Mac.All(char.IsLetterOrDigit)))
{
validationResult.Message = "Computer Mac Is Not Valid";
return validationResult;
}
if (type == "new")
{
using (var uow = new DAL.UnitOfWork())
{
if (uow.ComputerRepository.Exists(h => h.Name == computer.Name || h.Mac == computer.Mac))
{
validationResult.Message = "This Computer Already Exists";
return validationResult;
}
}
}
else
{
using (var uow = new DAL.UnitOfWork())
{
var originalComputer = uow.ComputerRepository.GetById(computer.Id);
if (originalComputer.Name != computer.Name)
{
if (uow.ComputerRepository.Exists(h => h.Name == computer.Name))
{
validationResult.Message = "This Computer Already Exists";
return validationResult;
}
}
else if (originalComputer.Mac != computer.Mac)
{
if (uow.ComputerRepository.Exists(h => h.Mac == computer.Mac))
{
validationResult.Message = "This Computer Already Exists";
return validationResult;
}
}
}
}
}
if (type == "delete")
{
if (BLL.ActiveImagingTask.IsComputerActive(computer.Id))
{
validationResult.Message = "A Computer Cannot Be Deleted While It Has An Active Task";
return validationResult;
}
}
validationResult.IsValid = true;
return validationResult;
}