Skip to content

Commit

Permalink
Merge pull request #191 from Hedlund01/issue_177a
Browse files Browse the repository at this point in the history
ID Collison between Generated and Vanity URLS
  • Loading branch information
FBoucher authored Dec 29, 2020
2 parents 1c9a43c + 4ff397d commit 120f2e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/shortenerTools/Domain/StorageTableHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;

Expand Down Expand Up @@ -85,6 +87,32 @@ public async Task<List<ClickStatsEntity>> GetAllStatsByVanity(string vanity)
return lstShortUrl;
}

/// <summary>
/// Returns the ShortUrlEntity of the <paramref name="vanity"/>
/// </summary>
/// <param name="vanity"></param>
/// <returns>ShortUrlEntity</returns>
public async Task<ShortUrlEntity> GetShortUrlEntityByVanity(string vanity)
{
var tblUrls = GetUrlsTable();
TableContinuationToken token = null;
ShortUrlEntity shortUrlEntity = null;
do
{
TableQuery<ShortUrlEntity> query = new TableQuery<ShortUrlEntity>().Where(
filter: TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, vanity));
var queryResult = await tblUrls.ExecuteQuerySegmentedAsync(query, token);
shortUrlEntity = queryResult.Results.FirstOrDefault();
} while (token != null);

return shortUrlEntity;
}

public async Task<bool> IfShortUrlEntityExistByVanity(string vanity)
{
ShortUrlEntity shortUrlEntity = await GetShortUrlEntityByVanity(vanity);
return (shortUrlEntity != null);
}

public async Task<bool> IfShortUrlEntityExist(ShortUrlEntity row)
{
Expand Down
3 changes: 3 additions & 0 deletions src/shortenerTools/Domain/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static async Task<string> GetValidEndUrl(string vanity, StorageTableHelpe
{
var newKey = await stgHelper.GetNextTableId();
string getCode() => Encode(newKey);
if (await stgHelper.IfShortUrlEntityExistByVanity(getCode()))
return await GetValidEndUrl(vanity, stgHelper);

return string.Join(string.Empty, getCode());
}
else
Expand Down

0 comments on commit 120f2e9

Please sign in to comment.