public IActionResult InfoActive(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 model = _readingsRepo.GetActiveReadingWithReadings(readingId); if (model == null) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ActiveReadingNotFound })); } if (!model.Device.User.UserId.Equals(GetCurrentUserAsync().Result.Id)) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.NotAuthorized })); } return(PartialView(model)); }
public ActiveReading GetActiveReadingWithReadings(string readingId) { ActiveReading reading = _context.ActiveReadings .Include(ar => ar.Readings) .Include(ar => ar.Device) .ThenInclude(d => d.User) .SingleOrDefault(ar => readingId.Equals(ar.Id)); return(reading); }
public IActionResult AddActive(ActiveReadingAddViewModel 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 })); } string authUserId = GetCurrentUserAsync().Result.Id; if (!device.User.UserId.Equals(authUserId)) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.NotAuthorized })); } if (_readingsRepo.CheckReadingNameAlreadyExists(model.Name, authUserId)) { return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.ReadingNameAlreadyExists })); } try { var activeReading = new ActiveReading { Id = Guid.NewGuid().ToString(), Device = device, ActiveSince = DateTime.Now, Name = model.Name, DataFilepath = model.DataFilepath, ReadingType = _readingsRepo.GetReadingType(model.ReadingType), Owner = device.User }; _readingsRepo.AddActiveReading(activeReading); _logger.LogInformation("User activated a new reading."); return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.AddActiveReadingSuccess })); } catch (Exception ex) { _logger.LogInformation(ex.Message); return(RedirectToAction("Manage", "Device", new { DeviceId = device.Id, armessage = ActiveReadingMessageId.AddActiveReadingFailed })); } }
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)); }
public void DeleteActive(ActiveReading reading) { _context.ActiveReadings.Remove(reading); _context.SaveChanges(); }
public void AddActiveReading(ActiveReading reading) { _context.ActiveReadings.Add(reading); _context.SaveChanges(); }