Merge pull request 'feature/21-ingredient' (#47) from feature/21-ingredient into dev
continuous-integration/drone/push Build is passing Details

Reviewed-on: #47
pull/49/head
Alexandre AGOSTINHO 2 years ago
commit b6729c5f68

@ -5,6 +5,7 @@
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

@ -11,11 +11,11 @@ Console.WriteLine("Hello, World!\n\n");
// TESTS: // TESTS:
//DataManager dataMgr = new DataManager(new Stubs()); DataManager dataMgr = new DataManager(new Stubs());
DataManager dataMgr = new DataManager(new DataContractXML()); //DataManager dataMgr = new DataManager(new DataContractXML());
//DataManager dataMgr = new DataManager(new DataContractJSON()); //DataManager dataMgr = new DataManager(new DataContractJSON());
//dataMgr.Serializer = new DataContractXML(); dataMgr.Serializer = new DataContractXML();
//dataMgr.Serializer = new DataContractJSON(); //dataMgr.Serializer = new DataContractJSON();
// /!\ here is an absolute path I put for testing purpose. It will only work on my computer so don't forget to change it whene you test. // /!\ here is an absolute path I put for testing purpose. It will only work on my computer so don't forget to change it whene you test.
@ -23,9 +23,11 @@ DataManager dataMgr = new DataManager(new DataContractXML());
//dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.json"); //dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.json");
PasswordManager passwordManager = new PasswordManager(); PasswordManager passwordManager = new PasswordManager();
RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast<Recipe>().ToArray()); //RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast<Recipe>().ToArray());
RecipeCollection rc = dataMgr.GetRecipes("All recipes");
//RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.GetFromData<Recipe>().ToArray());
User user = dataMgr.Data[nameof(User)].Cast<User>().Last(); User user = dataMgr.GetUsers().Last();
//rc[0].AddReview(new Review(user, 1, "bonne recette !1")); //rc[0].AddReview(new Review(user, 1, "bonne recette !1"));
//rc[0].AddReview(new Review(user, 1, "bonne recette !2")); //rc[0].AddReview(new Review(user, 1, "bonne recette !2"));

@ -38,7 +38,9 @@ namespace DataPersistence
{ {
typeof(Recipe), typeof(Recipe),
typeof(Review), typeof(Review),
typeof(User) typeof(User),
typeof(Ingredient),
typeof(Quantity)
} }
}; };
else else

@ -46,7 +46,9 @@ namespace DataPersistence
{ {
typeof(Recipe), typeof(Recipe),
typeof(Review), typeof(Review),
typeof(User) typeof(User),
typeof(Ingredient),
typeof(Quantity)
}, },
PreserveObjectReferences = true PreserveObjectReferences = true
}; };

