From 1049c8f6d9c3b40a3d3624803b95ef2ef9c94014 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Tue, 16 May 2023 15:04:22 +0200 Subject: [PATCH] impl in recipe and add author --- MCTG/Model/Recipes/Recipe.cs | 55 ++++++++++++++++++++++++++++++------ MCTG/Model/Recipes/Review.cs | 34 ++++++++++++++++++++-- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/MCTG/Model/Recipes/Recipe.cs b/MCTG/Model/Recipes/Recipe.cs index 6048fdf..f63760e 100644 --- a/MCTG/Model/Recipes/Recipe.cs +++ b/MCTG/Model/Recipes/Recipe.cs @@ -13,6 +13,8 @@ namespace Model { #region Attributes private string _title = ""; + + //private List _reviews; #endregion #region Properties @@ -21,6 +23,8 @@ namespace Model /// public int Id { get; init; } + public List Reviews { get; private set; } + /// /// The Title of the recipe.
/// Set to "No title." when the value passed is null, empty or contain white spaces. @@ -47,15 +51,17 @@ namespace Model /// /// Construct a new recipe. /// - /// The title of the recipe + /// The title of the recipe. /// The id of the recipe. If not given, get a new id. - /// The steps of the preparation of the meal - public Recipe(string title = "", int? id = null, - params PreparationStep[] preparationSteps) - { + /// The steps of the preparation of the meal. + public Recipe(string title, int? id, + List reviews, + params PreparationStep[] preparationSteps) + { Title = title; - PreparationSteps = new List(preparationSteps); - + PreparationSteps = new List(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; + } + + /// + /// + /// + /// The title of the recipe. + public Recipe(string title) + : this(title, null, new List()) + { + } + + /// + /// + /// + /// The title of the recipe. + /// The steps of the preparation of the meal. + public Recipe(string title, params PreparationStep[] preparationSteps) + : this(title, null, new List(), 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; diff --git a/MCTG/Model/Recipes/Review.cs b/MCTG/Model/Recipes/Review.cs index 2c005e5..bd0a988 100644 --- a/MCTG/Model/Recipes/Review.cs +++ b/MCTG/Model/Recipes/Review.cs @@ -7,13 +7,15 @@ using System.Threading.Tasks; namespace Model { - internal class Review + public class Review : IEquatable { 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(); } } }