Skip to content

Commit 5367a56

Browse files
Initial service
1 parent c804aa7 commit 5367a56

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<PackageVersion Include="ContentFeedNuget" Version="$(ToolingPackagesVersion)" />
1717
</ItemGroup>
1818
<ItemGroup>
19-
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="9.0.1" />
19+
<PackageVersion Include="Sqids" Version="3.1.0" />
20+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
2021
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
2122
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
2223
<PackageVersion Include="EssentialCSharp.Shared.Models" Version="$(ToolingPackagesVersion)" />

EssentialCSharp.Web/EssentialCSharp.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<PackageReference Include="Newtonsoft.Json" />
3434
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
3535
<PackageReference Include="Octokit" />
36+
<PackageReference Include="Sqids" />
3637
</ItemGroup>
3738
<ItemGroup>
3839
<Content Update="wwwroot\images\00mindmap.svg">

EssentialCSharp.Web/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
using Microsoft.AspNetCore.HttpOverrides;
99
using Microsoft.AspNetCore.Identity;
1010
using Microsoft.AspNetCore.Identity.UI.Services;
11-
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.EntityFrameworkCore;
12+
using Sqids;
1213

1314
namespace EssentialCSharp.Web;
1415

@@ -104,8 +105,14 @@ private static void Main(string[] args)
104105
builder.Services.AddRazorPages();
105106
builder.Services.AddCaptchaService(builder.Configuration.GetSection(CaptchaOptions.CaptchaSender));
106107
builder.Services.AddSingleton<ISiteMappingService, SiteMappingService>();
107-
builder.Services.AddHostedService<DatabaseMigrationService>();
108-
108+
builder.Services.AddHostedService<DatabaseMigrationService>();
109+
builder.Services.AddSingleton(new SqidsEncoder<int>(new()
110+
{
111+
// This is a shuffled version of the default alphabet so the id's are at least unique to this site.
112+
// This being open source, it will be easy to decode the ids, but these id's are not meant to be secure.
113+
Alphabet = "imx4BSz2Ys7GZLXDqT5IAkUOEnyvwbPKJtp13NWdeuH6rFfRhCcQogjaM8V09l",
114+
MinLength = 10,
115+
}));
109116

110117
if (!builder.Environment.IsDevelopment())
111118
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Sqids;
2+
3+
namespace EssentialCSharp.Web.Services;
4+
5+
public class ReferrerService(SqidsEncoder<int> sqids)
6+
{
7+
public string GenerateReferrerLink(string baseUrl, string userId)
8+
{
9+
string referrerId = sqids.Encode(1, 2, 3);
10+
string referrerLink = $"{baseUrl}?referrerId={referrerId}";
11+
12+
// Store the referrerId and userId in the database for tracking
13+
SaveReferrerIdToDatabase(userId, referrerId);
14+
15+
return referrerLink;
16+
}
17+
18+
private void SaveReferrerIdToDatabase(string userId, string referrerId)
19+
{
20+
// Implement your database logic here
21+
}
22+
23+
/// <summary>
24+
/// Track the referral in the database.
25+
/// </summary>
26+
/// <param name="referrerId">The referrer ID to track.</param>
27+
/// <returns>True if the referral was successfully tracked, otherwise false.</returns>
28+
public bool TrackReferral(string referrerId)
29+
{
30+
// Implement your logic to track the referral in the database
31+
32+
if (sqids.Decode(referrerId) is [var decodedId] &&
33+
referrerId == sqids.Encode(decodedId))
34+
{
35+
// `incomingId` decodes into a single number and is canonical, here you can safely proceed with the rest of the logic
36+
}
37+
else
38+
{
39+
// consider `incomingId` invalid — e.g. respond with 404
40+
}
41+
IReadOnlyList<int> numbers = sqids.Decode(referrerId); // [1, 2, 3]
42+
}
43+
}

0 commit comments

Comments
 (0)