@ -25,6 +25,11 @@ namespace DataPersistence
{ {
new Recipe( new Recipe(
title: "Cookies classiques", id: null, title: "Cookies classiques", id: null,
ingredients: new List<Ingredient>(new[]
{
new Ingredient("Patates", new Quantity(23, Unit.unit)),
new Ingredient("Farine", new Quantity(23, Unit.G))
}),
preparationSteps: new[] preparationSteps: new[]
{ {
new PreparationStep(1, "Faire cuire."), new PreparationStep(1, "Faire cuire."),

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.Ingredient
{
internal class Ingredient
{
}
}

@ -5,7 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DataPersistence namespace Model
{ {
/// <summary> /// <summary>
/// Define the manager of the data. This is where all the data are put, and where we call the loading and the saving of them. /// Define the manager of the data. This is where all the data are put, and where we call the loading and the saving of them.
@ -76,6 +76,30 @@ namespace DataPersistence
public void Export<T>(T obj, string pathToExport) public void Export<T>(T obj, string pathToExport)
where T : class where T : class
=> Serializer.Export<T>(obj, pathToExport); => Serializer.Export<T>(obj, pathToExport);
/// <summary>
/// Get all the recipe from the data.
/// </summary>
/// <param name="rcTitle">The title to give for the Recipe Collection</param>
/// <returns>A RecipeCollection that contain all the recipe in the data.</returns>
public RecipeCollection GetRecipes(string rcTitle = "default")
=> new RecipeCollection(rcTitle, Data[nameof(Recipe)].Cast<Recipe>().ToArray());
/// <summary>
/// Get all the Users from the data.
/// </summary>
/// <returns>A list of all Users.</returns>
public List<User> GetUsers()
=> new List<User>(Data[nameof(User)].Cast<User>());
/// <summary>
/// Get a list of an item in the data.
/// </summary>
/// <typeparam name="T">The type of the item</typeparam>
/// <returns>The list of all the item found in the data.</returns>
public ICollection<T> GetFromData<T>() where T : class
=> new List<T>(Data[typeof(T).Name].Cast<T>());
#endregion #endregion
} }
} }

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DataPersistence namespace Model
{ {
/// <summary> /// <summary>
/// Interface that define the methods of a data serializer. /// Interface that define the methods of a data serializer.

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
[DataContract(Name = "ingredient")]
public class Ingredient
{
#region Attributes
[DataMember(Name = "id")]
private string _name = "";
[DataMember(Name = "quantity")]
private Quantity _quantity = new Quantity(1, Unit.unit);
#endregion
#region Properties
/// <summary>
/// Property of the Ingredient's attribute _name. It raises an ArgumentNullException to prevent a nullable field.
/// </summary>
public string Name
{
get => _name;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException("Impossible de ne pas avoir de nom pour l'ingrédient");
}
_name = value;
}
}
/// <summary>
/// Property of the Ingredient's attribute Quantity.
/// </summary>
public Quantity QuantityI
{
get => _quantity;
set => _quantity = value;
}
public bool Equals(Ingredient? other)
{
if (other == null) { return false; }
if (this == other) { return true; }
return Name.Equals(other.Name);
}
#endregion
#region Constructor
public Ingredient(string name, Quantity quantity)
{
Name = name;
QuantityI = quantity;
}
#endregion
public override string ToString()
{
return $"{Name} ({QuantityI})";
}
}
}

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// Quantity is a class which is associate to Ingedients. It represents the _quantity of every ingredient with an enum
/// to indicate the unit of the _quantity.
/// </summary>
[DataContract(Name = "quantity")]
public class Quantity
{
#region Attributes
/// <summary>
/// get the quatity of ingredient
/// </summary>
[DataMember(Name = "digit")]
private int number;
/// <summary>
/// have the Unit enum of the _quantity of ingredient
/// </summary>
[DataMember(Name = "unit")]
private Unit unit;
#endregion
#region Properties
/// <summary>
/// Represents the _quantity in digits. The null value raise an argumentException.
/// </summary>
public int Number
{
get { return number; }
set
{
if (value < 0)
{
throw new ArgumentException("Si la quantité est inférieur à 0, enlever l'ingrédient!");
}
number = value;
}
}
public Unit UnitQ
{
get => unit;
set => unit = value;
}
#endregion
#region Constructor
/// <summary>
/// Constructor of Quantity
/// </summary>
/// <param _name="number"></param>
/// <param _name="unit"></param>
public Quantity(int number, Unit unit)
{
Number = number;
UnitQ = unit;
}
#endregion
public override string ToString()
{
return $"{Number}{UnitQ}";
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// Unit is an Enum that represents the units of quantities
/// <list type="Unit, kG, mG, G, L, cL, mL"/>
/// </summary>
public enum Unit
{ unit, kG, mG, G, L, cL, mL };
}

@ -46,8 +46,8 @@ namespace Model
/// <summary> /// <summary>
/// Construct a new step of preparation. /// Construct a new step of preparation.
/// </summary> /// </summary>
/// <param name="order">The number of the order in preparation</param> /// <param _name="order">The number of the order in preparation</param>
/// <param name="description">The description of the task</param> /// <param _name="description">The description of the task</param>
public PreparationStep(int order, string description = "") public PreparationStep(int order, string description = "")
{ {
Order = order; Order = order;

@ -31,6 +31,12 @@ namespace Model
[DataMember(Name = "reviews")] [DataMember(Name = "reviews")]
public List<Review> Reviews { get; private set; } public List<Review> Reviews { get; private set; }
/// <summary>
/// Author of the recipe.
/// </summary>
[DataMember(Name = "author")]
public User? Author { get; private set; }
/// <summary> /// <summary>
/// The Title of the recipe. <br/> /// The Title of the recipe. <br/>
/// Set to "No title." when the value passed is null, empty or contain white spaces. /// Set to "No title." when the value passed is null, empty or contain white spaces.
@ -47,6 +53,12 @@ namespace Model
} }
} }
/// <summary>
/// The list of ingredients.
/// </summary>
[DataMember(Name = "ingredient")]
public List<Ingredient> Ingredients { get; set; }
/// <summary> /// <summary>
/// The steps of the preparation. See: <see cref="PreparationStep"/>. /// The steps of the preparation. See: <see cref="PreparationStep"/>.
/// </summary> /// </summary>
@ -58,16 +70,20 @@ namespace Model
/// <summary> /// <summary>
/// Construct a new recipe. /// Construct a new recipe.
/// </summary> /// </summary>
/// <param name="title">The title of the recipe</param> /// <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="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal</param> /// <param _name="reviews">Thr list of reviews.</param>
public Recipe(string title, int? id, /// <param _name="ingredients">Thr list of ingredients.</param>
List<Review> reviews, /// <param _name="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string title, int? id, User? author,
List<Review> reviews, List<Ingredient> ingredients,
params PreparationStep[] preparationSteps) params PreparationStep[] preparationSteps)
{ {
Title = title; Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps); PreparationSteps = new List<PreparationStep>(preparationSteps);
Ingredients = ingredients;
Reviews = reviews; Reviews = reviews;
Author = author;
if (id == null) if (id == null)
{ {
@ -82,30 +98,43 @@ namespace Model
/// <summary> /// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/> /// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary> /// </summary>
/// <param name="title">The title of the recipe.</param> /// <param _name="title">The title of the recipe.</param>
public Recipe(string title) public Recipe(string title)
: this(title, null, new List<Review>()) : this(title, null, null, new List<Review>(), new List<Ingredient>())
{ {
} }
/// <summary> /// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/> /// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary> /// </summary>
/// <param name="title">The title of the recipe.</param> /// <param _name="title">The title of the recipe.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal.</param> /// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, params PreparationStep[] preparationSteps) public Recipe(string title, params PreparationStep[] preparationSteps)
: this(title, null, new List<Review>(), preparationSteps) : this(title, null, null, new List<Review>(), new List<Ingredient>(), preparationSteps)
{ {
} }
/// <summary> /// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/> /// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary> /// </summary>
/// <param name="title">The title of the recipe.</param> /// <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="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal.</param> /// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id, params PreparationStep[] preparationSteps) public Recipe(string title, int? id, params PreparationStep[] preparationSteps)
: this(title, id, new List<Review>(), preparationSteps) : this(title, id, null, new List<Review>(), new List<Ingredient>(), preparationSteps)
{
}
/// <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="ingredients">Thr list of ingredients.</param>
/// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id, List<Ingredient> ingredients,
params PreparationStep[] preparationSteps)
: this(title, id, null, new List<Review>(), ingredients, preparationSteps)
{ {
} }
#endregion #endregion
@ -114,7 +143,7 @@ namespace Model
/// <summary> /// <summary>
/// Add a review for the recipe. /// Add a review for the recipe.
/// </summary> /// </summary>
/// <param name="review">The new review to add.</param> /// <param _name="review">The new review to add.</param>
public void AddReview(Review review) public void AddReview(Review review)
=> Reviews.Add(review); => Reviews.Add(review);
@ -132,6 +161,22 @@ namespace Model
return sb.ToString(); return sb.ToString();
} }
/// <summary>
/// Concatenate the list of ingredients in a single string
/// </summary>
/// <returns>The list of ingredients in string format</returns>
private string ConcatIngredients()
{
StringBuilder sb = new StringBuilder();
foreach (Ingredient ingredient in Ingredients)
{
sb.Append("\t- " + ingredient.ToString() + "\n");
}
return sb.ToString();
}
public virtual bool Equals(Recipe? other) public virtual bool Equals(Recipe? other)
{ {
if (other == null) return false; if (other == null) return false;
@ -159,10 +204,14 @@ namespace Model
sb.AppendFormat("\t* {0}\n", ps.ToString()); sb.AppendFormat("\t* {0}\n", ps.ToString());
} }
sb.AppendLine(); sb.AppendLine();
sb.AppendLine(ConcatIngredients());
sb.AppendLine();
foreach (Review review in Reviews) foreach (Review review in Reviews)
{ {
sb.AppendLine(review.ToString()); sb.AppendLine(review.ToString());
} }
sb.AppendLine();
sb.AppendLine($"Posted by: {Author?.ToString()}");
return sb.ToString(); return sb.ToString();
} }
#endregion #endregion

