using System.Text.Json; namespace OpenNest.Data; /// /// The machine configurations shared through the nest server, in the same JSON as /// 's files. Reads come from the store's last-read copy; saves /// and deletes go to the server and throw when it cannot take them. /// public sealed class SharedMachineProvider(SharedSettingsStore store) : IDataProvider { public IReadOnlyList GetMachines() => store.List(SharedSettingKinds.Machine) .Select(Read) .OfType() .Select(machine => new MachineSummary(machine.Id, machine.Name)) .ToList(); public MachineConfig? GetMachine(Guid id) => store.Get(SharedSettingKinds.Machine, id.ToString("D")) is { } record ? Read(record) : null; public void SaveMachine(MachineConfig machine) => store.Save(SharedSettingKinds.Machine, machine.Id.ToString("D"), Serialize(machine)); public void DeleteMachine(Guid id) => store.Delete(SharedSettingKinds.Machine, id.ToString("D")); /// A machine's shared document, the same JSON its local file holds. public static string Serialize(MachineConfig machine) => JsonSerializer.Serialize(machine, LocalJsonProvider.JsonOptions); private static MachineConfig? Read(SharedSettingRecord record) { try { return JsonSerializer.Deserialize(record.Json, LocalJsonProvider.JsonOptions); } catch (JsonException) { return null; } } }