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/PlayerExtensionMethods.cs

100 lines
4.3 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 players :
/// convert model to DTO, model to Entity, ...
/// and equality protocols
/// </summary>
public static class PlayerExtensionMethods
{
// conversion methods
public static Player ToModel(this PlayerEntity p)
=> new Player(p.Nickname, p.HashedPassword, p.Id);
public static Player ToModel(this PlayerDto p)
=> new Player(p.Nickname, p.HashedPassword, p.Id);
public static PlayerDto ToDto(this Player p)
=> new PlayerDto { Id = p.Id, Nickname = p.Nickname, HashedPassword = p.HashedPassword};
public static PlayerEntity ToEntity(this Player p)
=> new PlayerEntity { Id = p.Id, Nickname = p.Nickname, HashedPassword = p.HashedPassword };
// reuse other methods
public static PlayerDto ToDto(this PlayerEntity p)
=> p.ToModel().ToDto();
public static PlayerEntity ToEntity(this PlayerDto p)
=> p.ToModel().ToEntity();
// equality protocols
public static bool Equals(this Player p1, Player p2)
=> p1.Nickname == p2.Nickname;
// reuse other methods
public static bool Equals(this Player p1, PlayerDto p2)
=> p1.Equals(p2.ToModel());
public static bool Equals(this Player p1, PlayerEntity p2)
=> p1.Equals(p2.ToModel());
public static bool Equals(this PlayerDto p1, Player p2)
=> p1.ToModel().Equals(p2);
public static bool Equals(this PlayerDto p1, PlayerDto p2)
=> p1.ToModel().Equals(p2.ToModel());
public static bool Equals(this PlayerDto p1, PlayerEntity p2)
=> p1.ToModel().Equals(p2.ToModel());
public static bool Equals(this PlayerEntity p1, Player p2)
=> p1.ToModel().Equals(p2);
public static bool Equals(this PlayerEntity p1, PlayerDto p2)
=> p1.ToModel().Equals(p2.ToModel());
public static bool Equals(this PlayerEntity p1, PlayerEntity p2)
=> p1.ToModel().Equals(p2.ToModel());
/// <summary>
/// equality protocol for a player
/// </summary>
/// <param name="p">the player</param>
/// <param name="o">an object</param>
/// <returns>true if the player and the object are sames, false otherwise</returns>
public static bool Equals(this Player p, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Player)) return Equals(p, (Player)o);
else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o));
else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o);
return false;
}
/// <summary>
/// equality protocol for a player entity
/// </summary>
/// <param name="p">the player entity</param>
/// <param name="o">an object</param>
/// <returns>true if the player entity and the object are sames, false otherwise</returns>
public static bool Equals(this PlayerEntity p, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Player)) return Equals(p, (Player)o);
else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o));
else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o);
return false;
}
/// <summary>
/// equality protocol for a player dto
/// </summary>
/// <param name="p">the player dto</param>
/// <param name="o">an object</param>
/// <returns>true if the player dto and the object are sames, false otherwise</returns>
public static bool Equals(this PlayerDto p, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Player)) return Equals(p, (Player)o);
else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o));
else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o);
return false;
}
}
}