public IActionResult putPlayer(Request req, int ID) { Player player = new Player { IdPlayer = req.IdPlayer, FirstName = req.FirstName, LastName = req.LastName, DateOfBirth = req.BirthDate }; Player_Team pt = new Player_Team { IdPlayer = req.IdPlayer, IdTeam = ID, NumonShirt = req.Numonshirt, Comment = req.Comment }; if (!_context.Players.Any(s => s.LastName.Equals(req.LastName))) { _context.Players.Add(player); } _context.Player_Teams.Add(pt); _context.SaveChanges(); return(new OkObjectResult(player)); }
public async Task <IActionResult> PlayerT(local_player player, int teamx) { if (player == null) { return(BadRequest("Brak danych playera w zapytaniu")); } var team = db.Team.Where(v => v.IdTeam == teamx).FirstOrDefault(); if (team == null) { return(BadRequest("Brak druzyny")); } var hasPlayer = db.Player.Where(v => v.FirstName == player.firstName && v.LastName == player.lastName).FirstOrDefault(); if (hasPlayer == null) { return(BadRequest("Brak playera w bazie danych")); } DateTime age = new DateTime(); if (!DateTime.TryParse(player.birthdate, out age)) { return(BadRequest()); } if (team.MaxAge != 0) { var ageLimit = DateTime.Now.AddYears(-team.MaxAge); if (age < ageLimit) { return(BadRequest("Przekroczony wiek")); } } var isInTeam = db.Player_Team.Where(v => v.IdTeam == teamx && v.IdPlayer == hasPlayer.IdPlayer).FirstOrDefault(); if (isInTeam != null) { return(BadRequest("Player jest już w tej drużynie")); } else { Player_Team PT = new Player_Team() { IdPlayer = hasPlayer.IdPlayer, IdTeam = teamx, NumOnShirt = player.numOnShirt, Comment = player.comment }; db.Player_Team.Add(PT); await db.SaveChangesAsync(); return(Ok()); } }
public IActionResult AssignPlayer(int id, AssignPlayerRequest request) { var team = _context.Team.SingleOrDefault(t => t.IdTeam == id); if (team == null) { return(NotFound("Nie ma takeij drużyny")); } var player = _context.Player .SingleOrDefault(p => p.FirstName == request.FirstName && p.LastName == request.LastName && p.DateOfBirth == request.GetBirthDate() ); if (player == null) { return(NotFound("Nie ma takeiego gracza")); } if (player.DateOfBirth.Year > team.MaxAge) { return(NotFound("Gracz zbyt stary")); } var playerTeam = _context.Player_Team.SingleOrDefault(pt => pt.IdPlayer == player.IdPlayer && pt.IdTeam == team.IdTeam); if (playerTeam != null) { return(NotFound("Gracz jest już przypisany")); } playerTeam = new Player_Team(); playerTeam.IdPlayer = player.IdPlayer; playerTeam.IdTeam = team.IdTeam; playerTeam.NumOnShirt = request.NumOnShirt; playerTeam.Comment = request.comment; player.Player_Teams.Add(playerTeam); team.Player_Teams.Add(playerTeam); _context.Attach(playerTeam); _context.Attach(team); _context.Attach(player); _context.SaveChanges(); return(Ok("Aktualizacja ukończona")); }
public Player_Team addToTeam(int IdTeam, NewPlayerDTO player) { Team team = context.Teams.Find(IdTeam); if (team == null) { throw new MyException("Brak takiej druzyny"); } Player databasePlayer = (from plr in context.Players where plr.FirstName == player.firstName && plr.LastName == player.lastName && plr.DateOfBirth.Equals(player.birthdate) select new Player { IdPlayer = plr.IdPlayer, DateOfBirth = plr.DateOfBirth, FirstName = plr.FirstName, LastName = plr.LastName }).First(); if (databasePlayer == null) { throw new MyException("Gracza nie ma w bazie"); } if ((DateTime.Today.Year - databasePlayer.DateOfBirth.Year) > team.MaxAge) { throw new MyException("Gracz za stary"); } if (context.Player_Teams.Any(item => item.IdPlayer == databasePlayer.IdPlayer)) { throw new MyException("gracz juz jest przypisany"); } Player_Team player_team = new Player_Team() { Player = databasePlayer, Team = team, NumOfShirt = player.numOnShirt, Comment = player.Comment }; context.Player_Teams.Add(player_team); context.SaveChanges(); return(player_team); }
public IActionResult GetTeamsForChampioship(int idTeam, AddPlayerRequest addPlayerRequest) { Team team = _context.Teams.Find(idTeam); if (team == null) { return(BadRequest("Nie ma takiego zespołu!")); } Player player = _context.Players .Where(p => p.FirstName.Equals(addPlayerRequest.FirstName)) .Where(p => p.LastName.Equals(addPlayerRequest.LastName)) .Where(p => p.DateOfBirth.Equals(addPlayerRequest.DateOfBirth)).First(); if (player == null) { return(BadRequest("Nie ma takiego zawodnika!")); } var age = (DateTime.Now.Year - addPlayerRequest.DateOfBirth.Value.Year); if (age > team.MaxAge) { return(BadRequest("Zawodnik jest za stary na tę drużynę!")); } var _link = _context.Player_Teams.Where(pt => pt.Player.Equals(player)).First(); if (_link != null) { return(BadRequest("Zawodnik już ma drużynę!")); } Player_Team player_Team = new Player_Team(); player_Team.Player = player; player_Team.Team = team; _context.Player_Teams.Add(player_Team); _context.SaveChanges(); return(Ok("Dodano")); }