impl in recipe and add author

pull/44/head
Alexandre AGOSTINHO 2 years ago
parent 58902cea37
commit 1049c8f6d9

@ -13,6 +13,8 @@ namespace Model
{ {
#region Attributes #region Attributes
private string _title = ""; private string _title = "";
//private List<Review> _reviews;
#endregion #endregion
#region Properties #region Properties
@ -21,6 +23,8 @@ namespace Model
/// </summary> /// </summary>
public int Id { get; init; } public int Id { get; init; }
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.
@ -47,14 +51,16 @@ 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="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title = "", int? id = null, public Recipe(string title, int? id,
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;
if (id == null) if (id == null)
{ {
@ -65,9 +71,42 @@ namespace Model
} }
else Id = (int)id; else Id = (int)id;
} }
/// <summary>
/// <inheritdoc cref="Recipe.Recipe(string, int?, List{Review}, PreparationStep[])"/>
/// </summary>
/// <param name="title">The title of the recipe.</param>
public Recipe(string title)
: this(title, null, new List<Review>())
{
}
/// <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>
public Recipe(string title, params PreparationStep[] preparationSteps)
: this(title, null, new List<Review>(), preparationSteps)
{
}
#endregion #endregion
#region Methods #region Methods
public void AddReview(Review review)
=> Reviews.Add(review);
public string GetReviews()
{
StringBuilder sb = new StringBuilder("Reviews:\n------------------------------\n");
foreach (Review review in Reviews)
{
sb.AppendLine(review.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;

@ -7,13 +7,15 @@ using System.Threading.Tasks;
namespace Model namespace Model
{ {
internal class Review public class Review : IEquatable<Review>
{ {
private int _stars; private int _stars;
private string _content = ""; private string _content = "";
public int Id { get; init; } public int Id { get; init; }
public User Author { get; private set; }
public int Stars public int Stars
{ {
get => _stars; get => _stars;
@ -34,7 +36,7 @@ namespace Model
} }
} }
public Review(int? id, int stars, string content) public Review(User author, int? id, int stars, string content)
{ {
if (id == null) if (id == null)
{ {
@ -45,12 +47,38 @@ namespace Model
} }
else Id = (int)id; else Id = (int)id;
Author = author;
Stars = stars; Stars = stars;
Content = content; Content = content;
} }
public Review(int stars, string content) : this(null, stars, content) public Review(User author, int stars, string content) : this(author, null, stars, content)
{
}
public override string ToString()
{
return $"{Author.Name} {Author.Surname}: [ {Stars} stars ]\n{Content}";
}
public bool Equals(Review? other)
{
if (other is null) return false;
return Id.Equals(other.Id);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(obj, null)) return false;
if (ReferenceEquals(obj, this)) return true;
if (GetType() != obj.GetType()) return false;
return Equals(obj);
}
public override int GetHashCode()
{ {
return Id.GetHashCode();
} }
} }
} }

Loading…
Cancel
Save