public bool Update(Team obj) { return(context.Update(obj)); }
public void Update(Team entity) { var oldValues = _teamContext.GetByID(entity.TeamID); List <int?> currentPlayers = new List <int?> { oldValues.TeamMember2ID, oldValues.TeamMember3ID, oldValues.TeamMember4ID, oldValues.TeamMember5ID }; TeamDTO tDTO = new TeamDTO { TeamID = entity.TeamID, //normal values are nullable TeamName = entity.TeamName, TeamCaptainID = entity.TeamCaptain?.PlayerID, //null propagation because this comes from an object TeamDivisionID = oldValues.TeamDivisionID, // null propagation teamdivision doesn't get edited in manage teams TeamLogo = entity.TeamLogo, TeamMember2ID = entity.TeamMembers?.Count > 0 ? entity.TeamMembers[0].PlayerID : null, TeamMember3ID = entity.TeamMembers?.Count > 1 ? entity.TeamMembers[1].PlayerID : null, TeamMember4ID = entity.TeamMembers?.Count > 2 ? entity.TeamMembers[2].PlayerID : null, TeamMember5ID = entity.TeamMembers?.Count > 3 ? entity.TeamMembers[3].PlayerID : null, }; _teamContext.Update(tDTO); //add players to DB if (entity?.TeamMembers != null) { foreach (var player in entity?.TeamMembers) { if (player.PlayerID != null) { if (currentPlayers.Contains(player.PlayerID)) { PlayerDTO p = new PlayerDTO { PlayerID = player.PlayerID, PlayerName = player.PlayerName, PlayerPlatformID = player.PlayerPlatformID, PlayerRoleID = player.PlayerRole?.RoleID, PlayerTeamID = entity.TeamID }; //if the player does or does not exist in the current database if (_playerContext.GetByID(p.PlayerID).PlayerID == null) { _playerContext.Add(p); } else { _playerContext.Update(p); } } else { var pID = currentPlayers.Where(p => p.Value == player.PlayerID).FirstOrDefault(); var pToUpdate = _playerContext.GetByID(pID); pToUpdate.PlayerTeamID = null; _playerContext.Update(pToUpdate); } } } } //add captain to DB //captain always exists in the database because it is required when an account is made if (entity.TeamCaptain?.PlayerID != null) { if (oldValues.TeamCaptainID == entity.TeamCaptain.PlayerID) { var captainValues = _playerContext.GetByID(oldValues.TeamCaptainID); PlayerDTO captain = new PlayerDTO { PlayerID = entity.TeamCaptain.PlayerID, PlayerName = captainValues.PlayerName, PlayerPlatformID = captainValues.PlayerPlatformID, PlayerRoleID = entity.TeamCaptain.PlayerRole?.RoleID, PlayerTeamID = entity.TeamID }; _playerContext.Update(captain); } else { //remove team from the previous captain var pToUpdate = _playerContext.GetByID(oldValues.TeamCaptainID); pToUpdate.PlayerTeamID = null; _playerContext.Update(pToUpdate); } } }