-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete GenerateShortCodeHandler, Refactor algorithm of short code G…
…enerater.
- Loading branch information
1 parent
da24dc0
commit 077b6cd
Showing
9 changed files
with
61 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
src/SwiftLink.Application/UseCases/Link/GenerateCommand/GenerateShortCodeCommand.cs
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/SwiftLink.Application/UseCases/Link/GenerateCommand/GenerateShortCodeCommandHandler.cs
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/SwiftLink.Application/UseCases/Links/GenerateCommand/GenerateShortCodeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
38 changes: 38 additions & 0 deletions
38
src/SwiftLink.Application/UseCases/Links/GenerateCommand/GenerateShortCodeCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...rateCommand/GenerateShortCodeValidator.cs → ...rateCommand/GenerateShortCodeValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters