add RecipeType and change ctors of Recipes

pull/65/head
Alexandre AGOSTINHO 2 years ago
parent a0e536c9cb
commit 596e699382

@ -39,12 +39,13 @@ namespace ConsoleApp.Menu
Recipe recipe = new Recipe(
title: title,
type: RecipeType.Unspecified,
id: null,
authorMail: masterMgr.CurrentConnectedUser?.Mail,
picture: "",
ingredients: new List<Ingredient>(),
preparationSteps: steps.ToArray()
);
picture: null)
{
PreparationSteps = steps
};
masterMgr.AddRecipe(recipe);
return null;

@ -37,6 +37,7 @@ namespace DataPersistence
KnownTypes = new[]
{
typeof(Recipe),
typeof(RecipeType),
typeof(Review),
typeof(User),
typeof(Ingredient),

@ -45,6 +45,7 @@ namespace DataPersistence
KnownTypes = new Type[]
{
typeof(Recipe),
typeof(RecipeType),
typeof(Review),
typeof(User),
typeof(Ingredient),

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DataPersistence
@ -21,77 +22,123 @@ namespace DataPersistence
nameof(Recipe),
new List<object>(new[]
{
new Recipe(
title: "Cookies classiques",
id: 50,
authorMail: "admin@mctg.fr",
picture : "room_service_icon.png",
ingredients: new List<Ingredient>(new[]
new Recipe("Cookies classiques", RecipeType.Dessert, null, "admin@mctg.fr", "")
{
Ingredients = new List<Ingredient>
{
new Ingredient("Patates", new Quantity(23, Unit.unit)),
new Ingredient("Farine", new Quantity(23, Unit.G))
}),
preparationSteps: new[]
},
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Faire cuire."),
new PreparationStep(2, "Manger.")
}),
},
Reviews = new List<Review>
{
new Review(4, "Bonne recette, je recommande !"),
new Review(3, "Bof bof, mais mangeable...")
}
},
new Recipe(
authorMail: "admin@mctg.fr",
title: "Cookies au chocolat",
id: null,
preparationSteps: new[]
type: RecipeType.Dessert)
{
Ingredients = new List<Ingredient>
{
new Ingredient("Farine", new Quantity(200, Unit.G))
},
PreparationSteps = new List<PreparationStep>()
{
new PreparationStep(1, "Moulinez la pâte."),
new PreparationStep(2, "Faire cuire pendant une bonne heure."),
new PreparationStep(3, "Sortir du four et mettre dans un plat.")
}),
}
},
new Recipe(
title: "Gateau nature", id: null,
authorMail: "admin@mctg.fr",
preparationSteps: new[]
title: "Gateau nature",
type: RecipeType.Dessert)
{
Ingredients = new List<Ingredient>
{
new Ingredient("Farine", new Quantity(200, Unit.G)),
new Ingredient("Lait", new Quantity(2, Unit.L))
},
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Achetez les ingrédients."),
new PreparationStep(2, "Préparez le matériel. Ustensiles et tout."),
new PreparationStep(3, "Pleurez.")
}),
},
Reviews = new List<Review>
{
new Review("pedrosamigos@hotmail.com", 5, "C'était vraiment IN-CROY-ABLE !!!")
}
},
new Recipe(
title: "Gateau au pommes", id: null,
authorMail: "admin@mctg.fr",
preparationSteps: new[]
title: "Gateau au pommes",
type: RecipeType.Dessert)
{
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Achetez les légumes."),
new PreparationStep(2, "Préparez le plat. Ustensiles et préchauffez le four."),
new PreparationStep(3, "Coupez les pommes en morceaux et disposez-les sur le plat."),
new PreparationStep(4, "Mettez enfin le plat au four, puis une fois cuit, dégustez !")
}),
}
},
new Recipe(
title: "Gateau au chocolat", id: null,
authorMail: "pedrosamigos@hotmail.com",
preparationSteps: new[]
title: "Gateau au chocolat",
type: RecipeType.Dessert,
id: null, authorMail: "pedrosamigos@hotmail.com")
{
Ingredients = new List<Ingredient>
{
new Ingredient("Mais", new Quantity(2, Unit.kG)),
new Ingredient("Sachet pépites de chocolat", new Quantity(1, Unit.unit)),
new Ingredient("Dinde", new Quantity(2, Unit.G))
},
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Ajouter les oeufs."),
new PreparationStep(2, "Ajouter la farine."),
new PreparationStep(3, "Ajouter 100g de chocolat fondu."),
new PreparationStep(4, "Mélanger le tout."),
new PreparationStep(5, "Faire cuire 45h au four traditionnel.")
}),
}
},
new Recipe(
title: "Dinde au jambon",
id: null,
authorMail: "pedrosamigos@hotmail.com",
preparationSteps: new[]
type: RecipeType.Dish,
id: null, authorMail: "pedrosamigos@hotmail.com")
{
Ingredients = new List<Ingredient>
{
new Ingredient("Morceaux de bois", new Quantity(2, Unit.unit)),
new Ingredient("Sachet gélatine", new Quantity(1, Unit.unit)),
new Ingredient("Jambon", new Quantity(2, Unit.kG))
},
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Faire une cuisson bien sec de la dinde à la poêle"),
new PreparationStep(2, "Mettre la dinde au frigo."),
new PreparationStep(3, "Mettre le jambon dans le micro-onde."),
new PreparationStep(4, "Faire chauffer 3min."),
new PreparationStep(5, "Présentez sur un plat la dinde et le jambon : Miam !")
}),
}
},
new Recipe(
title: "Poulet au curry", id: null,
authorMail: "pedrosamigos@hotmail.com",
preparationSteps: new[]
title: "Poulet au curry",
type: RecipeType.Dish,
id: null, authorMail: "pedrosamigos@hotmail.com")
{
Ingredients = new List<Ingredient>
{
new Ingredient("Pissenlis", new Quantity(200, Unit.unit)),
new Ingredient("Boule de pétanque", new Quantity(10, Unit.unit)),
new Ingredient("Poivre", new Quantity(4, Unit.mG))
},
PreparationSteps = new List<PreparationStep>
{
new PreparationStep(1, "Trouvez des épices de curry."),
new PreparationStep(2, "Trouvez maintenant du poulet."),
@ -99,7 +146,12 @@ namespace DataPersistence
new PreparationStep(4, "Parsemez d'épices curry la tête de la poule."),
new PreparationStep(5, "Mettre le tout au four traditionnel 30min."),
new PreparationStep(6, "Dégustez en famille !")
})
},
Reviews = new List<Review>
{
new Review(5, "Meilleure recette que j'ai avalé de tout les temps !!!!!!!")
}
}
})
#endregion
},

