-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Create Referrer Id's to track link shares for users #722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5367a56
Initial service
BenjaminMichaelis 01ad95d
WIP on Referral Service
BenjaminMichaelis 379f58b
Add ability to view referral count
BenjaminMichaelis 3926fc7
Apply suggestions from code review
BenjaminMichaelis ceae0fc
Concurrency
BenjaminMichaelis e310229
Add referral Id's into hyperlinks
BenjaminMichaelis b1cacc7
PR Feedback
BenjaminMichaelis 3cc0b4c
Migration
BenjaminMichaelis cb47d43
Move from sqids
BenjaminMichaelis 2d7125e
Apply suggestions from code review
BenjaminMichaelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
EssentialCSharp.Web/Areas/Identity/Pages/Account/Manage/Referrals.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| @page | ||
| @model ReferralsDataModel | ||
| @{ | ||
| ViewData["Title"] = "Referrals"; | ||
| ViewData["ActivePage"] = ManageNavPages.Referrals; | ||
| } | ||
|
|
||
| <h3>@ViewData["Title"]</h3> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-6"> | ||
| <p> | ||
| You have <strong>@Model.ReferralCount</strong> referrals. | ||
| </p> | ||
| </div> | ||
|
|
||
| @section Scripts { | ||
| <partial name="_ValidationScriptsPartial" /> | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
EssentialCSharp.Web/Areas/Identity/Pages/Account/Manage/Referrals.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using EssentialCSharp.Web.Areas.Identity.Data; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| namespace EssentialCSharp.Web.Areas.Identity.Pages.Account.Manage; | ||
|
|
||
| public class ReferralsDataModel : PageModel | ||
| { | ||
| private readonly UserManager<EssentialCSharpWebUser> _UserManager; | ||
| private readonly ILogger<ReferralsDataModel> _Logger; | ||
|
|
||
| public int ReferralCount { get; set; } | ||
BenjaminMichaelis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public ReferralsDataModel( | ||
| UserManager<EssentialCSharpWebUser> userManager, | ||
| ILogger<ReferralsDataModel> logger) | ||
| { | ||
| _UserManager = userManager; | ||
| _Logger = logger; | ||
| } | ||
|
|
||
| public async Task<IActionResult> OnGetAsync() | ||
| { | ||
| EssentialCSharpWebUser? user = await _UserManager.GetUserAsync(User); | ||
| if (user is null) | ||
| { | ||
| return NotFound($"Unable to load user with ID '{_UserManager.GetUserId(User)}'."); | ||
| } | ||
| ReferralCount = user.ReferralCount; | ||
|
|
||
| return Page(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System.Security.Claims; | ||
|
|
||
| namespace EssentialCSharp.Web.Extensions | ||
BenjaminMichaelis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public static class ClaimsExtensions | ||
| { | ||
| public static string? GetReferrerId(this ClaimsPrincipal claimsPrincipal) | ||
| { | ||
| return claimsPrincipal.FindFirstValue(ReferrerIdClaimType); | ||
| } | ||
|
|
||
| public static string? GetReferrerId(this IList<Claim> claims) | ||
| { | ||
| return claims.FirstOrDefault(claim => claim.Type == ReferrerIdClaimType)?.Value; | ||
| } | ||
| public const string ReferrerIdClaimType = "ReferrerId"; | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
EssentialCSharp.Web/Middleware/ReferralTrackingMiddleware.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using System.Security.Claims; | ||
| using System.Web; | ||
| using EssentialCSharp.Web.Areas.Identity.Data; | ||
| using EssentialCSharp.Web.Services.Referrals; | ||
| using Microsoft.AspNetCore.Identity; | ||
|
|
||
| namespace EssentialCSharp.Web.Middleware; | ||
|
|
||
| public sealed class ReferralMiddleware | ||
| { | ||
| private readonly RequestDelegate _Next; | ||
|
|
||
| public ReferralMiddleware(RequestDelegate next) | ||
| { | ||
| _Next = next; | ||
| } | ||
|
|
||
| public async Task InvokeAsync(HttpContext context, IReferralService referralService, UserManager<EssentialCSharpWebUser> userManager) | ||
| { | ||
| // Retrieve current referral Id for processing | ||
| System.Collections.Specialized.NameValueCollection query = HttpUtility.ParseQueryString(context.Request.QueryString.Value!); | ||
| string? referralId = query["rid"]; | ||
| if (context.User is { Identity.IsAuthenticated: true } claimsUser) | ||
| { | ||
| TrackReferralAsync(referralService, referralId, claimsUser); | ||
| } | ||
| else | ||
| { | ||
| TrackReferralAsync(referralService, referralId, null); | ||
| } | ||
|
|
||
| await _Next(context); | ||
|
|
||
| static void TrackReferralAsync(IReferralService referralService, string? referralId, ClaimsPrincipal? claimsUser) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(referralId)) | ||
| { | ||
| referralService.TrackReferralAsync(referralId, claimsUser); | ||
| } | ||
BenjaminMichaelis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.