diff --git a/src/SwiftLink.Application/Resources/ApplicationMessage.cs b/src/SwiftLink.Application/Constants/Constants.cs similarity index 56% rename from src/SwiftLink.Application/Resources/ApplicationMessage.cs rename to src/SwiftLink.Application/Constants/Constants.cs index 7369b19..8ca0207 100644 --- a/src/SwiftLink.Application/Resources/ApplicationMessage.cs +++ b/src/SwiftLink.Application/Constants/Constants.cs @@ -1,6 +1,6 @@ -namespace SwiftLink.Application.Resources; +namespace SwiftLink.Application; -public static class ApplicationMessage +internal static class Constants { public static class Link { diff --git a/src/SwiftLink.Presentation/Constants/Constants.cs b/src/SwiftLink.Presentation/Constants/Constants.cs new file mode 100644 index 0000000..9d0a811 --- /dev/null +++ b/src/SwiftLink.Presentation/Constants/Constants.cs @@ -0,0 +1,9 @@ +namespace SwiftLink.Presentation; + +internal static class Constants +{ + public static class EndPointFilterMessages + { + public const string InvalidUrl = "Url is not valid! :("; + } +} diff --git a/src/SwiftLink.Presentation/Controllers/LinkController.cs b/src/SwiftLink.Presentation/Controllers/LinkController.cs index 817ca6c..586f998 100644 --- a/src/SwiftLink.Presentation/Controllers/LinkController.cs +++ b/src/SwiftLink.Presentation/Controllers/LinkController.cs @@ -1,6 +1,7 @@ using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using SwiftLink.Application.UseCases.Links.GenerateCommand; +using SwiftLink.Presentation.Filters; namespace SwiftLink.Presentation.Controllers; @@ -8,7 +9,8 @@ namespace SwiftLink.Presentation.Controllers; public class LinkController : BaseController { [HttpPost] - public IActionResult Generate([FromBody] GenerateShortCodeCommand command) + [ShortenEndpointFilter] + public IActionResult Shorten([FromBody] GenerateShortCodeCommand command) { return Ok(MediatR.Send(command)); } diff --git a/src/SwiftLink.Presentation/Filters/ShortenEndpointFilter.cs b/src/SwiftLink.Presentation/Filters/ShortenEndpointFilter.cs new file mode 100644 index 0000000..2cc31fe --- /dev/null +++ b/src/SwiftLink.Presentation/Filters/ShortenEndpointFilter.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Mvc.Filters; +using SwiftLink.Application.UseCases.Links.GenerateCommand; +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(); +} \ No newline at end of file diff --git a/src/SwiftLink.Presentation/Program.cs b/src/SwiftLink.Presentation/Program.cs index 78cd9e3..3a4495d 100644 --- a/src/SwiftLink.Presentation/Program.cs +++ b/src/SwiftLink.Presentation/Program.cs @@ -3,6 +3,7 @@ using SwiftLink.Application; using SwiftLink.Infrastructure; using SwiftLink.Shared; +using SwiftLink.Shared.Constants; var builder = WebApplication.CreateBuilder(args); { diff --git a/src/SwiftLink.Shared/Error.cs b/src/SwiftLink.Shared/Constants/Constants.cs similarity index 84% rename from src/SwiftLink.Shared/Error.cs rename to src/SwiftLink.Shared/Constants/Constants.cs index ec10752..4d662a6 100644 --- a/src/SwiftLink.Shared/Error.cs +++ b/src/SwiftLink.Shared/Constants/Constants.cs @@ -1,5 +1,5 @@ namespace SwiftLink.Shared; -public class Error +public class Constants { public static string DefaultMessage = "عملیات با خطا مواجه شد"; }