private DeviceConfigAction SaveDeviceConfigAction(DeviceConfig deviceConfig, string function, string tag, User user) { List <Customer> customerQuery = (from c in DB.GetTable <Customer>() where c.CustomerId == deviceConfig.CustomerId select c).ToList(); Customer customer = customerQuery.Count < 1 ? null : customerQuery[0]; if (customer == null) { throw new ServiceException( string.Format("Could not find customer with ID {0} for device with device id {1}.", deviceConfig.CustomerId, deviceConfig.DeviceId), ServiceResultCode.FatalError); } DeviceConfigAction result = new DeviceConfigAction() { DeviceConfigActionId = Guid.NewGuid(), Function = function, DeviceId = deviceConfig.DeviceId, ApplicationName = deviceConfig.ApplicationName, CustomerCompanyName = customer.CompanyName, Tag = tag, DeviceConfigId = deviceConfig.DeviceConfigId, DateCreated = DateTime.Now }; Save <DeviceConfigAction>(result, false).ForEach(c => HandleChange(c, user)); return(result); }
public ServiceFunctionResult <DeviceConfigAction> LogDeviceConfigAction( string function, string deviceId, string applicationName, string tag) { try { DeviceConfigAction result = null; using (TransactionScope t = new TransactionScope()) { /*We need a separate to get the entity in order to be able to update its LastConnectionDate, otherwise the underlying LINQConext * will not update the database because the "original entity" will be the same object as the "new entity we're getting here.*/ DeviceConfigContext ctx = new DeviceConfigContext(); List <DeviceConfig> deviceConfigQuery = (from d in ctx.DB.GetTable <DeviceConfig>() where d.DeviceId == deviceId && d.ApplicationName == applicationName select d).ToList(); DeviceConfig deviceConfig = deviceConfigQuery.Count < 1 ? null : deviceConfigQuery[0]; if (deviceConfig == null) { throw new ServiceException( string.Format( "Could not retrieve settings for application on device {0}. Device not registered. Attempt to get device configuration for device to be added as a device pending approval.", deviceId), ServiceResultCode.FatalError); } User systemUser = GetSystemUser(); deviceConfig.LastConnectionDate = DateTime.Now; if (deviceConfig.ActionCount.HasValue) { deviceConfig.ActionCount++; } else { deviceConfig.ActionCount = 1; } Save <DeviceConfig>(deviceConfig, false).ForEach(c => HandleChange(c, systemUser)); SaveDeviceConfigAction(deviceConfig, function, tag, systemUser); t.Complete(); } return(new ServiceFunctionResult <DeviceConfigAction>() { Contents = result }); } catch (Exception ex) { return(new ServiceFunctionResult <DeviceConfigAction>(HandleException(ex))); } }