public async Task<Scene> CreateSceneAsync(Scene scene) { List<Appliance> usedAppliances = (await GetAppliancesAsync(scene.ApplianceSettings.Select(s => s.Appliance.Id))).ToList(); foreach (ApplianceSetting setting in scene.ApplianceSettings) setting.Appliance = usedAppliances.SingleOrDefault(a => a.Id == setting.Appliance.Id); if (scene.Schedule == null) scene.Schedule = new Schedule(); if (scene.Sequence == null) scene.Sequence = new Sequence(); Scene sceneToCreate = Context.Scenes.Add(scene); await SaveAsync(); return sceneToCreate; }
public async Task<Scene> UpdateSceneAsync(Scene scene) { Scene sceneToUpdate = (await GetScenesAsync(new List<int> { scene.Id })).SingleOrDefault(); if (sceneToUpdate == null) return null; if (scene.ApplianceSettings != null) { List<Appliance> usedAppliances = (await GetAppliancesAsync(scene.ApplianceSettings.Select(s => s.Appliance.Id))).ToList(); foreach (ApplianceSetting setting in scene.ApplianceSettings) setting.Appliance = usedAppliances.SingleOrDefault(a => a.Id == setting.Appliance.Id); sceneToUpdate.ApplianceSettings = scene.ApplianceSettings; } if (scene.Name != null) sceneToUpdate.Name = scene.Name; if (scene.Schedule != null) sceneToUpdate.Schedule = scene.Schedule; if (scene.Sequence != null) sceneToUpdate.Sequence = scene.Sequence; await SaveAsync(); return sceneToUpdate; }