public ActionResult AddPlaySession(PlaySession playSession, List <int> playerIds) { try { db.PlaySessions.Add(playSession); db.SaveChanges(); //var newest = db.PlaySessions.FirstOrDefault(x => x.BoardGameId == playSession.BoardGameId && x.DatePlayed == playSession.DatePlayed); var CurrSession = db.PlaySessions.Where(x => x.BoardGameId == playSession.BoardGameId).ToList().Last(); var sessionId = CurrSession.Id; SaveParticipants(playerIds, sessionId); var psPlayers = new List <playerSearch>(); foreach (var playerId in playerIds) { string currName = db.Players.Where(x => x.Id == playerId).Select(x => x.Name).FirstOrDefault(); psPlayers.Add(new playerSearch(playerId, currName)); } var NewPlaysession = new PlaySessionDTO(CurrSession.BoardGame, psPlayers, CurrSession.PlayTime, CurrSession.DatePlayed.ToString(), CurrSession.Id); return(Json(NewPlaysession)); } catch (Exception e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "failed" + e)); } }
public List <PlaySessionDTO> getPlaysessionData(List <int> sessions) { var results = new List <PlaySessionDTO>(); foreach (var play in sessions) { var newPlaySession = db.PlaySessions.FirstOrDefault(s => s.Id.Equals(play)); var psBoardgame = db.BoardGames.Where(s => s.Id == newPlaySession.BoardGameId).FirstOrDefault(); var participants = db.Participants.Where(p => p.PlaySession_Id == newPlaySession.Id).ToList(); var psPlayers = new List <playerSearch>(); ExtractParticipants(participants, psPlayers); var stuff = newPlaySession.DatePlayed.Date; var PlaySessionFinal = new PlaySessionDTO(psBoardgame, psPlayers, newPlaySession.PlayTime, newPlaySession.DatePlayed.ToString(), newPlaySession.Id); results.Add(PlaySessionFinal); } return(results); }