public bool ConnectTeamWithGame(int gameId, AwayHome model) { using (var ctx = new ApplicationDbContext()) { //grabbing a specific game via the URI var game = ctx .Games .Single(g => g.GameId == gameId); //grabbing the two specific teams from our model that the user specifies var homeTeam = ctx .Teams .Single(h => h.TeamId == model.HomeTeamId); var awayTeam = ctx .Teams .Single(a => a.TeamId == model.AwayTeamId); //adding the teams to our collection inside of the specific game game.ListOfTeams.Add(homeTeam); game.ListOfTeams.Add(awayTeam); //because of the many to many relationship I don't have to add the game to my list of games inside of team //set the specfic home team id to be just that inside of our database game.HomeTeamId = model.HomeTeamId; game.AwayTeamId = model.AwayTeamId; return(ctx.SaveChanges() >= 1); } }
/// <summary> /// Allows you to get a certian game by its ID. /// </summary> /// <param name="gameId"></param> /// <param name="model"></param> /// <returns></returns> public IHttpActionResult PutTeamsWithAGame(int gameId, AwayHome model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateGameService(); if (!service.ConnectTeamWithGame(gameId, model)) { return(InternalServerError()); } return(Ok()); }