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

Feature/issue 833 timeline context #1394

Merged
merged 34 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
79291a4
add: update endpoint for historicalContext
BapBap7 Mar 31, 2024
deb1821
add: getById endpoint for historicalContext
BapBap7 Mar 31, 2024
222e2d7
add: delete endpoint for historicalContext, todo: create
BapBap7 Apr 1, 2024
158c237
add: create for historicaContext + validation for unique context.
BapBap7 Apr 1, 2024
6fe64ec
Update Delete tag
VladyslavPavlysko Apr 14, 2024
9b838d7
sort tags
VladyslavPavlysko Apr 17, 2024
675dd6b
Merge branch 'develop' of https://github.com/ita-social-projects/Stre…
VladyslavPavlysko Apr 17, 2024
561abe6
change sort order
VladyslavPavlysko Apr 17, 2024
864cf15
Merge branch 'develop' of https://github.com/ita-social-projects/Stre…
VladyslavPavlysko Apr 22, 2024
c3042f3
add: validation for unique update
BapBap7 Apr 22, 2024
8cf70f1
add: sorting
BapBap7 Apr 22, 2024
871ae2d
Handler changes
VladyslavPavlysko Apr 23, 2024
bb0b55f
Integration tests for tags
VladyslavPavlysko Apr 23, 2024
f24d0f4
Unit tests for tags
VladyslavPavlysko Apr 23, 2024
de76ab7
add: unit test create, getById for historicalContext
BapBap7 Apr 27, 2024
2bb6bae
add: merged with release branch
BapBap7 Apr 27, 2024
fa83349
fix: getAllHistoricalContextTest.cs
BapBap7 Apr 27, 2024
c7bced2
add: test for delete
BapBap7 Apr 28, 2024
8aef160
add: update unit test
BapBap7 May 1, 2024
468732c
integrational tests, 2 not working
BapBap7 May 1, 2024
ab85693
add: getByTitle, unit test. fix: integrationalTests
BapBap7 May 1, 2024
5373fd7
returned appsettings string
BapBap7 May 1, 2024
653b1f2
Merge branch 'release/1.0.0' into tag-delete-update-requests
Lazy-Lenny May 1, 2024
5ff855f
add: nonsense change that might help
BapBap7 May 1, 2024
a7bd728
Merge branch 'release/1.0.0' into feature/issue-833-timeline-context
BapBap7 May 2, 2024
25f8529
merged with release, should fix problems
BapBap7 May 2, 2024
44db69a
returned connectionString
BapBap7 May 2, 2024
51c2087
Merge branch 'release/1.0.0' of https://github.com/ita-social-project…
VladyslavPavlysko May 8, 2024
e6a5ce8
Merge branch 'tag-delete-update-requests' of https://github.com/ita-s…
VladyslavPavlysko May 8, 2024
4c28b42
default string
VladyslavPavlysko May 8, 2024
3f343d5
Merge branch 'release/1.0.0' of https://github.com/ita-social-project…
VladyslavPavlysko May 8, 2024
bd9116c
fix tag tests
VladyslavPavlysko May 8, 2024
fb7ae54
Merge branch 'release/1.0.0' into feature/issue-833-timeline-context
vasylashka May 12, 2024
3be0093
removed unnecessary coma that caused problems with launching local pr…
BapBap7 May 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Streetcode.BLL.DTO.AdditionalContent.Tag
{
public class UpdateTagDTO
{
public int Id { get; set; }

public string Title { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TagProfile : Profile
public TagProfile()
{
CreateMap<Tag, TagDTO>().ForMember(x => x.Streetcodes, conf => conf.Ignore());
CreateMap<Tag, UpdateTagDTO>().ReverseMap();
CreateMap<Tag, StreetcodeTagDTO>().ReverseMap();
CreateMap<StreetcodeTagIndex, StreetcodeTagDTO>()
.ForMember(x => x.Id, conf => conf.MapFrom(ti => ti.TagId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public CreateTagHandler(IRepositoryWrapper repositoryWrapper, IMapper mapper, IL

public async Task<Result<TagDTO>> Handle(CreateTagQuery request, CancellationToken cancellationToken)
{
var exists = await _repositoryWrapper.TagRepository.GetFirstOrDefaultAsync(t => request.tag.Title == t.Title);

if (exists is not null)
{
var errMessage = $"Tag with title {request.tag.Title} already exists";
_logger.LogError(request, errMessage);
return Result.Fail(errMessage);
}

var newTag = await _repositoryWrapper.TagRepository.CreateAsync(new DAL.Entities.AdditionalContent.Tag()
{
Title = request.tag.Title
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FluentResults;
using MediatR;

namespace Streetcode.BLL.MediatR.AdditionalContent.Tag.Delete
{
public record DeleteTagCommand(int id)
: IRequest<Result<int>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.AdditionalContent.Tag.Delete
{
public class DeleteTagHandler : IRequestHandler<DeleteTagCommand, Result<int>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public DeleteTagHandler(IRepositoryWrapper repository, ILoggerService logger)
{
_repositoryWrapper = repository;
_logger = logger;
}

public async Task<Result<int>> Handle(DeleteTagCommand request, CancellationToken cancellationToken)
{
var tagToDelete =
await _repositoryWrapper.TagRepository.GetFirstOrDefaultAsync(x => x.Id == request.id);
if (tagToDelete is null)
{
string exMessage = $"No tag found by entered Id - {request.id}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
_repositoryWrapper.TagRepository.Delete(tagToDelete);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(request.id);
}
catch (Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public GetAllTagsHandler(IRepositoryWrapper repositoryWrapper, IMapper mapper, I
public async Task<Result<IEnumerable<TagDTO>>> Handle(GetAllTagsQuery request, CancellationToken cancellationToken)
{
var tags = await _repositoryWrapper.TagRepository.GetAllAsync();

if (tags is null)
{
string errorMsg = _stringLocalizerCannotFind?["CannotFindAnyTags"].Value;
_logger.LogError(request, errorMsg);
return Result.Fail(new Error(errorMsg));
}

return Result.Ok(_mapper.Map<IEnumerable<TagDTO>>(tags));
var sorted_tags = tags.OrderBy(tag => tag.Title);
return Result.Ok(_mapper.Map<IEnumerable<TagDTO>>(sorted_tags));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.AdditionalContent;
using Streetcode.BLL.DTO.AdditionalContent.Tag;

namespace Streetcode.BLL.MediatR.AdditionalContent.Tag.Update
{
public record UpdateTagCommand(UpdateTagDTO tag)
: IRequest<Result<TagDTO>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.AdditionalContent;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;
using TagEntity = Streetcode.DAL.Entities.AdditionalContent.Tag;

namespace Streetcode.BLL.MediatR.AdditionalContent.Tag.Update
{
public class UpdateTagHandler : IRequestHandler<UpdateTagCommand, Result<TagDTO>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly IMapper _mapper;
private readonly ILoggerService _logger;
public UpdateTagHandler(IRepositoryWrapper repository, IMapper mapper, ILoggerService logger)
{
_repositoryWrapper = repository;
_mapper = mapper;
_logger = logger;
}

public async Task<Result<TagDTO>> Handle(UpdateTagCommand request, CancellationToken cancellationToken)
{
var existedTag =
await _repositoryWrapper.TagRepository.GetFirstOrDefaultAsync(x => x.Id == request.tag.Id);
if (existedTag is null)
{
string exMessage = $"No tag found by entered Id - {request.tag.Id}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

existedTag = await _repositoryWrapper.TagRepository.GetFirstOrDefaultAsync(x => x.Title == request.tag.Title);

if (existedTag is not null)
{
var errMessage = $"Tag with title {request.tag.Title} already exists";
_logger.LogError(request, errMessage);
return Result.Fail(errMessage);
}

try
{
var tagToUpdate = _mapper.Map<TagEntity>(request.tag);
_repositoryWrapper.TagRepository.Update(tagToUpdate);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(_mapper.Map<TagDTO>(tagToUpdate));
}
catch(Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Timeline;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.Create
{
public record CreateHistoricalContextCommand(HistoricalContextDTO context)
: IRequest<Result<HistoricalContextDTO>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Timeline;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.Create
{
public class CreateHistoricalContextHandler : IRequestHandler<CreateHistoricalContextCommand, Result<HistoricalContextDTO>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public CreateHistoricalContextHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, ILoggerService logger)
{
_mapper = mapper;
_repositoryWrapper = repositoryWrapper;
_logger = logger;
}

public async Task<Result<HistoricalContextDTO>> Handle(CreateHistoricalContextCommand request, CancellationToken cancellationToken)
{
try
{
/* read comment at Streetcode.BLL.MediatR.Timeline.HistoricalContext.Update line 29 */
var context = _mapper.Map<DAL.Entities.Timeline.HistoricalContext>(request.context);
var checkIfContextExists = await _repositoryWrapper.HistoricalContextRepository.GetFirstOrDefaultAsync(j => j.Title == request.context.Title);

if (checkIfContextExists is not null)
{
string exceptionMessege = $"Context with title '{request.context.Title}' is already exists.";
_logger.LogError(request, exceptionMessege);
return Result.Fail(exceptionMessege);
}

var createdContext = await _repositoryWrapper.HistoricalContextRepository.CreateAsync(context);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(_mapper.Map<HistoricalContextDTO>(createdContext));
}
catch (Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FluentResults;
using MediatR;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.Delete
{
public record DeleteHistoricalContextCommand(int contextId)
: IRequest<Result<int>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.Delete
{
public class DeleteHistoricalContextHandler : IRequestHandler<DeleteHistoricalContextCommand, Result<int>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public DeleteHistoricalContextHandler(IRepositoryWrapper repository, ILoggerService logger)
{
_repositoryWrapper = repository;
_logger = logger;
}

public async Task<Result<int>> Handle(DeleteHistoricalContextCommand request, CancellationToken cancellationToken)
{
var contextToDelete =
await _repositoryWrapper.HistoricalContextRepository.GetFirstOrDefaultAsync(x => x.Id == request.contextId);
if (contextToDelete is null)
{
string exMessage = $"No context found by entered Id - {request.contextId}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
_repositoryWrapper.HistoricalContextRepository.Delete(contextToDelete);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(request.contextId);
}
catch(Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.AdditionalContent.Subtitles;
using Microsoft.Extensions.Localization;
using Streetcode.BLL.DTO.Timeline;
using Streetcode.BLL.Interfaces.Logging;
Expand Down Expand Up @@ -38,7 +37,9 @@ public async Task<Result<IEnumerable<HistoricalContextDTO>>> Handle(GetAllHistor
return Result.Fail(new Error(errorMsg));
}

return Result.Ok(_mapper.Map<IEnumerable<HistoricalContextDTO>>(historicalContextItems));
var historicalContexts = historicalContextItems.OrderBy(context => context.Title);

return Result.Ok(_mapper.Map<IEnumerable<HistoricalContextDTO>>(historicalContexts));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Timeline;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.GetById
{
public class GetHistoricalContextByIdHandler : IRequestHandler<GetHistoricalContextByIdQuery, Result<HistoricalContextDTO>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repository;
private readonly ILoggerService _loggerService;

public GetHistoricalContextByIdHandler(IMapper mapper, IRepositoryWrapper repository, ILoggerService loggerService)
{
_mapper = mapper;
_repository = repository;
_loggerService = loggerService;
}

public async Task<Result<HistoricalContextDTO>> Handle(GetHistoricalContextByIdQuery request, CancellationToken cancellationToken)
{
var context = await _repository.HistoricalContextRepository.GetFirstOrDefaultAsync(j => j.Id == request.contextId);

if (context is null)
{
string exceptionMessege = $"No context found by entered Id - {request.contextId}";
_loggerService.LogError(request, exceptionMessege);
return Result.Fail(exceptionMessege);
}

try
{
var contextDto = _mapper.Map<HistoricalContextDTO>(context);
return Result.Ok(contextDto);
}
catch (Exception ex)
{
_loggerService.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Timeline;

namespace Streetcode.BLL.MediatR.Timeline.HistoricalContext.GetById;

public record GetHistoricalContextByIdQuery(int contextId)
: IRequest<Result<HistoricalContextDTO>>;
Loading
Loading