public IActionResult DeleteFromDevice(string deviceId, string readingId)
        {
            if (deviceId == null)
            {
                return(RedirectToAction("Index", "Device", new { rmessage = ReadingMessageId.DeviceNotFound }));
            }

            Device device = _deviceRepo.Get(deviceId);

            if (device == null)
            {
                return(RedirectToAction("Index", "Device", new { rmessage = ReadingMessageId.DeviceNotFound }));
            }

            if (readingId == null)
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.ReadingNotFound }));
            }

            Reading reading = _readingsRepo.GetReading(readingId);

            if (reading == null)
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.ReadingNotFound }));
            }

            if (!reading.Owner.UserId.Equals(GetCurrentUserAsync().Result.Id))
            {
                return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.NotAuthorized }));
            }

            ViewData["deviceName"] = device.Hostname;
            var model = new ReadingDeleteFromDeviceViewModel
            {
                Id        = reading.Id,
                Name      = reading.Name,
                DeviceId  = device.Id,
                ReadValue = reading.ReadValue,
                Timestamp = reading.Timestamp
            };

            ViewData["readingType"] = reading.ReadingType.Name;

            return(PartialView(model));
        }
        public IActionResult DeleteFromDevice(ReadingDeleteFromDeviceViewModel model)
        {
            if (model.DeviceId == null)
            {
                return(RedirectToAction("Index", "Device", new { rmessage = ReadingMessageId.DeviceNotFound }));
            }

            Device device = _deviceRepo.Get(model.DeviceId);

            if (device == null)
            {
                return(RedirectToAction("Index", "Device", new { rmessage = ReadingMessageId.DeviceNotFound }));
            }

            if (model.Id == null)
            {
                return(RedirectToAction("Readings", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.ReadingNotFound }));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Readings", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.ValidationFailed }));
            }

            try
            {
                Reading reading = _readingsRepo.GetReading(model.Id);

                string authUserId = GetCurrentUserAsync().Result.Id;
                if (!reading.Owner.UserId.Equals(authUserId))
                {
                    return(RedirectToAction("Readings", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.NotAuthorized }));
                }

                _readingsRepo.Delete(reading);
                _logger.LogInformation("User deleted one Reading.");
                return(RedirectToAction("Readings", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.DeleteReadingSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("Readings", "Device", new { DeviceId = device.Id, rmessage = ReadingMessageId.DeleteReadingFailed }));
            }
        }