public Result <object> AddDevice(DeviceInfoDataContract device) { var result = new Result <object>(); try { if (device == null) { throw new Exception("Новое устройство не задано"); } using (var ctx = new PolicyProjectEntities()) { device.DeviceId = ctx.tbl_device_info.Any() ? ctx.tbl_device_info.Max(x => x.id) + 1 : 1; var newDevice = DeviceInfoDataContract.FromDeviceInfoDataContractToTblDeviceInfo(device); ctx.tbl_device_info.Add(newDevice); result.BoolRes = ctx.SaveChanges() > 0; } } catch (Exception ex) { result.BoolRes = false; result.ErrorRes = string.Concat("Ошибка добавления устройствa. ", ex.Message); } return(result); }
public Result <object> DeleteDevice(DeviceInfoDataContract device) { var result = new Result <object>(); try { if (device == null) { throw new Exception("Yстройство не задано"); } using (var ctx = new PolicyProjectEntities()) { var delDevice = ctx.tbl_device_info.FirstOrDefault(x => device.DeviceId == x.id); ctx.tbl_device_info.Remove(delDevice); result.BoolRes = ctx.SaveChanges() > 0; } } catch (Exception ex) { result.BoolRes = false; result.ErrorRes = string.Concat("Ошибка удаления устройствa. ", ex.Message); } return(result); }
public Result <DeviceInfoDataContract[]> GetDevice(DeviceInfoDataContract deviceFilter) { var result = new Result <DeviceInfoDataContract[]>(); try { using (var ctx = new PolicyProjectEntities()) { if (deviceFilter == null) { result.SomeResult = ctx.tbl_device_info.Select( deviceTblData => new DeviceInfoDataContract { DeviceId = deviceTblData.id, DeviceName = deviceTblData.device_name, DeviceSerialNumber = deviceTblData.device_serial_number, DevicePlatformId = deviceTblData.device_platform_id, DeviceIpAddress = deviceTblData.device_ip_addr, DeviceMacAddress = deviceTblData.device_mac_addr, DevicePlatformName = ctx.tbl_platform.FirstOrDefault(x => x.id == deviceTblData.device_platform_id) .platform_name }).ToArray(); } else { result.SomeResult = ctx.tbl_device_info.Where(x => x.id == deviceFilter.DeviceId || x.device_serial_number.Equals( deviceFilter.DeviceSerialNumber, StringComparison .InvariantCultureIgnoreCase)) .Select(deviceTblData => new DeviceInfoDataContract { DeviceId = deviceTblData.id, DeviceName = deviceTblData.device_name, DeviceSerialNumber = deviceTblData.device_serial_number, DevicePlatformId = deviceTblData.device_platform_id, DeviceIpAddress = deviceTblData.device_ip_addr, DeviceMacAddress = deviceTblData.device_mac_addr, DevicePlatformName = ctx.tbl_platform.FirstOrDefault(x => x.id == deviceTblData.device_platform_id) .platform_name }) .ToArray(); } result.BoolRes = true; } } catch (Exception ex) { result.BoolRes = false; result.ErrorRes = string.Concat("Ошибка получения списка устройств. ", ex.Message); } return(result); }
public static tbl_device_info FromDeviceInfoDataContractToTblDeviceInfo(DeviceInfoDataContract deviceData) { if (deviceData.DeviceId < 1 || string.IsNullOrEmpty(deviceData.DeviceName) || string.IsNullOrEmpty(deviceData.DeviceSerialNumber)) { return(null); } return(new tbl_device_info { id = deviceData.DeviceId, device_name = deviceData.DeviceName, device_ip_addr = deviceData.DeviceIpAddress, device_mac_addr = deviceData.DeviceMacAddress, device_platform_id = deviceData.DevicePlatformId, device_serial_number = deviceData.DeviceSerialNumber }); }
public Result <object> UpdateDevice(DeviceInfoDataContract oldDevice, DeviceInfoDataContract newDevice) { var result = new Result <object>(); try { if (oldDevice == null) { throw new Exception("Текущее устройство не задано"); } if (newDevice == null) { throw new Exception("Новое устройство не задано"); } using (var ctx = new PolicyProjectEntities()) { var selected = ctx.tbl_device_info.FirstOrDefault(x => oldDevice.DeviceId == x.id); if (selected == null) { throw new Exception("Текущее устройство не найдено"); } selected.device_name = newDevice.DeviceName; selected.device_ip_addr = newDevice.DeviceIpAddress; selected.device_serial_number = newDevice.DeviceSerialNumber; selected.device_mac_addr = newDevice.DeviceMacAddress; selected.device_platform_id = newDevice.DevicePlatformId; result.BoolRes = ctx.SaveChanges() > 0; } } catch (Exception ex) { result.BoolRes = false; result.ErrorRes = string.Concat("Ошибка изменения устройствa. ", ex.Message); } return(result); }
public static bool Compare(DeviceInfoDataContract obj1, DeviceInfoDataContract obj2) { if (obj1 == null && obj2 == null) { return(true); } if (obj1 == null && obj2 != null) { return(false); } if (obj1 != null && obj2 == null) { return(false); } return(obj1.DeviceId == obj2.DeviceId && string.Equals(obj1.DeviceName.Trim(), obj2.DeviceName.Trim(), StringComparison.CurrentCultureIgnoreCase) && string.Equals(obj1.DeviceSerialNumber.Trim(), obj2.DeviceSerialNumber.Trim(), StringComparison.CurrentCultureIgnoreCase)); }
public string GetDeviceRest(DeviceInfoDataContract deviceFilter) { var queryResult = GetDevice(deviceFilter); return(JsonConvert.SerializeObject(queryResult)); }