public IActionResult Settings([FromRoute] int devId, [FromForm] string name, [FromForm] int delaySeconds, [FromForm] string ownerId) { Device device = context.Devices.Find(devId); if (device == null) { return(NotFound()); } if (!IsAuthorized(device)) { return(Forbid()); } if (device.OwnerId != ownerId && !User.IsInRole(Role.Admin)) { return(Forbid()); } device.Name = name; device.DelaySeconds = delaySeconds; device.OwnerId = ownerId; context.Update(device); context.SaveChanges(); return(Redirect($"/{devId}")); }
public IActionResult Settings([FromRoute] int devId, [FromRoute] int senId, [FromForm] string name, [FromForm] string unit, [FromForm] bool hasAlert, [FromForm] Alert alert) { Sensor sensor = context.Sensors.Find(devId, senId); if (sensor == null) { return(NotFound()); } if (!IsAuthorized(sensor)) { return(Forbid()); } context.Entry(sensor).Reference(s => s.Alert).Load(); sensor.Name = name; sensor.Unit = unit; if (hasAlert && sensor.Alert != null) { sensor.Alert.Sign = alert.Sign; sensor.Alert.Limit = alert.Limit; sensor.Alert.Email = alert.Email; } else if (hasAlert && sensor.Alert == null) { sensor.Alert = alert; } else if (!hasAlert && sensor.Alert != null) { context.Remove(sensor.Alert); } context.Update(sensor); context.SaveChanges(); return(Redirect($"/{devId}/{senId}")); }