You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.9 KiB
111 lines
4.9 KiB
using DTOs;
|
|
using Entities;
|
|
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ExtensionsClassLibrairie
|
|
{
|
|
/// <summary>
|
|
/// define some methods to manipulate entity, model and dto questions :
|
|
/// convert model to DTO, model to Entity, ...
|
|
/// and equality protocols
|
|
/// </summary>
|
|
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());
|
|
/// <summary>
|
|
/// equality protocol for a question
|
|
/// </summary>
|
|
/// <param name="q">the question</param>
|
|
/// <param name="o">an object</param>
|
|
/// <returns>true if the question and the object are sames, false otherwise</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// equality protocol for a question entity
|
|
/// </summary>
|
|
/// <param name="q">the question entity</param>
|
|
/// <param name="o">an object</param>
|
|
/// <returns>true if the question entity and the object are sames, false otherwise</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// equality protocol for a question dto
|
|
/// </summary>
|
|
/// <param name="q">the question dto</param>
|
|
/// <param name="o">an object</param>
|
|
/// <returns>true if the question dto and the object are sames, false otherwise</returns>
|
|
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;
|
|
}
|
|
}
|
|
} |