Skip to content

Commit

Permalink
Add ShortenEndpointFilter, Add Constants. Unifies all mesages.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 9, 2024
1 parent e656f1f commit c3d3f5e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SwiftLink.Application.Resources;
namespace SwiftLink.Application;

public static class ApplicationMessage
internal static class Constants
{
public static class Link
{
Expand Down
9 changes: 9 additions & 0 deletions src/SwiftLink.Presentation/Constants/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SwiftLink.Presentation;

internal static class Constants
{
public static class EndPointFilterMessages
{
public const string InvalidUrl = "Url is not valid! :(";
}
}
4 changes: 3 additions & 1 deletion src/SwiftLink.Presentation/Controllers/LinkController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using SwiftLink.Application.UseCases.Links.GenerateCommand;
using SwiftLink.Presentation.Filters;

namespace SwiftLink.Presentation.Controllers;

[ApiVersion("1.0")]
public class LinkController : BaseController
{
[HttpPost]
public IActionResult Generate([FromBody] GenerateShortCodeCommand command)
[ShortenEndpointFilter]
public IActionResult Shorten([FromBody] GenerateShortCodeCommand command)
{
return Ok(MediatR.Send(command));
}
Expand Down
21 changes: 21 additions & 0 deletions src/SwiftLink.Presentation/Filters/ShortenEndpointFilter.cs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions src/SwiftLink.Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SwiftLink.Application;
using SwiftLink.Infrastructure;
using SwiftLink.Shared;
using SwiftLink.Shared.Constants;

var builder = WebApplication.CreateBuilder(args);
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace SwiftLink.Shared;
public class Error
public class Constants
{
public static string DefaultMessage = "عملیات با خطا مواجه شد";
}

0 comments on commit c3d3f5e

Please sign in to comment.