using DTOs; using Entities; using Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExtensionsClassLibrairie { /// /// define some methods to manipulate entity, model and dto questions : /// convert model to DTO, model to Entity, ... /// and equality protocols /// public static class QuestionExtensionMethods { // conversion methods public static Question ToModel(this QuestionEntity q) { var tmp = new Question(q.Content, q.Chapter?.ToModel(), q.Id, q.AnswerGood?.ToModel()); tmp.Difficulty = q.Difficulty; tmp.NbFalls = q.NbFalls; return tmp; } public static Question ToModel(this QuestionDto q) { var tmp = new Question(q.Content, q.Chapter?.ToModel(), q.Id, q.AnswerGood?.ToModel()); tmp.Difficulty = q.Difficulty; tmp.NbFalls = q.NbFalls; return tmp; } public static QuestionDto ToDto(this Question q) => new QuestionDto { Id = q.Id, Content = q.Content, AnswerGood = q.AnswerGood?.ToDto(), Chapter = q.Chapter?.ToDto(), Difficulty = q.Difficulty, NbFalls = q.NbFalls }; public static QuestionEntity ToEntity(this Question q) => new QuestionEntity { Id = q.Id, Content = q.Content, AnswerGood = q.AnswerGood?.ToEntity(), Chapter = q.Chapter?.ToEntity(), Difficulty = q.Difficulty, NbFalls = q.NbFalls }; // reuse other methods public static QuestionDto ToDto(this QuestionEntity q) => q.ToModel().ToDto(); public static QuestionEntity ToEntity(this QuestionDto q) => q.ToModel().ToEntity(); // equality protocols public static bool Equals(this Question q1, Question q2) => q1.Content == q2.Content; // reuse other methods public static bool Equals(this Question q1, QuestionDto q2) => q1.Equals(q2.ToModel()); public static bool Equals(this Question q1, QuestionEntity q2) => q1.Equals(q2.ToModel()); public static bool Equals(this QuestionDto q1, Question q2) => q1.ToModel().Equals(q2); public static bool Equals(this QuestionDto q1, QuestionDto q2) => q1.ToModel().Equals(q2.ToModel()); public static bool Equals(this QuestionDto q1, QuestionEntity q2) => q1.ToModel().Equals(q2.ToModel()); public static bool Equals(this QuestionEntity q1, Question q2) => q1.ToModel().Equals(q2); public static bool Equals(this QuestionEntity q1, QuestionDto q2) => q1.ToModel().Equals(q2.ToModel()); public static bool Equals(this QuestionEntity q1, QuestionEntity q2) => q1.ToModel().Equals(q2.ToModel()); /// /// equality protocol for a question /// /// the question /// an object /// true if the question and the object are sames, false otherwise public static bool Equals(this Question q, object? o) { if (o == null) return false; if (o.GetType() == typeof(Question)) return Equals(q, (Question)o); else if (o.GetType() == typeof(QuestionEntity)) return Equals(q, ((QuestionEntity)o)); else if (o.GetType() == typeof(QuestionDto)) return Equals(q, (QuestionDto)o); return false; } /// /// equality protocol for a question entity /// /// the question entity /// an object /// true if the question entity and the object are sames, false otherwise public static bool Equals(this QuestionEntity q, object? o) { if (o == null) return false; if (o.GetType() == typeof(Question)) return Equals(q, (Question)o); else if (o.GetType() == typeof(QuestionEntity)) return Equals(q, ((QuestionEntity)o)); else if (o.GetType() == typeof(QuestionDto)) return Equals(q, (QuestionDto)o); return false; } /// /// equality protocol for a question dto /// /// the question dto /// an object /// true if the question dto and the object are sames, false otherwise public static bool Equals(this QuestionDto q, object? o) { if (o == null) return false; if (o.GetType() == typeof(Question)) return Equals(q, (Question)o); else if (o.GetType() == typeof(QuestionEntity)) return Equals(q, ((QuestionEntity)o)); else if (o.GetType() == typeof(QuestionDto)) return Equals(q, (QuestionDto)o); return false; } } }