public void Delete(int playerId, int matchId) { PlayerOnMatch playerOnMatch = context.PlayerOnMatches.Find(playerId, matchId); context.PlayerOnMatches.Remove(playerOnMatch); }
public void Insert(PlayerOnMatch playerOnMatch) { context.PlayerOnMatches.Add(playerOnMatch); }
public void Update(PlayerOnMatch playerOnMatch) { context.Entry(playerOnMatch).State = EntityState.Modified; }
public ActionResult Create(CreateMatchViewModel match) { string[] hostPlayersIds = { }; string[] guestPlayersIds = { }; System.Diagnostics.Debug.WriteLine("Host players: " + match.SelectedHostTeamPlayers); System.Diagnostics.Debug.WriteLine("Guest players: " + match.SelectedGuestTeamPlayers); if (string.IsNullOrEmpty(match.SelectedHostTeamPlayers)) { ModelState.AddModelError("PlayersHostTeam", "You must select minimum 6 players"); } else { hostPlayersIds = match.SelectedHostTeamPlayers.Split(' '); if (hostPlayersIds.Length < 6) { ModelState.AddModelError("PlayersHostTeam", "You must select minimum 6 players"); } } if (string.IsNullOrEmpty(match.SelectedGuestTeamPlayers)) { ModelState.AddModelError("PlayersGuestTeam", "You must select minimum 6 players"); } else { guestPlayersIds = match.SelectedGuestTeamPlayers.Split(' '); if (guestPlayersIds.Length < 6) { ModelState.AddModelError("PlayersGuestTeam", "You must select minimum 6 players"); } } if (ModelState.IsValid) { var matchForInsert = new Match() { MatchPlace = match.MatchPlace, MatchTime = match.MatchTime, Status = "Not_started", HostTeamId = match.HostTeamId, GuestTeamId = match.GuestTeamId }; matchRepository.Insert(matchForInsert); matchRepository.Save(); for (int i = 0; i < hostPlayersIds.Length - 1; i++) { var playerOnMatch = new PlayerOnMatch() { MatchId = matchForInsert.Id, PlayerId = Int32.Parse(hostPlayersIds[i]) }; playerOnMatchRepository.Insert(playerOnMatch); playerOnMatchRepository.Save(); } for (int i = 0; i < guestPlayersIds.Length - 1; i++) { var playerOnMatch = new PlayerOnMatch() { MatchId = matchForInsert.Id, PlayerId = Int32.Parse(guestPlayersIds[i]) }; playerOnMatchRepository.Insert(playerOnMatch); playerOnMatchRepository.Save(); } return(RedirectToAction("Index")); } match.HostTeams = teamRepository.GetAllWithMinSixPlayers(); match.GuestTeams = teamRepository.GetAllWithMinSixPlayers(); match.PlayersHostTeam = new List <TeamPlayersForMatch>(); match.HostTeams.Find(t => t.Id == match.HostTeamId).Players.ToList().ForEach(p => { bool selected = false; if (hostPlayersIds.Contains(p.Id.ToString())) { selected = true; } match.PlayersHostTeam.Add(new TeamPlayersForMatch() { Id = p.Id, Name = p.Name, TeamId = p.TeamId, SelectedForMatch = selected, Team = p.Team }); }); match.PlayersGuestTeam = new List <TeamPlayersForMatch>(); match.GuestTeams.Find(t => t.Id == match.GuestTeamId).Players.ToList().ForEach(p => { bool selected = false; if (guestPlayersIds.Contains(p.Id.ToString())) { selected = true; } match.PlayersGuestTeam.Add(new TeamPlayersForMatch() { Id = p.Id, Name = p.Name, TeamId = p.TeamId, SelectedForMatch = selected, Team = p.Team });; }); match.DisabledDates = matchRepository.GetAllDisabledDate(match.HostTeamId, match.GuestTeamId).ToArray(); if (match.HostTeamId == match.GuestTeamId) { ModelState.AddModelError("HostTeamId", "The teams have to be different"); ModelState.AddModelError("GuestTeamId", "The teams have to be different"); } return(View(match)); }