done reviews
continuous-integration/drone/push Build is passing Details

pull/44/head
Alexandre AGOSTINHO 2 years ago
parent 2708d09107
commit 02c5d5b583

@ -16,8 +16,17 @@ Stub stub = new Stub();
List<User> users = stub.ConstrucList(); List<User> users = stub.ConstrucList();
RecipeCollection recipes = new RecipeCollection("all", stub.LoadRecipes().ToArray()); RecipeCollection recipes = new RecipeCollection("all", stub.LoadRecipes().ToArray());
recipes[0].AddReview(new Review(users[1], 1, "bonne recette !1"));
recipes[0].AddReview(new Review(users[1], 1, "bonne recette !2"));
recipes[0].AddReview(new Review(users[1], 4, "bonne recette !3"));
recipes[0].AddReview(new Review(users[1], 5, "bonne recette !4"));
recipes[0].AddReview(new Review(users[1], 3, "bonne recette !5"));
recipes[0].AddReview(new Review(users[1], 2, "bonne recette !6"));
Console.WriteLine(recipes); Console.WriteLine(recipes);
Console.WriteLine("\n---------------------------\n"); Console.WriteLine("\n---------------------------\n");
Console.WriteLine(recipes[0].GetReviews());
// press any key to quit // press any key to quit
Console.ReadKey(); Console.ReadKey();

@ -1,102 +1,102 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Model namespace Model
{ {
/// <summary> /// <summary>
/// Define a Recipe for the preparation of a meal. /// Define a Recipe for the preparation of a meal.
/// </summary> /// </summary>
public class Recipe : IEquatable<Recipe> public class Recipe : IEquatable<Recipe>
{ {
#region Attributes #region Attributes
private string _title = ""; private string _title = "";
//private List<Review> _reviews; //private List<Review> _reviews;
#endregion #endregion
#region Properties #region Properties
/// <summary> /// <summary>
/// The ID of the recipe - allows you to compare and/or get this item in an easier way. /// The ID of the recipe - allows you to compare and/or get this item in an easier way.
/// </summary> /// </summary>
public int Id { get; init; } public int Id { get; init; }
public List<Review> Reviews { get; private set; } public List<Review> Reviews { 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.
/// </summary> /// </summary>
public string Title public string Title
{ {
get => _title; get => _title;
set set
{ {
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
_title = "No title."; _title = "No title.";
else else
_title = value; _title = value;
} }
} }
/// <summary> /// <summary>
/// The steps of the preparation. See: <see cref="PreparationStep"/>. /// The steps of the preparation. See: <see cref="PreparationStep"/>.
/// </summary> /// </summary>
public List<PreparationStep> PreparationSteps { get; set; } public List<PreparationStep> PreparationSteps { get; set; }
#endregion #endregion
#region Constructors #region Constructors
/// <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="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id, public Recipe(string title, int? id,
List<Review> reviews, List<Review> reviews,
params PreparationStep[] preparationSteps) params PreparationStep[] preparationSteps)
{ {
Title = title; Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps); PreparationSteps = new List<PreparationStep>(preparationSteps);
Reviews = reviews; Reviews = reviews;
if (id == null) if (id == null)
{ {
var randomGenerator = RandomNumberGenerator.Create(); var randomGenerator = RandomNumberGenerator.Create();
byte[] data = new byte[16]; byte[] data = new byte[16];
randomGenerator.GetBytes(data); randomGenerator.GetBytes(data);
Id = Math.Abs(BitConverter.ToInt16(data)); Id = Math.Abs(BitConverter.ToInt16(data));
} }
else Id = (int)id; else Id = (int)id;
} }
/// <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, new List<Review>())
{ {
} }
/// <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, new List<Review>(), preparationSteps)
{ {
} }
#endregion #endregion
#region Methods #region Methods
public void AddReview(Review review) public void AddReview(Review review)
=> Reviews.Add(review); => Reviews.Add(review);
public string GetReviews() public string GetReviews()
{ {
StringBuilder sb = new StringBuilder("Reviews:\n------------------------------\n"); StringBuilder sb = new StringBuilder("Reviews:\n------------------------------\n");
@ -105,36 +105,36 @@ namespace Model
sb.AppendLine(review.ToString()); sb.AppendLine(review.ToString());
} }
return sb.ToString(); 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;
if (other == this) return true; if (other == this) return true;
return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps);
} }
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
var item = obj as Recipe; var item = obj as Recipe;
if (item == null) return false; if (item == null) return false;
return Equals(obj); return Equals(obj);
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return Id.GetHashCode(); return Id.GetHashCode();
} }
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n");
foreach (PreparationStep ps in PreparationSteps) foreach (PreparationStep ps in PreparationSteps)
{ {
sb.AppendFormat("\t* {0}\n", ps.ToString()); sb.AppendFormat("\t* {0}\n", ps.ToString());
} }
return sb.ToString(); return sb.ToString();
} }
#endregion #endregion
} }
} }

Loading…
Cancel
Save