From 2735c703839a7d75ff19c75d08982654c01ee75e Mon Sep 17 00:00:00 2001 From: Maxime BATISTA Date: Fri, 12 May 2023 15:25:57 +0200 Subject: [PATCH] add simple persistence system for recipes --- Endpoint/Services.csproj | 13 +++++++++ LocalEndpoint/LocalEndpoint.cs | 6 ++-- LocalEndpoint/LocalServices.csproj | 14 ++++++++++ LocalEndpoint/RecipesDatabase.cs | 40 ++++++++++++++++++++++++--- Models/Ingredient.cs | 19 +++++++++++-- Models/PreparationStep.cs | 18 ++++++++++-- Models/Recipe.cs | 26 +++++++++++++---- Models/RecipeInfo.cs | 37 +++++++++++++++++++------ Models/User.cs | 19 +++++++++++-- Platforms/Android/AndroidManifest.xml | 1 + Views/FavoritesPage.xaml.cs | 2 -- 11 files changed, 167 insertions(+), 28 deletions(-) create mode 100644 Endpoint/Services.csproj create mode 100644 LocalEndpoint/LocalServices.csproj diff --git a/Endpoint/Services.csproj b/Endpoint/Services.csproj new file mode 100644 index 0000000..d24baee --- /dev/null +++ b/Endpoint/Services.csproj @@ -0,0 +1,13 @@ + + + + net7.0 + enable + enable + + + + + + + diff --git a/LocalEndpoint/LocalEndpoint.cs b/LocalEndpoint/LocalEndpoint.cs index 9b27f1e..ea7ad3e 100644 --- a/LocalEndpoint/LocalEndpoint.cs +++ b/LocalEndpoint/LocalEndpoint.cs @@ -17,15 +17,15 @@ namespace LocalEndpoint public LocalEndpoint() { - RecipesDatabase db = new RecipesDatabase(); + RecipesDatabase db = new RecipesDatabase(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/recipes.xaml"); //miam - db.Insert(new Recipe(new RecipeInfo("Chicken Salad", 500, 20, new Uri("https://healthyfitnessmeals.com/wp-content/uploads/2021/04/Southwest-chicken-salad-7-500x500.jpg"), 4, Guid.NewGuid()), Constants.USER1, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); + /*db.Insert(new Recipe(new RecipeInfo("Chicken Salad", 500, 20, new Uri("https://healthyfitnessmeals.com/wp-content/uploads/2021/04/Southwest-chicken-salad-7-500x500.jpg"), 4, Guid.NewGuid()), Constants.USER1, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); db.Insert(new Recipe(new RecipeInfo("Chocolate Cake", 2500, 10, new Uri("https://bakewithshivesh.com/wp-content/uploads/2022/08/IMG_0248-scaled.jpg"), 3, Guid.NewGuid()), Constants.USER2, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); db.Insert(new Recipe(new RecipeInfo("Salmon", 20, 10, new Uri("https://www.wholesomeyum.com/wp-content/uploads/2021/06/wholesomeyum-Pan-Seared-Salmon-Recipe-13.jpg"), 4, Guid.NewGuid()), Constants.MAIN_USER_ACCOUNT.User, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); db.Insert(new Recipe(new RecipeInfo("Fish", 50, 30, new Uri("https://www.ciaanet.org/wp-content/uploads/2022/07/Atlantic-and-Pacific-whole-salmon-1024x683.jpg"), 4.5F, Guid.NewGuid()), Constants.MAIN_USER_ACCOUNT.User, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); db.Insert(new Recipe(new RecipeInfo("Space Cake", 800, 5, new Uri("https://static.youmiam.com/images/recipe/1500x1000/space-cake-22706?placeholder=web_recipe&sig=f14a7a86da837c6b8cc678cde424d6d5902f99ec&v3"), 5, Guid.NewGuid()), Constants.USER3, new List { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList())); - + */ recipesService = new RecipesService(db); } diff --git a/LocalEndpoint/LocalServices.csproj b/LocalEndpoint/LocalServices.csproj new file mode 100644 index 0000000..29b3db6 --- /dev/null +++ b/LocalEndpoint/LocalServices.csproj @@ -0,0 +1,14 @@ + + + + net7.0 + enable + enable + + + + + + + + diff --git a/LocalEndpoint/RecipesDatabase.cs b/LocalEndpoint/RecipesDatabase.cs index 511cd80..70aba26 100644 --- a/LocalEndpoint/RecipesDatabase.cs +++ b/LocalEndpoint/RecipesDatabase.cs @@ -1,15 +1,45 @@ using Models; using System.Collections.Generic; using System.Collections.Immutable; - +using System.Runtime.Serialization; + namespace LocalEndpoint { - //Simple class to simulate a recipe database internal class RecipesDatabase { - private Dictionary recipes = new Dictionary(); + private static readonly DataContractSerializer RECIPES_SERIALIZER = new DataContractSerializer(typeof(List)); + + private readonly Dictionary recipes = new Dictionary(); + private readonly string dbPath; + + public RecipesDatabase(string path) + { + dbPath = path; + + + if (!File.Exists(dbPath)) + { + File.Create(dbPath); + } + if (new FileInfo(dbPath).Length == 0) + return; //file is empty thus there is nothing to deserialize + Console.WriteLine(File.ReadAllText(dbPath)); + + using (var stream = File.OpenRead(dbPath)) { + var recipes = RECIPES_SERIALIZER.ReadObject(stream) as List; + recipes.ForEach(recipe => this.recipes.Add(recipe.Info.Id, recipe)); + } + } + + private void SaveAll() + { + using (var stream = File.OpenWrite(dbPath)) + { + RECIPES_SERIALIZER.WriteObject(stream, recipes.Values.ToList()); + } + } public Recipe? Lookup(Guid id) { @@ -26,12 +56,14 @@ namespace LocalEndpoint public void Insert(Recipe recipe) { - recipes[recipe.Info.Id] = recipe; + recipes[recipe.Info.Id] = recipe; + SaveAll(); } public void Remove(Guid id) { recipes.Remove(id); + SaveAll(); } public ImmutableList ListAll() diff --git a/Models/Ingredient.cs b/Models/Ingredient.cs index 3a40a75..7caaa9d 100644 --- a/Models/Ingredient.cs +++ b/Models/Ingredient.cs @@ -1,4 +1,19 @@ -namespace Models +using System.Runtime.Serialization; + +namespace Models { - public record Ingredient(string Name, float Amount); + [DataContract] + public class Ingredient + { + [DataMember] + public string Name { get; init; } + [DataMember] + public float Amount { get; init; } + + public Ingredient(string name, float amount) + { + Name = name; + Amount = amount; + } + } } diff --git a/Models/PreparationStep.cs b/Models/PreparationStep.cs index 40899a7..779f797 100644 --- a/Models/PreparationStep.cs +++ b/Models/PreparationStep.cs @@ -1,5 +1,19 @@ -namespace Models +using System.Runtime.Serialization; + +namespace Models { + [DataContract] + public record PreparationStep + { + [DataMember] + public string Name { get; init; } + [DataMember] + public string Description { get; init; } - public record PreparationStep(string Name, string Description); + public PreparationStep(string name, string description) + { + Name = name; + Description = description; + } + } } diff --git a/Models/Recipe.cs b/Models/Recipe.cs index e077c22..f6152bf 100644 --- a/Models/Recipe.cs +++ b/Models/Recipe.cs @@ -1,10 +1,26 @@ using System.Collections.Immutable; +using System.Runtime.Serialization; namespace Models { - public record Recipe( - RecipeInfo Info, - User Owner, - ImmutableList Ingredients, - ImmutableList Steps); + [DataContract] + public class Recipe + { + [DataMember] + public RecipeInfo Info { get; init; } + [DataMember] + public User Owner { get; init; } + [DataMember] + public ImmutableList Ingredients { get; init; } + [DataMember] + public ImmutableList Steps { get; init; } + + public Recipe(RecipeInfo info, User owner, ImmutableList ingredients, ImmutableList steps) + { + Info = info; + Owner = owner; + Ingredients = ingredients; + Steps = steps; + } + } } \ No newline at end of file diff --git a/Models/RecipeInfo.cs b/Models/RecipeInfo.cs index 1265c96..2dfbbcc 100644 --- a/Models/RecipeInfo.cs +++ b/Models/RecipeInfo.cs @@ -1,10 +1,31 @@ -namespace Models +using System.Runtime.Serialization; + +namespace Models { - public record RecipeInfo( - string Name, - uint CalPerPers, - uint CookTimeMins, - Uri? Image, - float AverageNote, - Guid Id); + [DataContract] + public class RecipeInfo + { + [DataMember] + public string Name { get; init; } + [DataMember] + public uint CalPerPers { get; init; } + [DataMember] + public uint CookTimeMins { get; init; } + [DataMember] + public Uri? Image { get; init; } + [DataMember] + public float AverageNote { get; init; } + [DataMember] + public Guid Id { get; init; } + + public RecipeInfo(string name, uint calPerPers, uint cookTimeMins, Uri? image, float averageNote, Guid id) + { + Name = name; + CalPerPers = calPerPers; + CookTimeMins = cookTimeMins; + Image = image; + AverageNote = averageNote; + Id = id; + } + } } diff --git a/Models/User.cs b/Models/User.cs index 981cef6..2273a93 100644 --- a/Models/User.cs +++ b/Models/User.cs @@ -1,4 +1,19 @@ -namespace Models +using System.Runtime.Serialization; + +namespace Models { - public record User(Uri ProfilePicture, string Name); + [DataContract] + public class User + { + [DataMember] + public Uri ProfilePicture { get; init; } + [DataMember] + public string Name { get; init; } + + public User(Uri profilePicture, string name) + { + ProfilePicture = profilePicture; + Name = name; + } + } } diff --git a/Platforms/Android/AndroidManifest.xml b/Platforms/Android/AndroidManifest.xml index 60c24e0..7871058 100644 --- a/Platforms/Android/AndroidManifest.xml +++ b/Platforms/Android/AndroidManifest.xml @@ -3,4 +3,5 @@ + \ No newline at end of file diff --git a/Views/FavoritesPage.xaml.cs b/Views/FavoritesPage.xaml.cs index 8282f86..20b9ef2 100644 --- a/Views/FavoritesPage.xaml.cs +++ b/Views/FavoritesPage.xaml.cs @@ -3,10 +3,8 @@ using Models; namespace ShoopNCook.Pages; using Endpoint; -using LocalEndpoint; using Models; using ShoopNCook.Views; -using System.Security.Principal; public partial class FavoritesPage : ContentPage {