예제 #1
0
        // ---------------------------- CREATE OR UPDATE DEVICE ----------------------------//
        // --------------------------------- STEP 2 (last) ------------------------------//

        public UpdateDeviceDto GetDeviceTypesWithPropValues(int deviceId, int deviceTypeId)
        {
            var device = _deviceRepository.GetAll().Include(x => x.DeviceType).Include(x => x.DevicePropertyValues)
                         .ThenInclude(x => x.DeviceTypeProperty).First(x => x.Id == deviceId);

            UpdateDeviceDto updatedDevice = new UpdateDeviceDto
            {
                DeviceId    = deviceId,
                DeviceName  = device.Name,
                Description = device.Description,
                TypeId      = deviceTypeId,
                DeviceTypes = new List <UpdateDeviceTypesPropValuesDto>()
            };

            var typesForView = new List <UpdateDeviceTypesPropValuesDto>();

            var newTypes = _typeService.GetDeviceTypeWithParents(deviceTypeId).ToList();

            foreach (var newType in newTypes)
            {
                var typeForView = new UpdateDeviceTypesPropValuesDto
                {
                    DeviceTypeId   = newType.Id,
                    DeviceTypeName = newType.Name,
                    PropValues     = new List <UpdateDevicePropValueDto>()
                };

                typesForView.Add(typeForView);

                foreach (var property in newType.DeviceTypeProperties)
                {
                    var propValueForView = new UpdateDevicePropValueDto
                    {
                        PropName = property.Name,
                        Required = property.IsRequired,
                        Type     = property.Type
                    };

                    typeForView.PropValues.Add(propValueForView);

                    var valueForView =
                        device.DevicePropertyValues.FirstOrDefault(x => x.DeviceTypePropertyId == property.Id);

                    if (valueForView == null)
                    {
                        propValueForView.Value = null;
                        continue;
                    }

                    propValueForView.Value = valueForView.Value;
                }
            }

            updatedDevice.DeviceTypes = typesForView.OrderBy(x => x.DeviceTypeId).ToList();

            return(updatedDevice);
        }