Example #1
0
        public async Task <IActionResult> JoinGroup([FromBody] JoinBetGroupCommand command)
        {
            var user = await GetCurrentUser();

            var userBetGroup = await _dbContext
                               .UserBetGroups
                               .SingleOrDefaultAsync(q => q.BetGroup.GroupCode == command.GroupCode &&
                                                     q.User.Id == user.Id);

            if (userBetGroup?.BetGroupId == 0)
            {
                return(BadRequest());
            }

            var token = GenerateJwtToken(user, userBetGroup.BetGroupId);

            return(Ok(new { Token = token }));
        }
        public async Task <IActionResult> JoinBetGroup([FromBody] JoinBetGroupCommand command)
        {
            var user = await GetCurrentUser();

            var betGroup = await _dbContext
                           .BetGroups
                           .SingleOrDefaultAsync(q => q.GroupCode == command.GroupCode);

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

            var existingBetUserGroup = await _dbContext
                                       .UserBetGroups.
                                       SingleOrDefaultAsync(q => q.BetGroupId == betGroup.Id && q.UserId == user.Id);

            if (existingBetUserGroup != null)
            {
                return(Ok(new { betGroup.Name, betGroup.Id }));
            }

            var newBetUserGroup = new UserBetGroup()
            {
                BetGroupId = betGroup.Id,
                UserId     = user.Id
            };

            _dbContext.Add(newBetUserGroup);

            await AddEmptyBonusPredictionsForUser(newBetUserGroup);

            await _dbContext.SaveChangesAsync();

            return(Ok(new { betGroup.Name, betGroup.Id }));
        }