Spaces:
Running
Running
| using System.Text.Json; | |
| namespace BakeryErp.Infrastructure.Database; | |
| public static class LocalDataStoreFile | |
| { | |
| private static readonly JsonSerializerOptions JsonOptions = new() | |
| { | |
| WriteIndented = true | |
| }; | |
| public static LocalDataStore Load(DatabaseOptions options) | |
| { | |
| if (!File.Exists(options.DatabasePath)) | |
| { | |
| return LocalDataStore.CreateSeed(); | |
| } | |
| var store = JsonSerializer.Deserialize<LocalDataStore>(File.ReadAllText(options.DatabasePath)) | |
| ?? LocalDataStore.CreateSeed(); | |
| store.EnsureSeedData(); | |
| return store; | |
| } | |
| public static void Save(DatabaseOptions options, LocalDataStore store) | |
| { | |
| var directory = Path.GetDirectoryName(options.DatabasePath); | |
| if (!string.IsNullOrWhiteSpace(directory)) | |
| { | |
| Directory.CreateDirectory(directory); | |
| } | |
| File.WriteAllText(options.DatabasePath, JsonSerializer.Serialize(store, JsonOptions)); | |
| } | |
| } | |