コード例 #1
0
    public async Task <IActionResult> InvitePlayer([FromBody] CreateInviteRequest request)
    {
        var leagueRecord = await GetExistingLeague(request.LeagueID, RequiredRelationship.LeagueManager);

        if (leagueRecord.FailedResult is not null)
        {
            return(leagueRecord.FailedResult);
        }
        var validResult = leagueRecord.ValidResult !;
        var league      = validResult.League;

        string            baseURL = $"{Request.Scheme}://{Request.Host.Value}";
        FantasyCriticUser inviteUser;

        if (!request.IsDisplayNameInvite())
        {
            if (request.InviteEmail is null)
            {
                return(BadRequest());
            }

            string inviteEmail = request.InviteEmail.ToLower();
            inviteUser = await _userManager.FindByEmailAsync(inviteEmail);

            if (inviteUser is null)
            {
                Result userlessInviteResult = await _leagueMemberService.InviteUserByEmail(league, inviteEmail);

                if (userlessInviteResult.IsFailure)
                {
                    return(BadRequest(userlessInviteResult.Error));
                }

                await _emailSendingService.SendSiteInviteEmail(inviteEmail, league, baseURL);

                return(Ok());
            }
        }
        else
        {
            inviteUser = await _userManager.FindByDisplayName(request.InviteDisplayName !, request.InviteDisplayNumber !.Value);
        }

        if (inviteUser is null)
        {
            return(BadRequest("No user is found with that information."));
        }

        Result userInviteResult = await _leagueMemberService.InviteUserByUserID(league, inviteUser);

        if (userInviteResult.IsFailure)
        {
            return(BadRequest(userInviteResult.Error));
        }

        await _emailSendingService.SendLeagueInviteEmail(inviteUser.Email, league, baseURL);

        return(Ok());
    }