using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Security.Cryptography; using System.Text; namespace Model { /// /// Define a Recipe for the preparation of a meal. /// [DataContract(Name = "recipe")] public class Recipe : IEquatable { #region Attributes [DataMember(Name = "title")] private string _title = ""; [DataMember(Name = "image")] private string _image = ""; #endregion #region Properties /// /// The ID of the recipe - allows you to compare and/or get this item in an easier way. /// [DataMember(Name = "id")] public int Id { get; init; } /// /// List of reviews of this recipe. /// [DataMember(Name = "reviews")] public List Reviews { get; private set; } /// /// AuthorMail's mail of the recipe. /// [DataMember(Name = "authorMail")] public string? AuthorMail { get; private set; } /// /// The Title of the recipe.
/// Set to "No title." when the value passed is null, empty or contain white spaces. ///
public string Title { get => _title; set { if (string.IsNullOrWhiteSpace(value)) _title = "No title."; else _title = value; } } /// /// The image of the recipe.
/// Set to "room_service_icon.png" when the value passed is null, empty or contain white space. ///
public string? Image { get => _image; set { if (!string.IsNullOrWhiteSpace(value)) _image = "room_service_icon.png"; _image = value; } } /// The list of ingredients. /// [DataMember(Name = "ingredient")] public List Ingredients { get; set; } /// /// The steps of the preparation. See: . /// [DataMember(Name = "preparation-steps")] public List PreparationSteps { get; set; } #endregion #region Constructors /// /// Construct a new recipe. /// /// The title of the recipe /// The id of the recipe. If not given, get a new id. /// The name of the user that create this recipe. /// The image that represent the recipe /// Thr list of reviews. /// Thr list of ingredients. /// The steps of the preparation of the meal public Recipe(string title, int? id, string? authorMail, string? picture, List reviews, List ingredients, params PreparationStep[] preparationSteps) { Title = title; Image = picture; PreparationSteps = new List(preparationSteps); Ingredients = ingredients; Reviews = reviews; AuthorMail = authorMail; if (id == null) { var randomGenerator = RandomNumberGenerator.Create(); byte[] data = new byte[16]; randomGenerator.GetBytes(data); Id = Math.Abs(BitConverter.ToInt16(data)); } else Id = (int)id; } /// /// /// /// The title of the recipe. /// The id of the recipe. If not given, get a new id. /// Mail of the user that create the recipe /// The steps of the preparation of the meal. public Recipe(string title, int? id, string? authorMail, params PreparationStep[] preparationSteps) : this(title, id, authorMail, null, new List(), new List(), preparationSteps) { } /// /// /// /// The title of the recipe. /// The id of the recipe. If not given, get a new id. /// Mail of the user that create the recipe /// Mail of the user that create the recipe /// List of ingredients that compose the recipe. /// The steps of the preparation of the meal. public Recipe(string title, int? id, string? authorMail, string? picture, List ingredients, params PreparationStep[] preparationSteps) : this(title, id, authorMail, picture, new List(), ingredients, preparationSteps) { } ///// ///// ///// ///// The title of the recipe. ///// The id of the recipe. If not given, get a new id. ///// Image that reppresent the recipe. ///// The steps of the preparation of the meal. //public Recipe(string title, int? id, string picture, params PreparationStep[] preparationSteps) // : this(title, id, null, picture, new List(), new List(), preparationSteps) //{ //} #endregion #region Methods /// /// Add a review for the recipe. /// /// The new review to add. public void AddReview(Review review) => Reviews.Add(review); /// /// Get a string representing all the reviews and their informations. /// /// public string GetReviews() { StringBuilder sb = new StringBuilder("Reviews:\n------------------------------\n"); foreach (Review review in Reviews) { sb.AppendLine(review.ToString()); } return sb.ToString(); } /// /// Concatenate the list of ingredients in a single string /// /// The list of ingredients in string format 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; if (other == this) return true; return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); } public override bool Equals(object? obj) { var item = obj as Recipe; if (item == null) return false; return Equals(obj); } public override int GetHashCode() { return Id.GetHashCode(); } public override string ToString() { StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); foreach (PreparationStep ps in PreparationSteps) { 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: {AuthorMail?.ToString()}"); return sb.ToString(); } #endregion } }