Skip to content

Commit

Permalink
Merge pull request #5 from kova1ev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kova1ev authored May 19, 2023
2 parents 66df29d + 6115f42 commit b0a98da
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/UrlShortener.Api/Controllers/LinkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public async Task<ActionResult> GetById([FromRoute] Guid id)
public async Task<ActionResult> GetLinks()
{
// TODO: PAGINATIONS!
var links = await Mediator.Send(new GetLinksQuery());
return Ok(links);
Result<IEnumerable<LinkDetailsResponse>> result = await Mediator.Send(new GetLinksQuery());
return Ok(result.Value);
}

// COMMANDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public static class LinkValidationErrorMessage
{
public const string ALIAS_TAKEN = "Alias is taken.";
public const string ALIAS_HAVE_WHITESPACE = "Alias must not contain spaces.";
public const string LINK_NOT_EXISTING = "Link is not existing.";
public const string URL_ADDRESS_REQUIRED = "UrlAddress is required.";
public const string URL_ADDRESS_IS_NOT_URL = "UrlAddress is not url.";
Expand Down
2 changes: 2 additions & 0 deletions src/UrlShortener.Application/Interfaces/ILinkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public interface ILinkService
Task<bool> AliasIsBusy(string alias);
Task<string> GenerateAlias();
string CreateShortUrl(string alias);

string RemoveWhiteSpacesFromAlisa(string alias);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ public CreateLinkCommandValidator()
.Length(3, 30)
.When(c => c.Alias != null)
.WithMessage(LinkValidationErrorMessage.ALIAS_BAD_RANGE);

RuleFor(c => c.Alias)
.Matches(@"^\S*$")
.When(c => c.Alias != null)
.WithMessage(LinkValidationErrorMessage.ALIAS_HAVE_WHITESPACE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ public UpdateLinkCommandValidator()
.Length(3, 30)
.When(c => c.Alias != null)
.WithMessage(LinkValidationErrorMessage.ALIAS_BAD_RANGE);

RuleFor(c => c.Alias)
.Matches(@"^\S*$")
.When(c => c.Alias != null)
.WithMessage(LinkValidationErrorMessage.ALIAS_HAVE_WHITESPACE);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using MediatR;
using UrlShortener.Application.Common.Models.Links;
using UrlShortener.Application.Common.Result;

namespace UrlShortener.Application.Links.Queries.GetLinks;
public class GetLinksQuery : IRequest<IEnumerable<LinkDetailsResponse>>
public class GetLinksQuery : IRequest<Result<IEnumerable<LinkDetailsResponse>>>
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using UrlShortener.Application.Common.Models.Links;
using UrlShortener.Application.Common.Result;
using UrlShortener.Application.Interfaces;


namespace UrlShortener.Application.Links.Queries.GetLinks;

public class GetLinksQueryHandler : IRequestHandler<GetLinksQuery, IEnumerable<LinkDetailsResponse>>
public class GetLinksQueryHandler : IRequestHandler<GetLinksQuery, Result<IEnumerable<LinkDetailsResponse>>>
{
private readonly IAppDbContext _appDbContext;

Expand All @@ -15,14 +16,15 @@ public GetLinksQueryHandler(IAppDbContext appDbContext)
_appDbContext = appDbContext ?? throw new ArgumentNullException(nameof(appDbContext));
}

public async Task<IEnumerable<LinkDetailsResponse>> Handle(GetLinksQuery request, CancellationToken cancellationToken)
public async Task<Result<IEnumerable<LinkDetailsResponse>>> Handle(GetLinksQuery request, CancellationToken cancellationToken)
{
IEnumerable<LinkDetailsResponse> links = await _appDbContext.Links
.Include(l => l.LinkStatistic)
.ThenInclude(st => st.Geolocation)
.AsNoTracking()
.Select(link => LinkDetailsResponse.MapToLInkDto(link))
.Select(link => LinkDetailsResponse.MapToLInkDto(link)!)
.ToArrayAsync();
return links;

return Result<IEnumerable<LinkDetailsResponse>>.Success(links);
}
}
5 changes: 5 additions & 0 deletions src/UrlShortener.Application/Services/LinkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public string CreateShortUrl(string alias)
throw new ArgumentNullException(nameof(_appOptions.AppUrl));
return string.Concat(_appOptions.AppUrl, RedirectRoute, alias);
}

public string RemoveWhiteSpacesFromAlisa(string alias)
{
return alias.Replace(" ", "");
}
}
9 changes: 5 additions & 4 deletions tests/Application.UnitTests/Links/Queries/GetLinksTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Application.UnitTests.Utility;
using UrlShortener.Application.Common.Models.Links;
using UrlShortener.Application.Common.Result;
using UrlShortener.Application.Links.Queries.GetLinks;
using UrlShortener.Data;

Expand All @@ -17,11 +18,11 @@ public async Task Get_all_links()

//act
GetLinksQuery request = new();
IEnumerable<LinkDetailsResponse> result = await handler.Handle(request, CancellationToken.None);
Result<IEnumerable<LinkDetailsResponse>> result = await handler.Handle(request, CancellationToken.None);

//assert
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Equal(2, result.Count());
Assert.NotNull(result.Value);
Assert.NotEmpty(result.Value);
Assert.Equal(2, result.Value.Count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,27 @@ public void Validate_createLinkCommand_with_bad_Alias(string alias)
Assert.Contains(LinkValidationErrorMessage.ALIAS_BAD_RANGE, errors);
Assert.True(errors.Length == 1);
}

[Theory]
[InlineData("fgg df gf")]
[InlineData("fg gdf")]
public void Validate_CreateLInkCommand_ShouldReturnFailureResult_with_WhiteSpaceError(string alias)
{
//arrange
var url = "https://git.com";
var request = new CreateLinkCommand(url, alias);
var validator = new CreateLinkCommandValidator();

//act
ValidationResult result = validator.Validate(request);

//assert
Assert.False(result.IsValid);
Assert.NotEmpty(result.Errors);

string[] errors = result.Errors.Select(s => s.ErrorMessage).ToArray();
Assert.Contains(LinkValidationErrorMessage.ALIAS_HAVE_WHITESPACE, errors);
Assert.True(errors.Length == 1);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,27 @@ public void Validate_updateLinkCommand_with_bad_Alias(string alias)
Assert.Contains(LinkValidationErrorMessage.ALIAS_BAD_RANGE, errors);
Assert.True(errors.Length == 1);
}

[Theory]
[InlineData("fgg df gf")]
[InlineData("fg gdf")]
public void Validate_UpdateLInkCommand_ShouldReturnFailureResult_with_WhiteSpaceError(string alias)
{
//arrange
var url = "https://git.com";
var request = new UpdateLinkCommand(_linkId, url, alias);
var validator = new UpdateLinkCommandValidator();

//act
ValidationResult result = validator.Validate(request);

//assert
Assert.False(result.IsValid);
Assert.NotEmpty(result.Errors);

string[] errors = result.Errors.Select(s => s.ErrorMessage).ToArray();
Assert.Contains(LinkValidationErrorMessage.ALIAS_HAVE_WHITESPACE, errors);
Assert.True(errors.Length == 1);

}
}

0 comments on commit b0a98da

Please sign in to comment.