Skip to content

Commit 113fb82

Browse files
committed
Merge pull request #3
Add Base62 encoding and refactor project structure
2 parents f151f8e + d1f7e2a commit 113fb82

17 files changed

+195
-3
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Test
2727
run: dotnet test --no-build --verbosity normal
2828
- name: dotnet publish
29-
run: dotnet publish UrlShortener.Api -c Release -o ${{env.DOTNET_ROOT}}/myapp
29+
run: dotnet publish Api/src/UrlShortener.Api -c Release -o ${{env.DOTNET_ROOT}}/myapp
3030
- name: Upload artifact for deployment job
3131
uses: actions/upload-artifact@v4
3232
with:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace UrlShortener.Api.Core;
2+
3+
public static class Base62EncodingExtensions
4+
{
5+
private const string Alphanumeric =
6+
"0123456789" +
7+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
8+
"abcdefghijklmnopqrstuvwxyz";
9+
10+
public static string EncodeToBase62(this long number)
11+
{
12+
if (number == 0) return Alphanumeric[0].ToString();
13+
14+
var result = new Stack<char>();
15+
while (number > 0)
16+
{
17+
result.Push(Alphanumeric[(int)(number % 62)]);
18+
number /= 62;
19+
}
20+
21+
return new string(result.ToArray());
22+
}
23+
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace UrlShortener.Api.Core;
2+
3+
public class ShortUrlGenerator
4+
{
5+
private readonly TokenProvider _tokenProvider;
6+
7+
public ShortUrlGenerator(TokenProvider tokenProvider)
8+
{
9+
_tokenProvider = tokenProvider;
10+
}
11+
12+
public string GenerateUniqueUrl()
13+
{
14+
return _tokenProvider.GetToken().EncodeToBase62();
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace UrlShortener.Api.Core;
2+
3+
public class TokenProvider
4+
{
5+
private readonly object _tokenLock = new();
6+
7+
private long _token = 0;
8+
private TokenRange? _tokenRange;
9+
10+
public void AssignRange(int start, int end)
11+
{
12+
AssignRange(new TokenRange(start, end));
13+
}
14+
15+
public void AssignRange(TokenRange tokenRange)
16+
{
17+
_tokenRange = tokenRange;
18+
_token = tokenRange.Start;
19+
}
20+
21+
public long GetToken()
22+
{
23+
lock (_tokenLock)
24+
{
25+
return _token++;
26+
}
27+
}
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace UrlShortener.Api.Core;
2+
3+
public record TokenRange(long Start, long End);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)