@ -148,7 +148,7 @@ namespace Model.Managers
/// <returns>The current connected user's personal recipes.</returns>
public RecipeCollection GetCurrentUserRecipes()
=> new RecipeCollection("User recipes",
DataMgr.GetRecipes().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray());
DataMgr.GetRecipes().ToList().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray());
/// <summary>
/// Notify property change handler.

@ -19,6 +19,9 @@ namespace Model
[DataMember(Name = "image")]
private string _image = "";
[DataMember(Name = "authorMail")]
private string _authorMail = "";
#endregion
#region Properties
@ -32,15 +35,22 @@ namespace Model
/// List of reviews of this recipe.
/// </summary>
[DataMember(Name = "reviews")]
public List<Review> Reviews { get; private set; }
public List<Review> Reviews { get; set; }
/// <summary>
/// AuthorMail's mail of the recipe.
/// </summary>
[DataMember(Name = "authorMail")]
public string? AuthorMail { get; private set; }
public string Toto { get; set; } = "Coucou";
public string? AuthorMail
{
get => _authorMail;
set
{
if (string.IsNullOrEmpty(value))
_authorMail = "admin@mctg.fr";
else
_authorMail = value;
}
}
/// <summary>
/// The Title of the recipe. <br/>
@ -67,12 +77,14 @@ namespace Model
get => _image;
set
{
if (!string.IsNullOrWhiteSpace(value))
if (string.IsNullOrWhiteSpace(value))
_image = "room_service_icon.png";
else
_image = value;
}
}
/// <summary>
/// The list of ingredients.
/// </summary>
[DataMember(Name = "ingredient")]
@ -84,30 +96,34 @@ namespace Model
/// </summary>
[DataMember(Name = "preparation-steps")]
public List<PreparationStep> PreparationSteps { get; set; }
/// <summary>
/// The type of recipe.
/// </summary>
[DataMember(Name = "type")]
public RecipeType Type { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Construct a new recipe.
/// </summary>
/// <param _name="title">The title of the recipe</param>
/// <param _name="id">The id of the recipe. If not given, get a new id.</param>
/// <param _name="authorMail">The name of the user that create this recipe.</param>
/// <param _name="picture"> The image that represent the recipe</param>
/// <param _name="reviews">Thr list of reviews.</param>
/// <param _name="ingredients">Thr list of ingredients.</param>
/// <param _name="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string title, int? id, string? authorMail, string? picture,
List<Review> reviews, List<Ingredient> ingredients,
params PreparationStep[] preparationSteps)
/// <param name="title">The title of the recipe</param>
/// <param name="type">The type of the recipe.</param>
/// <param name="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="authorMail">The name of the user that create this recipe.</param>
/// <param name="picture"> The image that represent the recipe</param>
public Recipe(string title, RecipeType type, int? id, string? authorMail, string? picture)
{
Title = title;
Type = type;
Image = picture;
PreparationSteps = new List<PreparationStep>(preparationSteps);
Ingredients = ingredients;
Reviews = reviews;
AuthorMail = authorMail;
PreparationSteps = new List<PreparationStep>();
Ingredients = new List<Ingredient>();
Reviews = new List<Review>();
if (id == null)
{
var randomGenerator = RandomNumberGenerator.Create();
@ -118,46 +134,20 @@ namespace Model
else Id = (int)id;
}
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param _name="title">The title of the recipe.</param>
/// <param _name="id">The id of the recipe. If not given, get a new id.</param>
/// <param _name="authorMail">Mail of the user that create the recipe</param>
/// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id, string? authorMail, params PreparationStep[] preparationSteps)
: this(title, id, authorMail, null, new List<Review>(), new List<Ingredient>(), preparationSteps)
public Recipe(string title, RecipeType type, int? id, string? authorMail)
: this(title, type, id, authorMail, null)
{
}
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param _name="title">The title of the recipe.</param>
/// <param _name="id">The id of the recipe. If not given, get a new id.</param>
/// <param _name="authorMail">Mail of the user that create the recipe</param>
/// <param _name="picture">Mail of the user that create the recipe</param>
/// <param _name="ingredients">List of ingredients that compose the recipe. </param>
/// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id, string? authorMail, string? picture, List<Ingredient> ingredients, params PreparationStep[] preparationSteps)
: this(title, id, authorMail, picture, new List<Review>(), ingredients, preparationSteps)
public Recipe(string title, RecipeType type)
: this(title, type, null, null)
{
}
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param _name="title">The title of the recipe.</param>
/// <param _name="id">The id of the recipe. If not given, get a new id.</param>
/// <param _name="picture">Image that reppresent the recipe.</param>
/// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe()
: this("", null, null, null, new List<Review>(), new List<Ingredient>(),new PreparationStep[0])
public Recipe(string title)
: this(title, RecipeType.Unspecified)
{
}
#endregion
#region Methods

@ -8,9 +8,10 @@ namespace Model
{
/// <summary>
/// Define a collection of <see cref="Recipe"/>.
/// <br/>This class implement <see cref="IList"/> and <see cref="IEquatable{T}"/>.
/// <br/>This class is derived from <see cref="ObservableCollection{Recipe}"/>
/// and implement <see cref="IEquatable{RecipeCollection}"/> and <see cref="ICloneable"/>.
/// </summary>
public class RecipeCollection : List<Recipe>, IEquatable<RecipeCollection>
public class RecipeCollection : ObservableCollection<Recipe>, IEquatable<RecipeCollection>, ICloneable
{
#region Attributes
private string _description = "";
@ -56,7 +57,8 @@ namespace Model
/// <exception cref="ArgumentException"/>
public Recipe? GetRecipeById(int id)
{
Recipe? recipe = this.Find(r => r.Id == id);
Recipe? recipe = this.ToList().Find(r => r.Id == id);
if (recipe == null) throw new ArgumentException("No _recipes match the given id.");
return recipe;
}
@ -73,7 +75,7 @@ namespace Model
return new RecipeCollection(
description: $"Results of the research: {str}",
recipes: this.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray());
recipes: this.ToList().FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray());
}
public virtual bool Equals(RecipeCollection? other)
@ -104,6 +106,13 @@ namespace Model
}
return sb.ToString();
}
public object Clone()
{
return new RecipeCollection(
description: this.Description,
recipes: this.ToArray());
}
#endregion
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public enum RecipeType
{
Unspecified,
Starter,
Dish,
Dessert
}
}

