public JoinSessionResponseModel JoinSession(JoinSessionRequestModel model) { ApplicationFactory.CurrentLogger.Info($@"BL_CreateGame > JoinSession start. SessionPin: {model.SessionPin} | Player Name: {model.Name} | Device Id: {model.DeviceId} | App Id: {model.AppId}"); ApplicationFactory.ValidateRequestModel(new JoinSessionRequestModelValidator(), model); JoinSessionResponseModel result = new JoinSessionResponseModel(); try { //refactor var session = db.T_Game_Sessions.SingleOrDefault(m => m.SessionPin == model.SessionPin && m.SessionIsActive); if (session.T_Game_Session_Players.Count() > 5) { result.Message = "There are too many players in the game."; throw new Exception(result.Message); } bool playerNameExists = session.T_Game_Session_Players.Any(m => m.PlayerName == model.Name); if (playerNameExists) { result.Message = "Player name exists. Please choose another name."; throw new Exception(result.Message); } //refactor using validator if (model.Name.Length > 10) { result.Message = "Player name has maximum length of 10 characters."; throw new Exception(result.Message); } T_Game_Session_Players newPlayer = new T_Game_Session_Players() { SessionId = session.SessionId, PlayerName = model.Name, JoinTime = DateTime.Now, DeviceId = model.DeviceId, RequestAppId = model.AppId, }; db.T_Game_Session_Players.Add(newPlayer); db.SaveChanges(); result.Successful = true; } catch (Exception ex) { ApplicationFactory.CurrentLogger.Error($"BL_CreateGame > JoinSession error. {ex.Message}"); result.Successful = false; } ApplicationFactory.LogWithObject($"BL_CreateGame > JoinSession success.", result); return(result); }
public async Task <JoinSessionResponseModel> JoinSession([FromBody] JoinSessionRequestModel model) { try { var blCreateGame = new BL_CreateGame(); return(await blCreateGame.JoinSessionAsync(model)); } catch (Exception ex) { ApplicationFactory.CurrentLogger.Error($"CreateGameController > JoinSession error. {ex.Message}"); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public async Task <JoinSessionResponseModel> JoinSessionAsync(JoinSessionRequestModel model) { return(await Task.Run(() => JoinSession(model))); }