Esempio n. 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);
        }
Esempio n. 2
0
        public IHttpActionResult GetUpdated(JObject jsonObject)
        {
            string   token       = jsonObject["token"].Value <string>();
            DateTime lastUpdated = jsonObject["lastUpdated"].Value <DateTime>();

            UpdateDeviceDto data = new UpdateDeviceDto();

            data.Events     = eventService.GetAll();
            data.Categories = categoryService.GetAll();
            data.Tickets    = ticketService.GetUpdated(lastUpdated);
            return(Ok(data));
        }
Esempio n. 3
0
        public async Task <IActionResult> PutDevice([FromRoute] int id, [FromBody] UpdateDeviceDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != dto.Id)
            {
                return(BadRequest());
            }

            var device = await _context.FindAsync <Device>(dto.Id);

            device = dto.Adapt(device);

            device.IPv4Address = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

            _context.Entry(device).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DeviceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }