Example #1
0
        public async Task <bool> AddBet(BetViewModel newBet)
        {
            // TODO Map betviewmodel to CreateBetDto
            var betDto = new CreateBetDto();

            var successfullyCreatedBet = await
                                         _httpClient.ExecutePostRequest <CreateBetDto, BetDto>(betDto, _configProvider.CreateBetResourceUrl, true);

            return(successfullyCreatedBet != null);
        }
        public async Task <IActionResult> CreateBet([FromBody] CreateBetDto createBetDto)
        {
            try
            {
                if (createBetDto == null || !ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var user = await GetUser();

                if (user == null)
                {
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var bet = Mapper.Map <Bet>(createBetDto);
                bet.User = user;

                await _betRepository.AddBet(bet);

                if (!await _betRepository.Save())
                {
                    return(GetErrorStatusCodeAndGenericMessage());
                }

                var betDtoToReturn = Mapper.Map <BetDto>(bet);

                return(CreatedAtRoute("GetBet", new { betId = betDtoToReturn.Id }, betDtoToReturn));
            }
            catch (Exception)
            {
                return(GetErrorStatusCodeAndGenericMessage());
            }
        }