//uzimanje liste propertija isto kao uzimanje ugnijezdenih tipova GetDeviceTypeNestedDtos
        public IEnumerable <DeviceTypePropertiesDto> GetDeviceTypePropertiesDto(int?deviceTypeId)
        {
            var deviceType = _repositoryDeviceType.GetAll().Include(c => c.DeviceTypeProperties)
                             .First(c => c.Id == deviceTypeId);


            var newDeviceTypePropertiesDto = new DeviceTypePropertiesDto()
            {
                Id          = deviceType.Id,
                Name        = deviceType.Name,
                Description = deviceType.Description,
                ParentId    = deviceType.ParentDeviceTypeId,
                Properties  = _objectMapper.Map <List <DeviceTypePropertyDto> >(deviceType.DeviceTypeProperties)
            };

            var result = new List <DeviceTypePropertiesDto>();

            if (deviceType.ParentDeviceTypeId == null)
            {
                result.Add(newDeviceTypePropertiesDto);
                return(result);
            }

            result.Add(newDeviceTypePropertiesDto);

            return(result.Concat(GetDeviceTypePropertiesDto(deviceType.ParentDeviceTypeId)).OrderBy(c => c.Id));
        }
예제 #2
0
        //------------------------GET FLAT LIST OF TYPES WITH PROPERTIES -------------------------//
        //----------- returns flat list of types, containing type with given id and parents------//

        public IEnumerable <DeviceTypePropertiesDto> GetDeviceTypesWithProperties(int?id)
        {
            var result = new List <DeviceTypePropertiesDto>();

            var type = _deviceTypeRepository.GetAll().Include(x => x.DeviceTypeProperties).Include(x => x.Devices).ThenInclude(x => x.DevicePropertyValues)
                       .First(x => x.Id == id);

            var currentType = new DeviceTypePropertiesDto
            {
                Id          = type.Id,
                Name        = type.Name,
                Description = type.Description,
                ParentId    = type.ParentDeviceTypeId,
                Properties  = ObjectMapper.Map <List <DeviceTypePropertyDto> >(type.DeviceTypeProperties)
            };

            if (_deviceTypeRepository.GetAll().Count() == 1)
            {
                result.Add(currentType);
                return(result);
            }

            if (type.ParentDeviceTypeId == null)
            {
                result.Add(currentType);
                return(result);
            }

            result.Add(currentType);

            return(result.Concat(GetDeviceTypesWithProperties(type.ParentDeviceTypeId)).OrderBy(x => x.Id));
        }
        public IEnumerable <DeviceTypePropertiesDto> GetDeviceTypesFlatList(int?deviceTypeId)
        {
            var type = _repositoryDeviceType.GetAll().Include(x => x.DeviceTypeProperties)
                       .First(x => x.Id == deviceTypeId);

            var result = new List <DeviceTypePropertiesDto>();

            var currentType = new DeviceTypePropertiesDto
            {
                Id          = type.Id,
                Name        = type.Name,
                Description = type.Description,
                ParentId    = type.ParentDeviceTypeId,
                Properties  = _objectMapper.Map <List <DeviceTypePropertyDto> >(type.DeviceTypeProperties)
            };

            if (type.ParentDeviceTypeId == null)
            {
                result.Add(currentType);
                return(result);
            }

            result.Add(currentType);

            return(result.Concat(GetDeviceTypesFlatList(type.ParentDeviceTypeId)).OrderBy(x => x.Id));
        }
예제 #4
0
        //---------------------- CREATE NEW TYPE -----------------------//
        public void CreateOrUpdateDeviceType(DeviceTypePropertiesDto input)
        {
            if (input.Id == 0)
            {
                var newDeviceType = new DeviceType
                {
                    Name               = input.Name,
                    Description        = input.Description,
                    ParentDeviceTypeId = input.ParentId
                };

                var newDeviceTypeId = _deviceTypeRepository.InsertAndGetId(newDeviceType);

                foreach (var property in input.Properties)
                {
                    _propertyRepository.Insert(new DeviceTypeProperty
                    {
                        Name         = property.NameProperty,
                        IsRequired   = property.Required,
                        Type         = property.Type,
                        DeviceTypeId = newDeviceTypeId
                    });
                }

                return;
            }

            var targetType = _deviceTypeRepository.GetAll().Include(x => x.DeviceTypeProperties)
                             .First(x => x.Id == input.Id);

            targetType.Name        = input.Name;
            targetType.Description = input.Description;
            //add option to change parent;
            targetType.ParentDeviceTypeId = input.ParentId;

            var updatedProperties = input.Properties.Where(x => x.DeviceTypeId == targetType.Id).ToList();

            foreach (var prop in targetType.DeviceTypeProperties)
            {
                var pr = updatedProperties.FirstOrDefault(x => x.Id == prop.Id);

                if (pr == null)
                {
                    _propertyRepository.Delete(prop);
                }
            }

            foreach (var property in updatedProperties)
            {
                if (property.Id == 0)
                {
                    _propertyRepository.Insert(new DeviceTypeProperty
                    {
                        Name         = property.NameProperty,
                        IsRequired   = property.Required,
                        Type         = property.Type,
                        DeviceTypeId = input.Id
                    });

                    continue;
                }

                var prop = _propertyRepository.Get(property.Id);
                ObjectMapper.Map(prop, property);
            }
        }