diff --git a/LocalServices/Data/CatastrophicPerformancesDatabase.cs b/LocalServices/Data/CatastrophicPerformancesDatabase.cs index 32c48c1..0d490a0 100644 --- a/LocalServices/Data/CatastrophicPerformancesDatabase.cs +++ b/LocalServices/Data/CatastrophicPerformancesDatabase.cs @@ -5,6 +5,8 @@ using System.Collections.Immutable; using System.Linq; using System.Runtime.Serialization; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace LocalEndpoint.Data @@ -62,9 +64,11 @@ namespace LocalEndpoint.Data Save(ACCOUNTS_FILENAME, ACCOUNTS_SERIALIZER, accountsData); } - public Recipe GetRecipe(Guid id) + public Recipe? GetRecipe(Guid id) { - return ConvertRecipeDataToRecipe(recipesData[id]); + if (recipesData.TryGetValue(id, out RecipeData? data)) + return ConvertRecipeDataToRecipe(data); + return null; } public RecipeRate GetRecipeRate(Guid user, Guid recipe) @@ -141,19 +145,16 @@ namespace LocalEndpoint.Data if (fileInfo.Length == 0) return new Dictionary(); //file is empty thus there is nothing to deserialize - Console.WriteLine(File.ReadAllText(file)); - using (var stream = File.OpenRead(file)) - return deserializer.ReadObject(stream) as Dictionary ?? throw new Exception("object read from " + file + " is not a dictionnary"); + string text = File.ReadAllText(file); + + return JsonSerializer.Deserialize>(text); } private void Save(string fileName, DataContractSerializer serializer, Dictionary dict) { - using (var stream = File.OpenWrite(dbPath + "/" + fileName)) - { - serializer.WriteObject(stream, dict); - stream.Flush(); - } + string json = JsonSerializer.Serialize(dict); + File.WriteAllText(dbPath + "/" + fileName, json); } } diff --git a/LocalServices/Data/Database.cs b/LocalServices/Data/Database.cs index 130093a..03f3975 100644 --- a/LocalServices/Data/Database.cs +++ b/LocalServices/Data/Database.cs @@ -9,7 +9,7 @@ namespace LocalEndpoint.Data public interface Database { - public Recipe GetRecipe(Guid id); + public Recipe? GetRecipe(Guid id); public RecipeRate GetRecipeRate(Guid user, Guid recipe); diff --git a/LocalServices/RecipesService.cs b/LocalServices/RecipesService.cs index 8969e9e..f47b19e 100644 --- a/LocalServices/RecipesService.cs +++ b/LocalServices/RecipesService.cs @@ -21,7 +21,7 @@ namespace LocalEndpoint return db.ListAllRecipes().Take(4).ToImmutableList().ConvertAll(v => v.Info); } - public Recipe GetRecipe(RecipeInfo info) + public Recipe? GetRecipe(RecipeInfo info) { return db.GetRecipe(info.Id); } diff --git a/Services/IRecipesService.cs b/Services/IRecipesService.cs index 88e8b65..232da18 100644 --- a/Services/IRecipesService.cs +++ b/Services/IRecipesService.cs @@ -8,7 +8,7 @@ namespace Endpoint { public ImmutableList PopularRecipes(); - public Recipe GetRecipe(RecipeInfo info); + public Recipe? GetRecipe(RecipeInfo info); public IAccountOwnedRecipes GetRecipesOf(Account account); public IAccountRecipesPreferences GetPreferencesOf(Account account); diff --git a/Views/FavoritesPage.xaml.cs b/Views/FavoritesPage.xaml.cs index 20b9ef2..fa9bf2a 100644 --- a/Views/FavoritesPage.xaml.cs +++ b/Views/FavoritesPage.xaml.cs @@ -31,7 +31,7 @@ public partial class FavoritesPage : ContentPage { RecipeViewLayout.Children.Add(new RecipeView(info, () => { - Recipe recipe = service.GetRecipe(info); + Recipe? recipe = service.GetRecipe(info); Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); })); }); diff --git a/Views/HomePage.xaml.cs b/Views/HomePage.xaml.cs index b74cfd2..3b46cb2 100644 --- a/Views/HomePage.xaml.cs +++ b/Views/HomePage.xaml.cs @@ -20,8 +20,13 @@ public partial class HomePage : ContentPage { layout.Children.Add(new RecipeView(info, () => { - Recipe recipe = service.GetRecipe(info); - Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); + Recipe? recipe = service.GetRecipe(info); + if (recipe != null) + Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1)); + else + { + notifier.Error("Could not find recipe"); + } })); }