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

101 lines
4.5 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 lobbies :
/// convert model to DTO, model to Entity, ...
/// and equality protocols
/// </summary>
public static class LobbyExtensionMethods
{
// conversion methods
public static Lobby ToModel(this LobbyEntity l)
=> new Lobby(l.Name, l.Creator.ToModel(), l.Password, l.NbPlayers, l.Id);
public static Lobby ToModel(this LobbyDto l)
=> new Lobby(l.Name, l.Creator.ToModel(), l.Password, l.NbPlayers, l.Id);
public static LobbyDto ToDto(this Lobby l)
=> new LobbyDto { Id = l.Id, Creator = l.Creator.ToDto(), IdCreator = l.IdCreator,
Name = l.Name, Password = l.Password, NbPlayers = l.NbPlayers};
public static LobbyEntity ToEntity(this Lobby l)
=> new LobbyEntity { Id = l.Id, Creator = l.Creator.ToEntity(), IdCreator = l.IdCreator,
Name = l.Name, Password = l.Password, NbPlayers = l.NbPlayers};
// reuse other methods
public static LobbyDto ToDto(this LobbyEntity l)
=> l.ToModel().ToDto();
public static LobbyEntity ToEntity(this LobbyDto l)
=> l.ToModel().ToEntity();
// equality protocols
public static bool Equals(this Lobby l1, Lobby l2)
=> l1.Name == l2.Name && l1.IdCreator == l2.IdCreator;
// reuse other methods
public static bool Equals(this Lobby l1, LobbyDto l2)
=> l1.Equals(l2.ToModel());
public static bool Equals(this Lobby l1, LobbyEntity l2)
=> l1.Equals(l2.ToModel());
public static bool Equals(this LobbyDto l1, Lobby l2)
=> l1.ToModel().Equals(l2);
public static bool Equals(this LobbyDto l1, LobbyDto l2)
=> l1.ToModel().Equals(l2.ToModel());
public static bool Equals(this LobbyDto l1, LobbyEntity l2)
=> l1.ToModel().Equals(l2.ToModel());
public static bool Equals(this LobbyEntity l1, Lobby l2)
=> l1.ToModel().Equals(l2);
public static bool Equals(this LobbyEntity l1, LobbyDto l2)
=> l1.ToModel().Equals(l2.ToModel());
public static bool Equals(this LobbyEntity l1, LobbyEntity l2)
=> l1.ToModel().Equals(l2.ToModel());
/// <summary>
/// equality protocol for a lobby
/// </summary>
/// <param name="l">the lobby</param>
/// <param name="o">an object</param>
/// <returns>true if the lobby and the object are sames, false otherwise</returns>
public static bool Equals(this Lobby l, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Lobby)) return Equals(l, (Lobby)o);
else if (o.GetType() == typeof(LobbyEntity)) return Equals(l, ((LobbyEntity)o));
else if (o.GetType() == typeof(LobbyDto)) return Equals(l, (LobbyDto)o);
return false;
}
/// <summary>
/// equality protocol for a lobby entity
/// </summary>
/// <param name="l">the lobby entity</param>
/// <param name="o">an object</param>
/// <returns>true if the lobby entity and the object are sames, false otherwise</returns>
public static bool Equals(this LobbyEntity l, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Lobby)) return Equals(l, (Lobby)o);
else if (o.GetType() == typeof(LobbyEntity)) return Equals(l, ((LobbyEntity)o));
else if (o.GetType() == typeof(LobbyDto)) return Equals(l, (LobbyDto)o);
return false;
}
/// <summary>
/// equality protocol for a lobby dto
/// </summary>
/// <param name="l">the lobby dto</param>
/// <param name="o">an object</param>
/// <returns>true if the lobby dto and the object are sames, false otherwise</returns>
public static bool Equals(this LobbyDto l, object? o)
{
if (o == null) return false;
if (o.GetType() == typeof(Lobby)) return Equals(l, (Lobby)o);
else if (o.GetType() == typeof(LobbyEntity)) return Equals(l, ((LobbyEntity)o));
else if (o.GetType() == typeof(LobbyDto)) return Equals(l, (LobbyDto)o);
return false;
}
}
}