add simple persistence system for recipes
continuous-integration/drone/push Build is passing Details

pull/52/head
Maxime BATISTA 2 years ago
parent e2df22c858
commit 2735c70383

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>
</Project>

@ -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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { 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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { 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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { 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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { 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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { 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<Ingredient> { new Ingredient("Ingredient 1", 6) }.ToImmutableList(), new List<PreparationStep> { new PreparationStep("Step 1", "Bake the eggs") }.ToImmutableList()));
*/
recipesService = new RecipesService(db);
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Endpoint\Services.csproj" />
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>
</Project>

@ -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<Guid, Recipe> recipes = new Dictionary<Guid, Recipe>();
private static readonly DataContractSerializer RECIPES_SERIALIZER = new DataContractSerializer(typeof(List<Recipe>));
private readonly Dictionary<Guid, Recipe> recipes = new Dictionary<Guid, Recipe>();
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<Recipe>;
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)
{
@ -27,11 +57,13 @@ namespace LocalEndpoint
public void Insert(Recipe recipe)
{
recipes[recipe.Info.Id] = recipe;
SaveAll();
}
public void Remove(Guid id)
{
recipes.Remove(id);
SaveAll();
}
public ImmutableList<Recipe> ListAll()

@ -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;
}
}
}

@ -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;
}
}
}

@ -1,10 +1,26 @@
using System.Collections.Immutable;
using System.Runtime.Serialization;
namespace Models
{
public record Recipe(
RecipeInfo Info,
User Owner,
ImmutableList<Ingredient> Ingredients,
ImmutableList<PreparationStep> Steps);
[DataContract]
public class Recipe
{
[DataMember]
public RecipeInfo Info { get; init; }
[DataMember]
public User Owner { get; init; }
[DataMember]
public ImmutableList<Ingredient> Ingredients { get; init; }
[DataMember]
public ImmutableList<PreparationStep> Steps { get; init; }
public Recipe(RecipeInfo info, User owner, ImmutableList<Ingredient> ingredients, ImmutableList<PreparationStep> steps)
{
Info = info;
Owner = owner;
Ingredients = ingredients;
Steps = steps;
}
}
}

@ -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;
}
}
}

@ -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;
}
}
}

@ -3,4 +3,5 @@
<application android:allowBackup="true" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

@ -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
{

Loading…
Cancel
Save