public IActionResult DeleteActive(ActiveReadingDeleteViewModel model) { if (model.DeviceId == null) { return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound })); } Device device = _deviceRepo.Get(model.DeviceId); if (device == null) { return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound })); } if (!ModelState.IsValid) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ValidationFailed })); } if (model.Id == null) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ActiveReadingNotFound })); } try { ActiveReading activeReading = _readingsRepo.GetActiveReading(model.Id); string authUserId = GetCurrentUserAsync().Result.Id; if (!activeReading.Device.User.UserId.Equals(authUserId)) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.NotAuthorized })); } _readingsRepo.DeleteActive(activeReading); _logger.LogInformation("User deleted one Active Reading."); return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.DeleteActiveReadingSuccess })); } catch (Exception ex) { _logger.LogInformation(ex.Message); return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.DeleteActiveReadingFailed })); } }
public IActionResult DeleteActive(string deviceId, string readingId) { if (deviceId == null) { return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound })); } Device device = _deviceRepo.Get(deviceId); if (device == null) { return(RedirectToAction("Index", "Device", new { armessage = ActiveReadingMessageId.DeviceNotFound })); } if (readingId == null) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ActiveReadingNotFound })); } ActiveReading reading = _readingsRepo.GetActiveReading(readingId); if (reading == null) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ActiveReadingNotFound })); } if (!reading.Device.User.UserId.Equals(GetCurrentUserAsync().Result.Id)) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.NotAuthorized })); } ViewData["deviceName"] = device.Hostname; var model = new ActiveReadingDeleteViewModel { Id = reading.Id, Name = reading.Name, DeviceId = device.Id }; return(PartialView(model)); }