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.
3.01-QCM_MuscuMaths/WebApi/ExtensionsClassLibrairie/ChapterExtensionMethods.cs

100 lines
4.2 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 chapters :
/// convert model to DTO, model to Entity, ...
/// and equality protocols
/// </summary>
public static class ChapterExtensionMethods
{
// conversion methods
public static Chapter ToModel(this ChapterEntity c)
=> new Chapter(c.Name, c.Id);
public static Chapter ToModel(this ChapterDto c)
=> new Chapter(c.Name, c.Id);
public static ChapterDto ToDto(this Chapter c)
=> new ChapterDto { Id = c.Id, Name = c.Name };
public static ChapterEntity ToEntity(this Chapter c)
=> new ChapterEntity { Id = c.Id, Name = c.Name };
// reuse other methods
public static ChapterDto ToDto(this ChapterEntity c)
=> c.ToModel().ToDto();
public static ChapterEntity ToEntity(this ChapterDto c)
=> c.ToModel().ToEntity();
// equality protocols
public static bool Equals(this Chapter c1, Chapter c2)
=> c1.Name == c2.Name;
// reuse other methods
public static bool Equals(this Chapter c1, ChapterDto c2)
=> c1.Equals(c2.ToModel());
public static bool Equals(this Chapter c1, ChapterEntity c2)
=> c1.Equals(c2.ToModel());
public static bool Equals(this ChapterDto c1, Chapter c2)
=> c1.ToModel().Equals(c2);
public static bool Equals(this ChapterDto c1, ChapterDto c2)
=> c1.ToModel().Equals(c2.ToModel());
public static bool Equals(this ChapterDto c1, ChapterEntity c2)
=> c1.ToModel().Equals(c2.ToModel());
public static bool Equals(this ChapterEntity c1, Chapter c2)
=> c1.ToModel().Equals(c2);
public static bool Equals(this ChapterEntity c1, ChapterDto c2)
=> c1.ToModel().Equals(c2.ToModel());
public static bool Equals(this ChapterEntity c1, ChapterEntity c2)
=> c1.ToModel().Equals(c2.ToModel());
/// <summary>
/// equality protocol for a chapter
/// </summary>
/// <param name="c">the chapter</param>
/// <param name="o">an object</param>
/// <returns>true if the chapter and the object are sames, false otherwise</returns>
public static bool Equals(this Chapter c, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o);
else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o));
else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o);
return false;
}
/// <summary>
/// equality protocol for a chapter entity
/// </summary>
/// <param name="c">the chapter entity</param>
/// <param name="o">an object</param>
/// <returns>true if the chapter entity and the object are sames, false otherwise</returns>
public static bool Equals(this ChapterEntity c, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o);
else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o));
else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o);
return false;
}
/// <summary>
/// equality protocol for a chapter dto
/// </summary>
/// <param name="c">the chapter dto</param>
/// <param name="o">an object</param>
/// <returns>true if the chapter dto and the object are sames, false otherwise</returns>
public static bool Equals(this ChapterDto c, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o);
else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o));
else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o);
return false;
}
}
}