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,11 +5,12 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;CI</Configurations>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DataPersistence\DataPersistence.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ItemGroup>
<ProjectReference Include="..\DataPersistence\DataPersistence.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -11,11 +11,11 @@ Console.WriteLine("Hello, World!\n\n");
// TESTS:
//DataManager dataMgr = new DataManager(new Stubs());
DataManager dataMgr = new DataManager(new DataContractXML());
DataManager dataMgr = new DataManager(new Stubs());
//DataManager dataMgr = new DataManager(new DataContractXML());
//DataManager dataMgr = new DataManager(new DataContractJSON());
//dataMgr.Serializer = new DataContractXML();
dataMgr.Serializer = new DataContractXML();
//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.
@ -23,9 +23,11 @@ DataManager dataMgr = new DataManager(new DataContractXML());
//dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.json");
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 !2"));

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

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

@ -25,6 +25,11 @@ namespace DataPersistence
{
new Recipe(
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[]
{
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.Threading.Tasks;
namespace DataPersistence
namespace Model
{
/// <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.
@ -76,6 +76,30 @@ namespace DataPersistence
public void Export<T>(T obj, string pathToExport)
where T : class
=> 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
}
}

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataPersistence
namespace Model
{
/// <summary>
/// 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>
/// Construct a new step of preparation.
/// </summary>
/// <param name="order">The number of the order in preparation</param>
/// <param name="description">The description of the task</param>
/// <param _name="order">The number of the order in preparation</param>
/// <param _name="description">The description of the task</param>
public PreparationStep(int order, string description = "")
{
Order = order;

@ -31,6 +31,12 @@ namespace Model
[DataMember(Name = "reviews")]
public List<Review> Reviews { get; private set; }
/// <summary>
/// Author of the recipe.
/// </summary>
[DataMember(Name = "author")]
public User? Author { get; private set; }
/// <summary>
/// The Title of the recipe. <br/>
/// 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>
/// The steps of the preparation. See: <see cref="PreparationStep"/>.
/// </summary>
@ -58,16 +70,20 @@ namespace Model
/// <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="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string title, int? id,
List<Review> reviews,
/// <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="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, User? author,
List<Review> reviews, List<Ingredient> ingredients,
params PreparationStep[] preparationSteps)
{
Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps);
Ingredients = ingredients;
Reviews = reviews;
Author = author;
if (id == null)
{
@ -82,30 +98,43 @@ namespace Model
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param name="title">The title of the recipe.</param>
/// <param _name="title">The title of the recipe.</param>
public Recipe(string title)
: this(title, null, new List<Review>())
: this(title, null, null, new List<Review>(), new List<Ingredient>())
{
}
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param name="title">The title of the recipe.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal.</param>
/// <param _name="title">The title of the recipe.</param>
/// <param _name="preparationSteps">The steps of the preparation of the meal.</param>
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>
/// <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="preparationSteps">The steps of the preparation of the meal.</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="preparationSteps">The steps of the preparation of the meal.</param>
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
@ -114,7 +143,7 @@ namespace Model
/// <summary>
/// Add a review for the recipe.
/// </summary>
/// <param name="review">The new review to add.</param>
/// <param _name="review">The new review to add.</param>
public void AddReview(Review review)
=> Reviews.Add(review);
@ -132,6 +161,22 @@ namespace Model
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)
{
if (other == null) return false;
@ -159,12 +204,16 @@ namespace Model
sb.AppendFormat("\t* {0}\n", ps.ToString());
}
sb.AppendLine();
sb.AppendLine(ConcatIngredients());
sb.AppendLine();
foreach (Review review in Reviews)
{
sb.AppendLine(review.ToString());
}
sb.AppendLine();
sb.AppendLine($"Posted by: {Author?.ToString()}");
return sb.ToString();
}
#endregion
}
}
}

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

@ -11,7 +11,7 @@ using System.Runtime.Serialization;
namespace Model
{
/// <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
/// </summary>
[DataContract(Name = "user")]
@ -137,9 +137,9 @@ namespace Model
/// <summary>
/// Construtors of user.
/// </summary>
/// <param name="name">The name 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="name">The _name of the user</param>
/// <param _name="surname">The surname of the user</param>
/// <param _name="mail">The user needs an email to login. </param>
public User(string name, string surname, string mail, int password)
{
Name = name;

@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
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>

@ -23,4 +23,5 @@
<ProjectReference Include="..\..\Model\Model.csproj" />
<ProjectReference Include="..\..\DataPersistence\DataPersistence.csproj" />
</ItemGroup>
</Project>
</Project>

Loading…
Cancel
Save