public GamePlayHistory AddNewGameHistory(GamePlayHistory gamePlayHistory)
        {
            if (string.IsNullOrEmpty(ToSavourToken))
            {
                throw new InvalidOperationException("No ToSavour Token is set");
            }

            RequestClient = new WebClient();
            RequestClient.Headers.Add("Authorization", ToSavourToken);
            RequestClient.Headers.Add("Content-Type", "application/json");

            var memoryStream = new MemoryStream();
            GetSerializer(typeof(GamePlayHistory)).WriteObject(memoryStream, gamePlayHistory);

            memoryStream.Position = 0;
            var sr = new StreamReader(memoryStream);
            var json = sr.ReadToEnd();

            var userJsonString = RequestClient.UploadString(_host + @"gamehistories", json);

            var byteArray = Encoding.ASCII.GetBytes(userJsonString);
            var stream = new MemoryStream(byteArray);

            var returnedGamePlayHistory = GetSerializer(typeof(GamePlayHistory)).ReadObject(stream) as GamePlayHistory;

            return returnedGamePlayHistory;
        }
        public void UpdateGameHistory()
        {
            // Login
            var userList = So.GetAllUsers();
            Assert.IsTrue(userList != null && userList.Count > 0);

            // Get list of game
            var gameList = So.GetDailyGame();
            Assert.IsTrue(gameList != null && gameList.Count > 0);

            // Create a Game Play History
            var gameHistory = new GamePlayHistory
            {
                DailyGameId = gameList[0].Id,
                PlayedDateTime = DateTime.UtcNow,
                Result = "",
                UserId = userList[0].Id
            };

            var returnedGame = So.AddNewGameHistory(gameHistory);
            Assert.IsTrue(returnedGame != null);
            Assert.AreEqual(userList[0].Id, returnedGame.UserId);

            // change something
            returnedGame.Result = "Win";

            // Update the History
            var updatedGameHistory = So.UpdateGameHistory(returnedGame.Id.ToString(CultureInfo.InvariantCulture), returnedGame);
            Assert.IsTrue(updatedGameHistory != null);
            Assert.AreEqual(userList[0].Id, updatedGameHistory.UserId);
            Assert.AreEqual(returnedGame.Result.ToLower(), updatedGameHistory.Result.ToLower());
        }
        public GamePlayHistory AddNewGameHistory(GamePlayHistory gamePlayHistory)
        {
            if (_userId == Guid.Empty)
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return null;
            }

            gamePlayHistory.UserId = _userId;
            gamePlayHistory.PlayedDateTime = DateTime.UtcNow;
            if (gamePlayHistory.Result != null)
            {
                gamePlayHistory.Result = gamePlayHistory.Result.ToLower();
            }

            switch (gamePlayHistory.Result)
            {
                case "win":
                case "lose":
                    gamePlayHistory.Result = gamePlayHistory.Result.ToLower();
                    break;
                default:
                    gamePlayHistory.Result = string.Empty;
                    break;
            }

            _dbContext.GamePlayHistories.Add(gamePlayHistory);
            _dbContext.SaveChanges();

            var last24Hours = DateTime.UtcNow.AddDays(-1);

            var resultGamePlayHistory = _dbContext.GamePlayHistories.Where(
                g => g.PlayedDateTime > last24Hours && g.UserId == _userId)
                      .OrderByDescending(h => h.PlayedDateTime)
                      .First(); // Possible Exception for First only.

            return resultGamePlayHistory;
        }
        public void PostNewGameHistory()
        {
            // Login
            var userList = So.GetAllUsers();
            Assert.IsTrue(userList != null && userList.Count > 0);

            // Get list of game
            var gameList = So.GetDailyGame();
            Assert.IsTrue(gameList != null && gameList.Count > 0);

            var gameHistory = new GamePlayHistory
                {
                    DailyGameId = gameList[0].Id,
                    PlayedDateTime = DateTime.UtcNow,
                    Result = "Win",
                    UserId = userList[0].Id
                };

            // Get list of game
            var returnedGame = So.AddNewGameHistory(gameHistory);
            Assert.IsTrue(returnedGame != null);
            Assert.AreEqual(userList[0].Id, returnedGame.UserId);
        }
        public GamePlayHistory UpdateGameHistory(string gameHistoriesId, GamePlayHistory gamePlayHistory)
        {
            if (_userId == Guid.Empty)
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return null;
            }

            if (gameHistoriesId != gamePlayHistory.Id.ToString(CultureInfo.InvariantCulture))
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return null;
            }

            // Lookup the Game Play History
            var resultGamePlayHistory = _dbContext.GamePlayHistories.FirstOrDefault(g => g.Id == gamePlayHistory.Id);

            if (resultGamePlayHistory == null)
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return null;
            }

            // If there is no change to the game play result, do nothing.
            if (resultGamePlayHistory.Result == gamePlayHistory.Result)
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified;
                return null;
            }

            // Otherwise make the game result changes we need
            switch (gamePlayHistory.Result.ToLower())
            {
                case "win":
                case "lose":
                    resultGamePlayHistory.Result = gamePlayHistory.Result.ToLower();
                    break;
                default:
                    resultGamePlayHistory.Result = string.Empty;
                    break;
            }

            // Update database
            _dbContext.SaveChanges();

            var returnedGamePlayHistory = _dbContext.GamePlayHistories.FirstOrDefault(h => h.Id == resultGamePlayHistory.Id);

            if (returnedGamePlayHistory == null)
            {
                // Something went wrong, the recently saved game play history was not able to be retrieved from database. Log@@@
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return null;
            }

            // Upoin Successful database update, give the winner a new coupons.
            if (resultGamePlayHistory.Result == "win")
            {
                // trigger Game win and add coupon scenarios
                var newCoupon = _dbContext.Coupons.Create();
                newCoupon.ReceiverUserId = _userId;
                newCoupon.CreatedDateTime = DateTime.UtcNow;
                newCoupon.RedeemedDateTime = DateTime.MaxValue;
                newCoupon.SponsorName = _dbContext.DailyGames.First(g => g.Id == returnedGamePlayHistory.DailyGameId).SponsorName;
                newCoupon.Price = 15;

                var newItem = new Item { ProductId = 1, CreatedDateTime = DateTime.UtcNow, Status = "pending" };
                newItem.ItemSelectedOptions.Add(new ItemSelectedOption { OptionChoiceId = 3 });
                newItem.ItemSelectedOptions.Add(new ItemSelectedOption { OptionChoiceId = 7 });
                newItem.ItemSelectedOptions.Add(new ItemSelectedOption { OptionChoiceId = 10 });
                newCoupon.Items.Add(newItem);

                newCoupon.ReferenceCode = ReferenceCodeGenerator();

                _dbContext.Coupons.Add(newCoupon);
                _dbContext.SaveChanges();
            }

            return resultGamePlayHistory;
        }