Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #537

Merged
merged 18 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Streetcode/Streetcode.BLL/DTO/Streetcode/Catalog/CatalogItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Streetcode.BLL.DTO.Streetcode.CatalogItem;

public class CatalogItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string? Alias { get; set; }
public int ImageId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Streetcode.BLL.DTO.Streetcode.TextContent.Text
{
public class TextPreviewDTO
{
public string TextContent { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using AutoMapper;
using Streetcode.BLL.DTO.Streetcode.CatalogItem;
using Streetcode.DAL.Entities.Streetcode.Types;
using Streetcode.DAL.Entities.Streetcode;
using Streetcode.BLL.DTO.Streetcode.RelatedFigure;
using Streetcode.DAL.Enums;

namespace Streetcode.BLL.Mapping.Streetcode.Catalog;

public class CatalogItemProfile : Profile
{
public CatalogItemProfile()
{
CreateMap<EventStreetcode, CatalogItem>()
.IncludeBase<StreetcodeContent, CatalogItem>();

CreateMap<PersonStreetcode, CatalogItem>()
.IncludeBase<StreetcodeContent, CatalogItem>();

CreateMap<StreetcodeContent, CatalogItem>()
.ForPath(dto => dto.Url, conf => conf
.MapFrom(e => e.TransliterationUrl))
.ForPath(dto => dto.ImageId, conf => conf
.MapFrom(e => e.Images.Select(i => i.Id).FirstOrDefault()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public async Task<Result<IEnumerable<StreetcodeTagDTO>>> Handle(GetTagByStreetco
return Result.Fail(new Error(errorMsg));
}

return Result.Ok(_mapper.Map<IEnumerable<StreetcodeTagDTO>>(tagIndexed.OrderBy(ti => ti.Index)));
return Result.Ok(_mapper.Map<IEnumerable<StreetcodeTagDTO>>(tagIndexed.OrderBy(ti => ti.TagId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public async Task<Result<Unit>> Handle(DeleteStreetcodeCommand request, Cancella
{
var streetcode = await _repositoryWrapper.StreetcodeRepository
.GetFirstOrDefaultAsync(
predicate: s => s.Id == request.Id,
include: s => s.Include(x => x.Observers)
.Include(x => x.Targets));
predicate: s => s.Id == request.Id);

if (streetcode is null)
{
Expand All @@ -32,6 +30,10 @@ public async Task<Result<Unit>> Handle(DeleteStreetcodeCommand request, Cancella
return Result.Fail(new Error(errorMsg));
}

var relatedFigures = await _repositoryWrapper.RelatedFigureRepository
.GetAllAsync(rf => rf.ObserverId == streetcode.Id || rf.TargetId == streetcode.Id);

_repositoryWrapper.RelatedFigureRepository.DeleteRange(relatedFigures);
_repositoryWrapper.StreetcodeRepository.Delete(streetcode);

var resultIsSuccess = await _repositoryWrapper.SaveChangesAsync() > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using FluentResults;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Streetcode.BLL.DTO.Streetcode.CatalogItem;
using Streetcode.BLL.DTO.Streetcode.RelatedFigure;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Entities.Media.Images;
using Streetcode.DAL.Enums;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Streetcode.Streetcode.GetAllCatalog
{
public class GetAllStreetcodesCatalogHandler : IRequestHandler<GetAllStreetcodesCatalogQuery,
Result<IEnumerable<RelatedFigureDTO>>>
Result<IEnumerable<CatalogItem>>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repositoryWrapper;
Expand All @@ -22,16 +25,22 @@ public GetAllStreetcodesCatalogHandler(IRepositoryWrapper repositoryWrapper, IMa
_logger = logger;
}

public async Task<Result<IEnumerable<RelatedFigureDTO>>> Handle(GetAllStreetcodesCatalogQuery request, CancellationToken cancellationToken)
public async Task<Result<IEnumerable<CatalogItem>>> Handle(GetAllStreetcodesCatalogQuery request, CancellationToken cancellationToken)
{
var streetcodes = await _repositoryWrapper.StreetcodeRepository.GetAllAsync(
predicate: sc => sc.Status == DAL.Enums.StreetcodeStatus.Published,
include: src => src.Include(item => item.Tags).Include(item => item.Images));
include: src => src.Include(item => item.Images).ThenInclude(x => x.ImageDetails));

if (streetcodes != null)
{
const int keyNumOfImageToDisplay = (int)ImageAssigment.Blackandwhite;
foreach (var streetcode in streetcodes)
{
streetcode.Images = streetcode.Images.Where(x => x.ImageDetails.Alt.Equals(keyNumOfImageToDisplay.ToString())).ToList();
}

var skipped = streetcodes.Skip((request.page - 1) * request.count).Take(request.count);
return Result.Ok(_mapper.Map<IEnumerable<RelatedFigureDTO>>(skipped));
return Result.Ok(_mapper.Map<IEnumerable<CatalogItem>>(skipped));
}

const string errorMsg = $"Cannot find any subtitles";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Streetcode.CatalogItem;
using Streetcode.BLL.DTO.Streetcode.RelatedFigure;

namespace Streetcode.BLL.MediatR.Streetcode.Streetcode.GetAllCatalog
{
public record GetAllStreetcodesCatalogQuery(int page, int count) : IRequest<Result<IEnumerable<RelatedFigureDTO>>>;
public record GetAllStreetcodesCatalogQuery(int page, int count) : IRequest<Result<IEnumerable<CatalogItem>>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public GetTextByStreetcodeIdHandler(IRepositoryWrapper repositoryWrapper, IMappe
NullResult<TextDTO?> result = new NullResult<TextDTO?>();
if (text != null)
{
text.TextContent = await _textService.AddTermsTag(text?.TextContent ?? "");
result.WithValue(_mapper.Map<TextDTO?>(text));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace Streetcode.BLL.MediatR.Streetcode.Text.GetParsed
{
public class GetParsedTextAdminPreviewHandler : IRequestHandler<GetParsedTextForAdminPreviewCommand, Result<string>>
public class UpdateParsedTextAdminPreviewHandler : IRequestHandler<UpdateParsedTextForAdminPreviewCommand, Result<string>>
{
private readonly ITextService _textService;

public GetParsedTextAdminPreviewHandler(ITextService textService)
public UpdateParsedTextAdminPreviewHandler(ITextService textService)
{
_textService = textService;
}

public async Task<Result<string>> Handle(GetParsedTextForAdminPreviewCommand request, CancellationToken cancellationToken)
public async Task<Result<string>> Handle(UpdateParsedTextForAdminPreviewCommand request, CancellationToken cancellationToken)
{
string? parsedText = await _textService.AddTermsTag(request.textToParse);
string? parsedText = await _textService.AddTermsTag(request.TextToParse);
return parsedText == null ? Result.Fail(new Error("text was not parsed successfully")) : Result.Ok(parsedText);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Streetcode.BLL.MediatR.Streetcode.Text.GetParsed
{
public record GetParsedTextForAdminPreviewCommand(string textToParse) : IRequest<Result<string>>
public record UpdateParsedTextForAdminPreviewCommand(string TextToParse) : IRequest<Result<string>>
{
}
}
23 changes: 15 additions & 8 deletions Streetcode/Streetcode.BLL/Services/Text/AddTermsToTextService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,28 @@ public async Task<string> AddTermsTag(string text)
var splittedText = Pattern.Split(text)
.Where(x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x)).ToArray();

if (splittedText[0].Contains("<p"))
{
var split = splittedText[0].Replace("<p", "<span");
splittedText[0] = split;
}

foreach (var word in splittedText)
{
var manipulationWord = word;
if (word.Contains("<p"))
{
var split = word.Replace("<p>", "<p><span>");
manipulationWord = split;
}

if (word.Contains("</p"))
{
var split = word.Replace("</p>", "</span></p>");
manipulationWord = split;
}

if (word.Contains('<'))
{
_text.Append(word);
_text.Append(manipulationWord);
continue;
}

var (resultedWord, extras) = CleanWord(word);
var (resultedWord, extras) = CleanWord(manipulationWord);

var term = await _repositoryWrapper.TermRepository
.GetFirstOrDefaultAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentResults;
using Microsoft.AspNetCore.Mvc;
using Streetcode.BLL.DTO.Streetcode.TextContent;
using Streetcode.BLL.DTO.Streetcode.TextContent.Text;
using Streetcode.BLL.MediatR.Streetcode.Text.GetAll;
using Streetcode.BLL.MediatR.Streetcode.Text.GetById;
using Streetcode.BLL.MediatR.Streetcode.Text.GetByStreetcodeId;
Expand Down Expand Up @@ -28,9 +29,9 @@ public async Task<IActionResult> GetByStreetcodeId([FromRoute] int streetcodeId)
return HandleResult(await Mediator.Send(new GetTextByStreetcodeIdQuery(streetcodeId)));
}

[HttpGet]
public async Task<IActionResult> GetParsedText([FromQuery] string text)
[HttpPost]
public async Task<IActionResult> UpdateParsedText([FromBody] TextPreviewDTO text)
{
return HandleResult(await Mediator.Send(new GetParsedTextForAdminPreviewCommand(text)));
return HandleResult(await Mediator.Send(new UpdateParsedTextForAdminPreviewCommand(text.TextContent)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,90 @@
namespace Streetcode.XUnitTest.MediatRTests.StreetCode.Streetcode
{
public class DeleteStreetcodeHandlerTests
{
{
private readonly Mock<IRepositoryWrapper> _repository;
private readonly Mock<ILoggerService> _mockLogger;

public DeleteStreetcodeHandlerTests()
{
_repository = new Mock<IRepositoryWrapper>();
_mockLogger = new Mock<ILoggerService>();
this._repository = new Mock<IRepositoryWrapper>();
this._mockLogger = new Mock<ILoggerService>();
}

[Theory]
[InlineData(1)]
public async Task Handle_ReturnsSuccess(int id)
{
{
// arrange
var testStreetcode = new StreetcodeContent();
var relatedFigure = new RelatedFigure();
int testSaveChangesSuccess = 1;

RepositorySetup(testStreetcode, testSaveChangesSuccess);
this.RepositorySetup(testStreetcode, relatedFigure, testSaveChangesSuccess);

var handler = new DeleteStreetcodeHandler(this._repository.Object, this._mockLogger.Object);

var handler = new DeleteStreetcodeHandler(_repository.Object, _mockLogger.Object);
// act
var result = await handler.Handle(new DeleteStreetcodeCommand(id), CancellationToken.None);

// assert
Assert.True(result.IsSuccess);
}

[Theory]
[InlineData(1)]
public async Task Handle_ReturnsNullError(int id)
{
{
// arrange
string expectedErrorMessage = $"Cannot find a streetcode with corresponding categoryId: {id}";
int testSaveChangesSuccess = 1;
var relatedFigure = new RelatedFigure();

RepositorySetup(null, testSaveChangesSuccess);
this.RepositorySetup(null, relatedFigure, testSaveChangesSuccess);

var handler = new DeleteStreetcodeHandler(this._repository.Object, this._mockLogger.Object);

var handler = new DeleteStreetcodeHandler(_repository.Object, _mockLogger.Object);
// act
var result = await handler.Handle(new DeleteStreetcodeCommand(id), CancellationToken.None);

// assert
Assert.Equal(expectedErrorMessage, result.Errors.Single().Message);
}

[Theory]
[InlineData(1)]
public async Task Handle_ReturnsSaveAsyncError(int id)
{
{
// arrange
var testStreetcode = new StreetcodeContent();
string expectedErrorMessage = "Failed to delete a streetcode";
int testSaveChangesFailed = -1;
var relatedFigure = new RelatedFigure();

RepositorySetup(testStreetcode, testSaveChangesFailed);
this.RepositorySetup(testStreetcode, relatedFigure, testSaveChangesFailed);

var handler = new DeleteStreetcodeHandler(this._repository.Object, this._mockLogger.Object);

var handler = new DeleteStreetcodeHandler(_repository.Object, _mockLogger.Object);
// act
var result = await handler.Handle(new DeleteStreetcodeCommand(id), CancellationToken.None);

// assert
Assert.Equal(expectedErrorMessage, result.Errors.Single().Message);
}

private void RepositorySetup(StreetcodeContent streetcodeContent, int saveChangesVariable)
private void RepositorySetup(StreetcodeContent streetcodeContent, RelatedFigure relatedFigure, int saveChangesVariable)
{
_repository.Setup(x => x.StreetcodeRepository.Delete(streetcodeContent));
_repository.Setup(x => x.SaveChangesAsync()).ReturnsAsync(saveChangesVariable);
_repository.Setup(x => x.StreetcodeRepository.GetFirstOrDefaultAsync(
this._repository.Setup(x => x.StreetcodeRepository.Delete(streetcodeContent));
this._repository.Setup(x => x.SaveChangesAsync()).ReturnsAsync(saveChangesVariable);
this._repository.Setup(x => x.StreetcodeRepository.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<StreetcodeContent, bool>>>(),
It.IsAny<Func<IQueryable<StreetcodeContent>,
IIncludableQueryable<StreetcodeContent, object>>>()))
.ReturnsAsync(streetcodeContent);
It.IsAny<Func<IQueryable<StreetcodeContent>, IIncludableQueryable<StreetcodeContent, object>>>())).ReturnsAsync(streetcodeContent);

this._repository.Setup(x => x.RelatedFigureRepository.Delete(relatedFigure));
this._repository.Setup(x => x.SaveChangesAsync()).ReturnsAsync(saveChangesVariable);
this._repository.Setup(x => x.RelatedFigureRepository.GetFirstOrDefaultAsync(
It.IsAny<Expression<Func<RelatedFigure, bool>>>(),
It.IsAny<Func<IQueryable<RelatedFigure>, IIncludableQueryable<RelatedFigure, object>>>())).ReturnsAsync(relatedFigure);
}
}
}
Loading