public async Task <BetDTO> CreateBet(CreateBetModel newBet) { var match = await _matchRepository.GetAsync(newBet.MatchConcernedId); if (match == null) { throw new AppException($"Match with id: {newBet.MatchConcernedId} doesn't exits."); } if (match.Participants.Away.Id != newBet.BetParticipantId && match.Participants.Home.Id != newBet.BetParticipantId) { throw new AppException($"The member with id: {newBet.BetParticipantId} does not participate in the match."); } var participants = await _participantRepository.GetAsync(newBet.BetParticipantId); var ownerUser = await _userRepository.GetSimpleUserAsync(newBet.OwnerId); if (newBet.Value <= 0) { throw new AppException($"The bet cannot be negative."); } if (ownerUser.Coins < newBet.Value) { throw new AppException($"The user does not have enough coins"); } if (DateTime.Now > match.BeginsAt) { throw new AppException($"Match has started already"); } ownerUser.Coins -= newBet.Value; var bet = new Bet(match, newBet.Value, participants, ownerUser, DateTime.Now); await _userRepository.UpdateAsync(ownerUser); await _betRepository.AddAsync(bet); return(_mapper.Map <BetDTO>(bet)); }
public async Task <bool> Bet(BetModel model) { try { var usuarios = await _usuarioRepository.ObterUsuarios(); var usuario = usuarios.FirstOrDefault(); var bet = new Bet() { Date = DateTime.Now, Stake = Convert.ToDecimal(model.Stake), ODD = Convert.ToDecimal(model.Odd), Green = model.Green, }; bet.Lucro = CalcProfit(bet.Green, bet.Stake, bet.ODD); usuario.AtualizarBancaAtual(bet.Lucro); var saldo = usuario.BancaInicial + bet.Lucro; bet.Porcentagem = (saldo - usuario.BancaInicial) / usuario.BancaInicial * 100; //bet.SaldoAtual = usuario.BancaAtual; await _betRepository.AddAsync(bet); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }