Skip to content

Commit 3ae7c3b

Browse files
Add Inquery for back-half text.
Refactor Error Type and Failure
1 parent b38dc0a commit 3ae7c3b

11 files changed

Lines changed: 51 additions & 17 deletions

File tree

src/SwiftLink.Application/Common/CommonMessages.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public static class CommonMessages
44
{
55
public static class Database
66
{
7-
public static readonly Error InsertFailed = Error.Failure("Database.Failed", "Insert into db is failed :(");
8-
public static readonly Error UpdateFailed = Error.Failure("Database.Failed", "Update record in db is failed :(");
7+
public static readonly Error InsertFailed = Error.Exception("Database.Failed", "Insert into db is failed :(");
8+
public static readonly Error UpdateFailed = Error.Exception("Database.Failed", "Update record in db is failed :(");
99
}
1010
}

src/SwiftLink.Application/UseCases/Links/Commands/GenerateShortCode/GenerateShortCodeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public GenerateShortCodeValidator(IApplicationDbContext dbContext)
1616
.Must(BeAValidUrl).WithMessage(LinkMessages.InvalidUrlFormat.Message);
1717

1818
RuleFor(x => x.BackHalf)
19-
.MustAsync(BeAValidBackHalf).WithMessage(LinkMessages.InvalidBackHalf.Message);
19+
.MustAsync(BeAValidBackHalf).WithMessage(LinkMessages.BackHalfIsExist.Message);
2020

2121
RuleFor(x => x.ExpirationDate)
2222
.Must(BeAValidExpirationDate).WithMessage(LinkMessages.ExpirationDateMustBeMoreThanTomorrow.Message);

src/SwiftLink.Application/UseCases/Links/LinkMessages.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal record LinkMessages
1111
public static readonly Error LinkIsNotFound = Error.Validation("ShortCodeValidation", "Invalid ShortCode");
1212
public static readonly Error InvalidLinkId = Error.Validation("LinkValidation", "Invalid LinkId or Subscriber Token.");
1313
public static readonly Error InvalidSubscriberId = Error.Validation("LinkValidation", "Invalid SubscriberId.");
14-
public static readonly Error InvalidBackHalf = Error.Validation("LinkValidation", "This back-half text is used before.");
14+
public static readonly Error BackHalfIsExist = Error.Failure("LinkValidation", "This back-half text is used before.");
1515
public static readonly Error GroupNameMustBeSent = Error.Validation("GroupName Validation", "GroupName field can not be null.");
16+
public static readonly Error InvalidBackHalfLength = Error.Validation("LinkValidation", "maximum length for back-half text is 30.");
17+
public static readonly Error InvalidBackHalf = Error.Validation("LinkValidation", "maximum length for back-half text is 30.");
18+
1619
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SwiftLink.Application.Common.Interfaces;
2+
3+
namespace SwiftLink.Application.UseCases.Links.Queries;
4+
public class InquiryBackHalfHanlder(IApplicationDbContext dbContext) : IRequestHandler<InquiryBackHalfQuery, Result<bool>>
5+
{
6+
private readonly IApplicationDbContext _dbContext = dbContext;
7+
8+
public async Task<Result<bool>> Handle(InquiryBackHalfQuery request, CancellationToken cancellationToken)
9+
{
10+
var result = await _dbContext.Set<Link>()
11+
.Where(x => x.ShortCode == request.BackHalfText)
12+
.AsNoTracking()
13+
.AnyAsync(cancellationToken);
14+
15+
return result ? Result.Failure<bool>(LinkMessages.BackHalfIsExist) : Result.Success(true);
16+
}
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace SwiftLink.Application.UseCases.Links.Queries;
2+
3+
public record InquiryBackHalfQuery(string BackHalfText) : IRequest<Result<bool>>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using FluentValidation;
2+
3+
namespace SwiftLink.Application.UseCases.Links.Queries;
4+
public class InquiryBackHalfValidator : AbstractValidator<InquiryBackHalfQuery>
5+
{
6+
public InquiryBackHalfValidator()
7+
{
8+
RuleFor(x => x.BackHalfText)
9+
.NotNull().WithMessage(LinkMessages.InvalidBackHalf.Message)
10+
.MaximumLength(30).WithMessage(LinkMessages.InvalidBackHalfLength.Message);
11+
}
12+
}

src/SwiftLink.Infrastructure/Persistence/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
internal static class ConstantMessages
44
{
5-
public static readonly Error SaveChangesFailed = Error.Failure("SaveChangesFailed", "Database operation failed :(");
5+
public static readonly Error SaveChangesFailed = Error.Exception("SaveChangesFailed", "Database operation failed :(");
66
}

src/SwiftLink.Presentation/Constants/ConstantMessages.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/SwiftLink.Presentation/Controllers/BaseController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public abstract class BaseController(ISender sender) : Controller
1212
protected readonly ISender MediatR = sender;
1313

1414
protected IActionResult OK<T>(Result<T> response)
15-
=> response.IsFailure ? Ok(response.MapToProblemDetails()) : (IActionResult)Ok(response);
15+
=> response.IsSuccess || response.Error.Type == ErrorType.None ? Ok(response) : (IActionResult)Ok(response.MapToProblemDetails());
1616
}

src/SwiftLink.Presentation/Controllers/V1/LinkController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public async Task<IActionResult> Disable([FromRoute] int id,
5454
public async Task<IActionResult> GetByGroupName([FromQuery] GetLinkByGroupNameQuery query,
5555
CancellationToken cancellationToken = default)
5656
=> OK(await MediatR.Send(query, cancellationToken));
57+
58+
[HttpGet]
59+
public async Task<IActionResult> Inquery([FromQuery] InquiryBackHalfQuery query,
60+
CancellationToken cancellationToken = default)
61+
=> OK(await MediatR.Send(query, cancellationToken));
5762
}

0 commit comments

Comments
 (0)