Skip to content

Commit 5938fa7

Browse files
committed
Casting from int to long
1 parent b310e40 commit 5938fa7

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Api/src/UrlShortener.Core/Base62EncodingExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ public static class Base62EncodingExtensions
55
private const string Alphanumeric = "0123456789" +
66
"abcdefghijklmnopqrstuvwxyz" +
77
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
8-
public static string EncodeToBase62(this int number)
8+
public static string EncodeToBase62(this long number)
99
{
1010
if(number is 0) return Alphanumeric[0].ToString();
1111

1212
var result = new Stack<char>();
1313

1414
while (number > 0)
1515
{
16-
result.Push(Alphanumeric[number % 62]);
16+
result.Push(Alphanumeric[(int)(number % 62)]);
1717
number /= 62;
1818
}
1919

Api/src/UrlShortener.Core/TokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public void AssignRange(TokenRange tokenRange)
1111
{
1212
_tokenRange = tokenRange;
1313
}
14-
public int GetToken()
14+
public long GetToken()
1515
{
16-
return _tokenRange!.Start;
16+
return _tokenRange.Start;
1717
}
1818
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
namespace UrlShortener.Core;
22

3-
public record TokenRange(int Start,int End);
3+
public record TokenRange
4+
{
5+
public long Start { get; }
6+
public long End { get; }
7+
8+
public TokenRange(long start, long end)
9+
{
10+
if(end< start)
11+
throw new ArgumentException("End must be greater than or equal to start");
12+
13+
Start = start;
14+
End = end;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using UrlShortener.Core;
2+
3+
namespace UrlShortener.Api.Core.Tests;
4+
5+
public class TokenRangeScenarios
6+
{
7+
[Fact]
8+
public void When_start_token_is_greater_than_end_token_then_throws_exception()
9+
{
10+
var act = () => new TokenRange(10, 5);
11+
12+
act.Should()
13+
.Throw<ArgumentException>()
14+
.WithMessage("End must be greater than or equal to start");
15+
}
16+
}

0 commit comments

Comments
 (0)