public EntityInteractionStatus AddEntity(object a_EntityToAdd) { EntityInteractionStatus v_ActualStatus = EntityInteractionStatus.Invalid; if (a_EntityToAdd is Player) { Players.Add((Player)a_EntityToAdd); v_ActualStatus = EntityInteractionStatus.Added; } else { throw new NotImplementedException(String.Format("Adding entities of type [{0}] is not supported.", a_EntityToAdd.GetType())); } return(v_ActualStatus); }
/// <summary> /// Any database object should be only removed through this method. It encapsulates sanity testing and only deletes /// an entity if it is not referenced in any other entity. /// </summary> /// <param name="a_EntityToRemove">The entity will be tried to converted into a known object.</param> /// <returns>Invalid if something went wrong, Removed if the entity was removed and NotRemoved if the entity is still referenced.</returns> public EntityInteractionStatus RemoveEntity(object a_EntityToRemove) { EntityInteractionStatus v_ActualStatus = EntityInteractionStatus.Invalid; if (a_EntityToRemove is null) { return(v_ActualStatus); } else if (a_EntityToRemove is Player) { Player v_PlayerToRemove = a_EntityToRemove as Player; var v_ReferencedPlayer = Results.SelectMany(p => p.Scores).Where(p => p.IdPlayer == v_PlayerToRemove.Id); if (v_ReferencedPlayer.ToList().Count == 0) { PlayersById.Remove(v_PlayerToRemove.Id); Players.Remove(v_PlayerToRemove); m_Logger.Info(String.Format("Removed Player [{0}].", v_PlayerToRemove)); v_ActualStatus = EntityInteractionStatus.Removed; } else { if (v_PlayerToRemove.Gender == Player.Genders.Male) { m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "he", v_ReferencedPlayer.ToList().Count)); } else if (v_PlayerToRemove.Gender == Player.Genders.Female) { m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "she", v_ReferencedPlayer.ToList().Count)); } else { // Just for the case someone adds another gender... m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "insert pronoun here", v_ReferencedPlayer.ToList().Count)); } v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is GameFamily) { GameFamily v_GameFamilyToRemove = a_EntityToRemove as GameFamily; var v_GamesWithFamilyToRemove = Games.Where(p => p.IdGamefamilies.Contains(v_GameFamilyToRemove.Id)); if (v_GamesWithFamilyToRemove.ToList().Count == 0) { GameFamilies.Remove(v_GameFamilyToRemove); m_Logger.Info(String.Format("Removed GameFamily [{0}].", v_GameFamilyToRemove)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove [{0}] because it is is referenced in {1} games.", v_GameFamilyToRemove.Name, v_GamesWithFamilyToRemove.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is Game) { Game v_GameToRemove = a_EntityToRemove as Game; var v_ReferencesToGame = Results.Where(p => p.IdGame == v_GameToRemove.Id); if (v_ReferencesToGame.ToList().Count == 0) { Games.Remove(v_GameToRemove); m_Logger.Info(String.Format("Removed Game [{0}].", v_GameToRemove.Name)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove [{0}] because it is is referenced in {1} results.", v_GameToRemove.Name, v_ReferencesToGame.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is Location) { Location v_LocationToRemove = a_EntityToRemove as Location; var v_ReferencesToLocation = Results.Where(p => p.IdLocation == v_LocationToRemove.Id); if (v_ReferencesToLocation.ToList().Count == 0) { Locations.Remove(v_LocationToRemove); m_Logger.Info(String.Format("Removed Location [{0}].", v_LocationToRemove.Name)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove {0} because it is is referenced in {1} results.", v_LocationToRemove.Name, v_ReferencesToLocation.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else { throw new NotImplementedException(String.Format("Removing entities of type [{0}] is not supported.", a_EntityToRemove.GetType())); } return(v_ActualStatus); }