//method for business logic. public WalletsAccount calculateBalance(WalletsAccount walletBucket, ref int?entryFees) { var bonusAmount = walletBucket.BonusBucket; var depositAmount = walletBucket.DepositBucket; var winningsAmount = walletBucket.WinningsBucket; WalletsAccount updatedWallet = new WalletsAccount(); updatedWallet.BonusBucket = entryFees / 10 < bonusAmount ? bonusAmount - (entryFees / 10) : 0; entryFees = entryFees - (bonusAmount - updatedWallet.BonusBucket); updatedWallet.DepositBucket = entryFees > 0?depositAmount > entryFees?depositAmount - entryFees:0:depositAmount; entryFees = entryFees - (depositAmount - updatedWallet.DepositBucket); updatedWallet.WinningsBucket = entryFees > 0 ? winningsAmount > entryFees ? winningsAmount - entryFees :0 :winningsAmount; entryFees = entryFees - (winningsAmount - updatedWallet.WinningsBucket); return(updatedWallet); }
public void calculateBalanceSameAmountTest() { int?entryAmount = 500; WalletManipulation walletManipulation = new WalletManipulation(); WalletsAccount userAccount = new WalletsAccount(); userAccount.BonusBucket = 50; userAccount.DepositBucket = 100; userAccount.WinningsBucket = 350; var updatedWallet = walletManipulation.calculateBalance(userAccount, ref entryAmount); Console.WriteLine(entryAmount); Assert.AreEqual(entryAmount, 0); Assert.AreEqual(updatedWallet.BonusBucket, 0); Assert.AreEqual(updatedWallet.DepositBucket, 0); Assert.AreEqual(updatedWallet.WinningsBucket, 0); }
//save user and contest details public HttpResponseMessage postContestData([FromUri] int userId, [FromUri] int contestId, [FromUri] float discount) { using (TestWallets dbContext = new TestWallets()) { try { var userResult = dbContext.UserDetails.ToList().Find(m => m.Id == userId); var contestDetail = dbContext.ContestsDetails.ToList().Find(m => m.ContestId == contestId); var contestToUserMapper = dbContext.UserToContestMappers.ToList().Find(m => m.ContestId == contestId && m.UserId == userId); var userWalletAccount = userResult != null?dbContext.WalletsAccounts.ToList().Find(m => m.WalletId == userResult.WalletId) : null; if (contestToUserMapper != null) { return(Request.CreateResponse(HttpStatusCode.Conflict)); } //condition to check whether user and contest exists and if user has already partipcated or not. if (userResult != null && contestDetail != null && userWalletAccount != null) { WalletManipulation walletManipulation = new WalletManipulation(); int?entryAmount = contestDetail.EntryAmount ?? 0; entryAmount = Convert.ToInt32(entryAmount - (entryAmount * (discount / 100))); var balance = ((userWalletAccount.BonusBucket > entryAmount / 10)?entryAmount / 10:userWalletAccount.BonusBucket) + userWalletAccount.DepositBucket + userWalletAccount.WinningsBucket; WalletsAccount updatedWallet = new WalletsAccount(); //condition to check that whether current balance of user is greater than entryAmount or not if (balance >= entryAmount) { updatedWallet = walletManipulation.calculateBalance(userWalletAccount, ref entryAmount); } else { return(Request.CreateResponse(HttpStatusCode.PreconditionFailed)); } contestToUserMapper = new UserToContestMapper(); contestToUserMapper.BonusAmount = userWalletAccount.BonusBucket - updatedWallet.BonusBucket; contestToUserMapper.DepositAmount = userWalletAccount.DepositBucket - updatedWallet.DepositBucket; contestToUserMapper.WinningsAmount = userWalletAccount.WinningsBucket - updatedWallet.WinningsBucket; contestToUserMapper.DiscountApplied = Convert.ToInt32(discount); contestToUserMapper.UserId = userId; contestToUserMapper.ContestId = contestId; userWalletAccount.BonusBucket = updatedWallet.BonusBucket; userWalletAccount.DepositBucket = updatedWallet.DepositBucket; userWalletAccount.WinningsBucket = updatedWallet.WinningsBucket; userWalletAccount.TotalWalletAmount = userWalletAccount.TotalWalletAmount - contestDetail.EntryAmount; dbContext.UserToContestMappers.Add(contestToUserMapper); dbContext.Entry(userWalletAccount).State = EntityState.Modified; dbContext.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } } catch (Exception ex) { throw ex; } } }