Skip to content

Commit

Permalink
Add UrlFormatChecker for Centeralize Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 10, 2024
1 parent e40b84a commit fd6f10a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

public interface ICacheProvider
{

/// <summary>
/// Sets a string value in the cache with a specified expiration date.
/// </summary>
/// <param name="key">The key under which the value will be stored.</param>
/// <param name="value">The string value to be stored in the cache.</param>
/// <param name="expirationDate">The expiration date for the cached item.</param>
/// <returns>A task representing the asynchronous operation, returning true if the operation was successful.</returns>
public Task<bool> Set(string key, string value, DateTime expirationDate);

/// <summary>
/// Associate a value with a key in the Cache such as Redis.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/SwiftLink.Application/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ internal static class Constants
public static class Link
{
public static string UrlMustBeSent = "Url must be sent!";

public static string InvalidUrlFormat = "Invalid Url Format!";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Options;
using SwiftLink.Application.Common;
using SwiftLink.Application.Common.Interfaces;
using System.Text.Json;

namespace SwiftLink.Application.UseCases.Links.GenerateCommand;

Expand All @@ -21,19 +22,25 @@ public async Task<Result<object>> Handle(GenerateShortCodeCommand request, Cance
var link = new Link()
{
OriginalUrl = request.Url,
Password = PasswordHasher.HashPassword(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
};

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, link.ShortCode);
await _cache.Set(request.Url, JsonSerializer.Serialize(link), link.ExpirationDate);
return Result<object>.Success(link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ public class GenerateShortCodeValidator : AbstractValidator<GenerateShortCodeCom
{
public GenerateShortCodeValidator()
{
RuleFor(x => x.Url).NotNull()
.WithMessage(Constants.Link.UrlMustBeSent);
RuleFor(x => x.Url)
.NotNull().WithMessage(Constants.Link.UrlMustBeSent)
.Must(BeAValidUrl).WithMessage(Constants.Link.InvalidUrlFormat);
}
}

private bool BeAValidUrl(string url)
=> UrlFormatChecker.UrlRegex().IsMatch(url);
}
10 changes: 4 additions & 6 deletions src/SwiftLink.Presentation/Filters/ShortenEndpointFilter.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using Microsoft.AspNetCore.Mvc.Filters;
using SwiftLink.Application.UseCases.Links.GenerateCommand;
using SwiftLink.Shared;
using System.Text.RegularExpressions;

namespace SwiftLink.Presentation.Filters;

public partial class ShortenEndpointFilter : ActionFilterAttribute
{
private const int _urlArgumentIndex = 0;
private const string _pattern = @"^(?:(?:https?|ftp)://)?[^\s/$.?#].[^\s]*$";


public override void OnActionExecuting(ActionExecutingContext context)
{
}

private static bool IsValidUrl(string url)
=> ShortCodeRegex().IsMatch(url);

[GeneratedRegex(_pattern, RegexOptions.IgnoreCase, "en-US")]
private static partial Regex ShortCodeRegex();
=> UrlFormatChecker.UrlRegex().IsMatch(url);

}
10 changes: 10 additions & 0 deletions src/SwiftLink.Shared/UrlRegex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.RegularExpressions;

namespace SwiftLink.Shared;
public static partial class UrlFormatChecker
{
private const string _pattern = @"^(https?|ftp)://[^\s/$.?#].[^\s]*$";

[GeneratedRegex(_pattern, RegexOptions.IgnoreCase, "en-US")]
public static partial Regex UrlRegex();
}

0 comments on commit fd6f10a

Please sign in to comment.