Skip to content

Commit

Permalink
Complete GenerateShortCodeHandler, Refactor algorithm of short code G…
Browse files Browse the repository at this point in the history
…enerater.
  • Loading branch information
mohammadKarimi committed Jan 9, 2024
1 parent da24dc0 commit 077b6cd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
/// </summary>
public interface IShortCodeGenerator
{
Task<string> GenerateAsync(string originalUrl, CancellationToken cancellationToken = default);
string Generate(string originalUrl);
}
7 changes: 2 additions & 5 deletions src/SwiftLink.Application/Common/ShortCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ public class TimeBasedShortCodeGenerator : IShortCodeGenerator
/// <param name="originalUrl">the original link which must be shorted.</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> GenerateAsync(string originalUrl, CancellationToken cancellationToken = default)
{
var shortCode = $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}{GetRandomString(COUNT_OF_RANDOM_NUMBER)}{GetHash(originalUrl)}";
return await Task.FromResult(shortCode);
}
public string Generate(string originalUrl)
=> $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}{GetRandomString(COUNT_OF_RANDOM_NUMBER)}{GetHash(originalUrl)}";

private static string GetRandomString(int length)
{
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MediatR;

namespace SwiftLink.Application.UseCases.Links.GenerateCommand;

public record GenerateShortCodeCommand : IRequest<Result<object>>
{
public string Url { get; set; }
public string Description { get; set; }
public DateTime? ExpirationDate { get; set; }
public string Password { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MediatR;
using Microsoft.Extensions.Options;
using SwiftLink.Application.Common.Interfaces;

namespace SwiftLink.Application.UseCases.Links.GenerateCommand;

public class GenerateShortCodeCommandHandler(IApplicationDbContext dbContext,
ICacheProvider cacheProvider,
IShortCodeGenerator codeGenerator,
IOptions<AppSettings> options)
: IRequestHandler<GenerateShortCodeCommand, Result<object>>
{
private readonly IApplicationDbContext _dbContext = dbContext;
private readonly ICacheProvider _cache = cacheProvider;
private readonly IShortCodeGenerator _codeGenerator = codeGenerator;
private readonly AppSettings _options = options.Value;

public async Task<Result<object>> Handle(GenerateShortCodeCommand request, CancellationToken cancellationToken)
{
var link = new Link()
{
OriginalUrl = request.Url,
Password = request.Password,
ShortCode = _codeGenerator.Generate(request.Url),
ExpirationDate = request.ExpirationDate is null ? DateTime.Now.AddDays(_options.DefaultExpirationTimeInDays) : request.ExpirationDate.Value,
Description = request.Description,
SubscriberId = 1
};

_dbContext.Set<Link>().Add(link);
var dbResult = await _dbContext.SaveChangesAsync(cancellationToken);
if (dbResult.IsFailure)
return Result<object>.Failure();

await _cache.Set(request.Url, link.ShortCode);
return Result<object>.Success(link);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentValidation;
using SwiftLink.Application.Resources;

namespace SwiftLink.Application.UseCases.Link.GenerateCommand;
namespace SwiftLink.Application.UseCases.Links.GenerateCommand;

public class GenerateShortCodeValidator : AbstractValidator<GenerateShortCodeCommand>
{
Expand Down
6 changes: 5 additions & 1 deletion src/SwiftLink.Presentation/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"ApplicationDbContext": "Server=DESKTOP-TVCSFN3\\MHA;Database=SwiftLink;Trusted_Connection=True;Encrypt=false"
},
"AppSettings": {
"DefaultUrlOnNotFound": "https://agah.com/",
"DefaultExpirationTimeInDays": "7",
Expand All @@ -9,5 +12,6 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"AllowedHosts": "*"
}
3 changes: 3 additions & 0 deletions src/SwiftLink.Presentation/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"ApplicationDbContext": "Server=DESKTOP-TVCSFN3\\MHA;Database=SwiftLink;Trusted_Connection=True;Encrypt=false"
},
"AppSettings": {
"DefaultUrlOnNotFound": "https://agah.com/",
"DefaultExpirationTimeInDays": "7",
Expand Down

0 comments on commit 077b6cd

Please sign in to comment.