@ -38,8 +38,8 @@ namespace Model
/// <summary> /// <summary>
/// Construct a new collection of _recipes. /// Construct a new collection of _recipes.
/// </summary> /// </summary>
/// <param name="description">A short description of what this list will contain</param> /// <param _name="description">A short description of what this list will contain</param>
/// <param name="recipes">Recipes to add in this new collection</param> /// <param _name="recipes">Recipes to add in this new collection</param>
public RecipeCollection(string description, params Recipe[] recipes) public RecipeCollection(string description, params Recipe[] recipes)
: base(recipes) : base(recipes)
{ {
@ -51,7 +51,7 @@ namespace Model
/// <summary> /// <summary>
/// Find a recipe in this list by giving the id. /// Find a recipe in this list by giving the id.
/// </summary> /// </summary>
/// <param name="id">The id of the list we are looking for</param> /// <param _name="id">The id of the list we are looking for</param>
/// <returns>The recipe of the id given</returns> /// <returns>The recipe of the id given</returns>
/// <exception cref="ArgumentException"/> /// <exception cref="ArgumentException"/>
public Recipe? GetRecipeById(int id) public Recipe? GetRecipeById(int id)
@ -62,9 +62,9 @@ namespace Model
} }
/// <summary> /// <summary>
/// Utility to find a recipe by his name. /// Utility to find a recipe by his _name.
/// </summary> /// </summary>
/// <param name="str">The string for the search</param> /// <param _name="str">The string for the search</param>
/// <returns>A collection of Recipe where their Title contain the string.</returns> /// <returns>A collection of Recipe where their Title contain the string.</returns>
public RecipeCollection ResearchByName(string str) public RecipeCollection ResearchByName(string str)
{ {

@ -11,7 +11,7 @@ using System.Runtime.Serialization;
namespace Model namespace Model
{ {
/// <summary> /// <summary>
/// A user is an entity with a name, a surname, mail, profilePict and a list of priority. /// A user is an entity with a _name, a surname, mail, profilePict and a list of priority.
/// This user can login with an Id and a password /// This user can login with an Id and a password
/// </summary> /// </summary>
[DataContract(Name = "user")] [DataContract(Name = "user")]
@ -137,9 +137,9 @@ namespace Model
/// <summary> /// <summary>
/// Construtors of user. /// Construtors of user.
/// </summary> /// </summary>
/// <param name="name">The name of the user</param> /// <param _name="name">The _name of the user</param>
/// <param name="surname">The surname of the user</param> /// <param _name="surname">The surname of the user</param>
/// <param name="mail">The user needs an email to login. </param> /// <param _name="mail">The user needs an email to login. </param>
public User(string name, string surname, string mail, int password) public User(string name, string surname, string mail, int password)
{ {
Name = name; Name = name;

@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290 VisualStudioVersion = 17.5.33516.290

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace Model_UnitTests
{
public class Ingredient_UT
{
public void TestConstructIngredient()
{
Quantity quantity = new Quantity(200, Unit.mL);
Ingredient patate = new Ingredient("Patate", quantity);
Assert.Equal("Patate", patate.Name);
}
}
}

@ -1,31 +0,0 @@
<<<<<<< HEAD
<Project Sdk="Microsoft.NET.Sdk">
=======
<Project Sdk="Microsoft.NET.Sdk">
>>>>>>> feature/18-model-user
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Model\Model.csproj" />
<ProjectReference Include="..\..\DataPersistence\DataPersistence.csproj" />
</ItemGroup>
</Project>

@ -24,3 +24,4 @@
<ProjectReference Include="..\..\DataPersistence\DataPersistence.csproj" /> <ProjectReference Include="..\..\DataPersistence\DataPersistence.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save