Ejemplo n.º 1
0
        //kreiranje uredjaja
        //Front nam salje Id - ukoliko nam posalje Id = 0 (to je prvi korak u kreiranju na onom mokapu sto imamo) kreiramo novi uredjaj
        //bilo koji drugi Id radimo apdejt
        //Id nam salju u trenutku klikom na Save, kada popune sve sto je potreno za kreiranje novog Device-a
        /// <summary>
        /// Creates or update devices.
        /// </summary>
        /// <param name="createDto">The create dto.</param>
        public void CreateUpdateDevices(DeviceCreateDto createDto)
        {
            //dio koji se odnosi na Step 2 - Properties gdje unosimo Ime i Opis uredjaja i za selektovani tip dobijamo spisak propertija
            //i ukoliko ima nestovanih tipova da imamo i spisak nestovanih propertija
            var device = new DeviceCreateDto()
            {
                DeviceName  = createDto.DeviceName,
                Description = createDto.Description,
                DeviceTypes = createDto.DeviceTypes
            };

            foreach (var deviceType in device.DeviceTypes)
            {
                var listaDeviceType = new DeviceTypeCreateDeviceDto()
                {
                    DeviceTypeId = deviceType.DeviceTypeId,
                    PropValues   = new List <PropertyValuesCreateDeviceDto>()
                };

                foreach (var listaDevTypeProp in listaDeviceType.PropValues)
                {
                    var listaDeviceTypeProperties = new PropertyValuesCreateDeviceDto()
                    {
                        PropName = listaDevTypeProp.PropName,
                        Value    = listaDevTypeProp.Value
                    };
                }

                if (createDto.Id == 0)
                {
                    var novi = new Models.Device()
                    {
                        Name                 = device.DeviceName,
                        Description          = device.Description,
                        DevicePropertyValues = new List <Models.DevicePropertyValue>()
                    };

                    var tipovi = createDto.DeviceTypes;

                    foreach (var tip in tipovi)
                    {
                        foreach (var properiValues in tip.PropValues)
                        {
                            var pId = _repositoryDeviceTypeProperty.GetAll().Include(c => c.DeviceType).First(c => c.Name == properiValues.PropName && c.DeviceTypeId == tip.DeviceTypeId).Id;
                            novi.DevicePropertyValues.Add(new Models.DevicePropertyValue()
                            {
                                DeviceId             = novi.Id,
                                Value                = properiValues.Value,
                                DeviceTypePropertyId = pId
                            });
                        }
                        novi.DeviceTypeId = createDto.DeviceTypes.Max(c => c.DeviceTypeId);
                        _repositoryDevice.Insert(novi);
                    }
                }

                else
                {
                    var updateDevice = _repositoryDevice.GetAll().Include(c => c.DevicePropertyValues).FirstOrDefault(c => c.Id == createDto.Id);
                    updateDevice.Name        = createDto.DeviceName;
                    updateDevice.Description = createDto.Description;

                    var tipovi = createDto.DeviceTypes;
                    foreach (var tip in tipovi)
                    {
                        foreach (var prop in tip.PropValues)
                        {
                            var propValue = _repositoryDevicePropertyValue.GetAll().Include(c => c.Device)
                                            .Include(x => x.DeviceTypeProperty);

                            var deviceTake = propValue.Where(c =>
                                                             c.DeviceId == updateDevice.Id && c.DeviceTypeProperty.Name == prop.PropName).First();

                            deviceTake.Value = prop.Value;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Create(DeviceCreateDto input)
        {
            var device = new DeviceCreateDto();

            device.DeviceName  = input.DeviceName;
            device.Description = input.Description;
            device.DeviceTypes = input.DeviceTypes;
            foreach (var deviceType in input.DeviceTypes)
            {
                var prList = new DeviceTypeCreateDeviceDto();
                prList.DeviceTypeId = deviceType.DeviceTypeId;

                prList.PropValues = new List <PropertyValuesCreateDeviceDto>();
                foreach (var propValue in prList.PropValues)
                {
                    var prValues = new PropertyValuesCreateDeviceDto();
                    prValues.PropName = propValue.PropName;
                    prValues.Value    = propValue.Value;
                }
            }

            if (input.DeviceId == 0)
            {
                var newDevice = new Models.Device();
                newDevice.Name                = device.DeviceName;
                newDevice.Description         = device.Description;
                newDevice.DevicePropertyValue = new List <DevicePropertyValue>();
                var types = input.DeviceTypes;
                foreach (var type in types)
                {
                    foreach (var prop in type.PropValues)
                    {
                        var propId = _devicePropRepository.GetAll().Include(x =>
                                                                            x.DeviceType).First(y => y.Name == prop.PropName && y.DeviceTypeId == type.DeviceTypeId).Id;
                        newDevice.DevicePropertyValue.Add(new DevicePropertyValue()
                        {
                            DeviceId             = newDevice.Id,
                            Value                = prop.Value,
                            DeviceTypePropertyId = propId
                        });
                    }
                    newDevice.DeviceTypeId = input.DeviceTypes.Max(x => x.DeviceTypeId);

                    _deviceRepository.Insert(newDevice);
                }
            }
            else
            {
                var oldDevice = _deviceRepository.GetAll().Include(x => x.DevicePropertyValue).FirstOrDefault(x => x.Id == input.DeviceId);
                oldDevice.Name        = input.DeviceName;
                oldDevice.Description = input.Description;

                var types = input.DeviceTypes;
                foreach (var type in types)
                {
                    foreach (var prop in type.PropValues)
                    {
                        var getPropValue = _devicePropValueRepository.GetAll().Include(x => x.Device)
                                           .Include(x => x.DeviceTypeProperty);

                        var getExistingDevice = getPropValue.Where(x =>
                                                                   x.DeviceId == oldDevice.Id && x.DeviceTypeProperty.Name == prop.PropName).First();

                        getExistingDevice.Value = prop.Value;
                    }
                }
            }
        }