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 19 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 @@
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>>;
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.BLL.MediatR.Timeline.HistoricalContext.GetById;
using Streetcode.DAL.Repositories.Interfaces.Base;

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

public class GetHistoricalContextByTitleHandler : IRequestHandler<GetHistoricalContextByTitleQuery, Result<HistoricalContextDTO>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repository;
private readonly ILoggerService _loggerService;

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

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

if (context is null)
{
string exceptionMessege = $"No context found by title - {request.title}";
_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.GetByTitle;

public record GetHistoricalContextByTitleQuery(string title)
: IRequest<Result<HistoricalContextDTO>>;
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.Update
{
public record UpdateHistoricalContextCommand(HistoricalContextDTO HistoricalContext) : IRequest<Result<HistoricalContextDTO>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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.Update
{
public class
UpdateHistoricalContextHandler : IRequestHandler<UpdateHistoricalContextCommand, Result<HistoricalContextDTO>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

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

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

var historicalContextRepeat = await _repositoryWrapper.HistoricalContextRepository.GetFirstOrDefaultAsync(
x =>
x.Title == request.HistoricalContext.Title);

if (historicalContextRepeat is not null)
{
string exMessage = $"There is already a context with title - {request.HistoricalContext.Title}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
/* have some shitty code right here, if you delete the DAL.Entities.Timeline. part it will show error
even if you have using Streetcode.DAL.Entities.Timeline; so sad :( */
var contextToUpdate = _mapper.Map<DAL.Entities.Timeline.HistoricalContext>(request.HistoricalContext);
_repositoryWrapper.HistoricalContextRepository.Update(contextToUpdate);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(_mapper.Map<HistoricalContextDTO>(contextToUpdate));
}
catch (Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Streetcode.BLL.DTO.Timeline;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.Create;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.Delete;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.GetAll;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.GetById;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.GetByTitle;
using Streetcode.BLL.MediatR.Timeline.HistoricalContext.Update;
using Streetcode.DAL.Enums;

namespace Streetcode.WebApi.Controllers.Timeline
{
Expand All @@ -10,5 +18,44 @@ public async Task<IActionResult> GetAll()
{
return HandleResult(await Mediator.Send(new GetAllHistoricalContextQuery()));
}

[HttpGet("{id:int}")]
public async Task<IActionResult> GetById(int id)
{
return HandleResult(await Mediator.Send(new GetHistoricalContextByIdQuery(id)));
}

[HttpGet("{title}")]
public async Task<IActionResult> GetByTitle(string title)
{
return HandleResult(await Mediator.Send(new GetHistoricalContextByTitleQuery(title)));
}

[HttpPut]
[Authorize(Roles = nameof(UserRole.Admin))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> Update([FromBody]HistoricalContextDTO contextDto)
{
return HandleResult(await Mediator.Send(new UpdateHistoricalContextCommand(contextDto)));
}

[HttpDelete("{id:int}")]
[Authorize(Roles = nameof(UserRole.Admin))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> Delete(int id)
{
return HandleResult(await Mediator.Send(new DeleteHistoricalContextCommand(id)));
}

[HttpPost]
[Authorize(Roles = nameof(UserRole.Admin))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> Create([FromBody]HistoricalContextDTO contextDto)
{
return HandleResult(await Mediator.Send(new CreateHistoricalContextCommand(contextDto)));
}
}
}
2 changes: 1 addition & 1 deletion Streetcode/Streetcode.WebApi/appsettings.Local.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"
"DefaultConnection": "Server=PC;Database=Streetcode1;MultipleActiveResultSets=True;TrustServerCertificate=true;Trusted_Connection=true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

U need to change this back to:
"DefaultConnection": "Server=127.0.0.1;Database=StreetcodeDb;User Id=sa;Password=Admin@1234;MultipleActiveResultSets=true"

},
"CORS": {
"AllowedOrigins": [ "http://localhost:3000" ],
Expand Down
Loading
Loading