예제 #1
0
        //CRUD
        public void Add(Team entity)
        {
            //validation for team members already being in a team happens in the view.
            TeamDTO tDTO = new TeamDTO
            {
                TeamID         = entity.TeamID,
                TeamName       = entity.TeamName,
                TeamCaptainID  = entity.TeamCaptain?.PlayerID,    //null propagation
                TeamDivisionID = entity.TeamDivision?.DivisionID, // null propagation
                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,
            };

            //set / get the teamID to use in the other contexts
            entity.TeamID = _teamContext.Add(tDTO);

            //add players to DB
            if (entity.TeamMembers != null)
            {
                foreach (var player in entity?.TeamMembers)
                {
                    if (player.PlayerID != null)
                    {
                        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);
                        }
                    }
                }
            }
            //add captain to DB
            if (entity.TeamCaptain?.PlayerID != null)
            {
                if (_playerContext.GetByID(entity.TeamCaptain.PlayerID).PlayerID != null)
                {
                    var captain = _playerContext.GetByID(entity.TeamCaptain.PlayerID);
                    captain.PlayerTeamID = entity.TeamID;
                    _playerContext.Update(captain);
                }
            }
        }