Skip to content

Commit

Permalink
Refactor GeneratorShortCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 10, 2024
1 parent fd6f10a commit b4adb28
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@ public class GenerateShortCodeCommandHandler(IApplicationDbContext dbContext,

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

if (request.ExpirationDate is null)
link.ExpirationDate = DateTime.Now.AddDays(_options.DefaultExpirationTimeInDays);
else
link.ExpirationDate = request.ExpirationDate.Value;

if (link.Password is not null)
link.Password = PasswordHasher.HashPassword(request.Password);

_dbContext.Set<Link>().Add(link);

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

await _cache.Set(request.Url, JsonSerializer.Serialize(link), link.ExpirationDate);
return Result<object>.Success(link);
}
}
}

0 comments on commit b4adb28

Please sign in to comment.