Device enrolment with no model information

An exception was previously thrown when a device lacking
manufacturer/model information attempted to enrol.
This commit is contained in:
Gary Sharp
2016-11-09 15:43:44 +11:00
parent c72c18e825
commit afa548fb70
3 changed files with 42 additions and 9 deletions
@@ -50,6 +50,21 @@ namespace Disco.Services
public static Tuple<DeviceModel, bool> GetOrCreateDeviceModel(this DbSet<DeviceModel> DeviceModelsSet, string Manufacturer, string Model, string ModelType)
{
if (string.IsNullOrWhiteSpace(Manufacturer))
Manufacturer = "Unknown";
else
Manufacturer = Manufacturer.Trim();
if (string.IsNullOrWhiteSpace(Model))
Model = "Unknown";
else
Model = Model.Trim();
if (string.IsNullOrWhiteSpace(ModelType))
ModelType = null;
else
ModelType = ModelType.Trim();
// Already Exists?
var deviceModel = DeviceModelsSet.FirstOrDefault(dm => dm.Manufacturer == Manufacturer && dm.Model == Model);
if (deviceModel == null)
@@ -65,12 +80,19 @@ namespace Disco.Services
// Create the Device Model in a different DataContext so we don't have to commit unrelated changes
using (DiscoDataContext database = new DiscoDataContext())
{
var description = $"{Manufacturer} {Model}";
if (Model.Equals("Unknown", StringComparison.OrdinalIgnoreCase))
{
description = $"Unknown {Manufacturer} Model";
}
var addDeviceModel = new DeviceModel
{
Manufacturer = Manufacturer,
Model = Model,
ModelType = ModelType,
Description = string.Format("{0} {1}", Manufacturer, Model)
Description = description
};
database.DeviceModels.Add(addDeviceModel);
database.SaveChanges();