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 answers : /// convert model to DTO, model to Entity, ... /// and equality protocols /// public static class AnswerExtensionMethods { // conversion methods public static Answer ToModel(this AnswerEntity a) => new Answer(a.Content, a.Question?.ToModel(), a.Id); public static Answer ToModel(this AnswerDto a) => new Answer(a.Content, a.Question?.ToModel(), a.Id); public static AnswerDto ToDto(this Answer a) => new AnswerDto { Id = a.Id, Content = a.Content, Question = a.Question?.ToDto(), IdQuestion = a.IdQuestion }; public static AnswerEntity ToEntity(this Answer a) => new AnswerEntity { Id = a.Id, Content = a.Content, Question = a.Question?.ToEntity(), IdQuestion = a.IdQuestion }; // reuse other methods public static AnswerDto ToDto(this AnswerEntity a) => a.ToModel().ToDto(); public static AnswerEntity ToEntity(this AnswerDto a) => a.ToModel().ToEntity(); // equality protocols public static bool Equals(this Answer a1, Answer a2) => a1.Content == a2.Content && a1.IdQuestion == a2.IdQuestion; // reuse other methods public static bool Equals(this Answer a1, AnswerDto a2) => a1.Equals(a2.ToModel()); public static bool Equals(this Answer a1, AnswerEntity a2) => a1.Equals(a2.ToModel()); public static bool Equals(this AnswerDto a1, Answer a2) => a1.ToModel().Equals(a2); public static bool Equals(this AnswerDto a1, AnswerDto a2) => a1.ToModel().Equals(a2.ToModel()); public static bool Equals(this AnswerDto a1, AnswerEntity a2) => a1.ToModel().Equals(a2.ToModel()); public static bool Equals(this AnswerEntity a1, Answer a2) => a1.ToModel().Equals(a2); public static bool Equals(this AnswerEntity a1, AnswerDto a2) => a1.ToModel().Equals(a2.ToModel()); public static bool Equals(this AnswerEntity a1, AnswerEntity a2) => a1.ToModel().Equals(a2.ToModel()); /// /// equality protocol for an answer /// /// the answer /// an object /// true if the answer and the object are sames, false otherwise public static bool Equals(this Answer a, object? o) { if (o == null) return false; if (o.GetType() == typeof(Answer)) return Equals(a, (Answer)o); else if (o.GetType() == typeof(AnswerEntity)) return Equals(a, ((AnswerEntity)o)); else if (o.GetType() == typeof(AnswerDto)) return Equals(a, (AnswerDto)o); return false; } /// /// equality protocol for an answer entity /// /// the answer entity /// an object /// true if the answer entity and the object are sames, false otherwise public static bool Equals(this AnswerEntity a, object? o) { if (o == null) return false; if (o.GetType() == typeof(Answer)) return Equals(a, (Answer)o); else if (o.GetType() == typeof(AnswerEntity)) return Equals(a, ((AnswerEntity)o)); else if (o.GetType() == typeof(AnswerDto)) return Equals(a, (AnswerDto)o); return false; } /// /// equality protocol for an answer dto /// /// the answer dto /// an object /// true if the answer dto and the object are sames, false otherwise public static bool Equals(this AnswerDto a, object? o) { if (o == null) return false; if (o.GetType() == typeof(Answer)) return Equals(a, (Answer)o); else if (o.GetType() == typeof(AnswerEntity)) return Equals(a, ((AnswerEntity)o)); else if (o.GetType() == typeof(AnswerDto)) return Equals(a, (AnswerDto)o); return false; } } }