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
private string _title = "";
//private List<Review> _reviews;
#endregion
#region Properties
@ -21,6 +23,8 @@ namespace Model
/// </summary>
public int Id { get; init; }
public List<Review> Reviews { 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,15 +51,17 @@ namespace Model
/// <summary>
/// Construct a new recipe.
/// </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="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string title = "", int? id = null,
params PreparationStep[] preparationSteps)
{
/// <param name="preparationSteps">The steps of the preparation of the meal.</param>
public Recipe(string title, int? id,
List<Review> reviews,
params PreparationStep[] preparationSteps)
{
Title = title;
PreparationSteps = new List<PreparationStep>(preparationSteps);
PreparationSteps = new List<PreparationStep>(preparationSteps);
Reviews = reviews;
if (id == null)
{
var randomGenerator = RandomNumberGenerator.Create();
@ -63,11 +69,44 @@ namespace Model
randomGenerator.GetBytes(data);
Id = Math.Abs(BitConverter.ToInt16(data));
}
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
#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)
{
if (other == null) return false;

@ -7,13 +7,15 @@ using System.Threading.Tasks;
namespace Model
{
internal class Review
public class Review : IEquatable<Review>
{
private int _stars;
private string _content = "";
public int Id { get; init; }
public User Author { get; private set; }
public int 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)
{
@ -45,12 +47,38 @@ namespace Model
}
else Id = (int)id;
Author = author;
Stars = stars;
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