@ -8,9 +8,8 @@ namespace Model_UnitTests
public void TestVoidConstructor()
{
Recipe r = new Recipe(
title: "test recipe",
id: null,
authorMail: "test@test.fr");
title: "test recipe", type: RecipeType.Unspecified);
Assert.NotNull(r.Title);
}
}

@ -17,13 +17,16 @@ namespace Views
{
public partial class App : Application
{
private Recipe currentRecipe { get; set; }
/// <summary>
/// Master manager - access to the Model.
/// </summary>
public MasterManager MasterMgr { get; private set; } = new MasterManager(new Stubs());
//L'utilisateur courant de l'application
public User CurrentUser { get; set; }
private Recipe currentRecipe { get; set; }
/// <summary>
/// Current selected recipe.
/// </summary>
public Recipe CurrentRecipe
{
get => currentRecipe;

@ -31,7 +31,8 @@
<Button
Text="Entrées"
ImageSource="flatware_icon.png"
Style="{StaticResource button1}"/>
Style="{StaticResource button1}"
Clicked="Entrees_Clicked"/>
<Button
Text="Plats"
ImageSource="room_service_icon.png"
@ -73,7 +74,6 @@
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
</StackLayout>
</ScrollView>

@ -2,6 +2,7 @@
using DataPersistence;
using Model;
using Model.Managers;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace Views
@ -12,8 +13,8 @@ namespace Views
public User? user => (App.Current as App).CurrentUser;
public RecipeCollection AllRecipe => (App.Current as App).AllRecipes;
public RecipeCollection RecipesDisplayed { get; set; }
private readonly RecipeCollection _recipesDisplayed;
public ReadOnlyObservableCollection<Recipe> RecipesDisplayed { get; private set; }
public static readonly BindableProperty IsNotConnectedProperty =
BindableProperty.Create("IsNotConnected", typeof(bool), typeof(bool));
@ -36,7 +37,8 @@ namespace Views
public Home()
{
RecipesDisplayed = MasterMgr.DataMgr.GetRecipes("Suggestions");
_recipesDisplayed = (RecipeCollection)AllRecipe.Clone();
RecipesDisplayed = new ReadOnlyObservableCollection<Recipe>(_recipesDisplayed);
InitializeComponent();
BindingContext = this;
IsNotConnected = true;
@ -45,24 +47,36 @@ namespace Views
public Home(RecipeCollection recipesDisplayed)
{
RecipesDisplayed = recipesDisplayed;
_recipesDisplayed = recipesDisplayed;
InitializeComponent();
BindingContext = this;
IsNotConnected = true;
NeedReturn = true;
}
private async void SearchBar_SearchButtonPressed(object sender, EventArgs e)
private void ModifyRecipesDisplayed(RecipeCollection recipes)
{
_recipesDisplayed.Clear();
foreach (Recipe recipe in recipes)
{
_recipesDisplayed.Add(recipe);
}
}
private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
{
string searchStr = (sender as SearchBar).Text;
await Navigation.PushModalAsync(new Home(AllRecipe.ResearchByName(searchStr)));
ModifyRecipesDisplayed(AllRecipe.ResearchByName(searchStr));
}
public void OnImageClicked(object sender, EventArgs e)
{
(App.Current as App).CurrentRecipe = (Recipe)(sender as ImageButton).BindingContext;
Navigation.PushModalAsync(new ViewRecette());
}
private void Entrees_Clicked(object sender, EventArgs e)
{
return;
}
}
}

Loading…
Cancel
Save