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

Add class resolver for timetable #20

Merged
merged 2 commits into from
Jan 29, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ namespace Vulder.Timetable.Api.Controllers.Timetable;

[ApiController]
[Route("/timetable/[controller]")]
public class GetTimetableController : ControllerBase
public class TimetableController : ControllerBase
{
private readonly IMediator _mediator;

public GetTimetableController(IMediator mediator)
public TimetableController(IMediator mediator)
{
_mediator = mediator;
}

[HttpGet]
public async Task<IActionResult> GetTimetable([FromQuery] Guid schoolId, [FromQuery] string className,
[FromQuery] string shortPath)
public async Task<IActionResult> GetTimetable([FromQuery] Guid schoolId, [FromQuery] string shortPath)
{
var timetable = await _mediator.Send(new GetTimetableRequestModel
var className = await _mediator.Send(new ResolveClassRequestModel
{
SchoolId = schoolId,
ClassName = className,
Path = shortPath
});

var timetable = await _mediator.Send(new TimetableRequestModel
{
SchoolId = schoolId,
Class = className,
ShortPath = shortPath
});

Expand Down
2 changes: 1 addition & 1 deletion src/Vulder.Timetable.Application/ApplicationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ApplicationModule : Module
public ApplicationModule()
{
_assemblies.Add(Assembly.GetAssembly(typeof(GetBranchesRequestHandler)));
_assemblies.Add(Assembly.GetAssembly(typeof(GetTimetableRequestHandler)));
_assemblies.Add(Assembly.GetAssembly(typeof(TimetableRequestHandler)));
}

protected override void Load(ContainerBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediatR;
using Optivulcan;
using Vulder.Timetable.Application.Cache;
using Vulder.Timetable.Core.Models;
using Vulder.Timetable.Core.ProjectAggregate.Branch;
using Vulder.Timetable.Infrastructure.Api;
Expand All @@ -23,14 +24,8 @@ public GetBranchesRequestHandler(IBranchRepository branchRepository)
if (branchesFromCache != null && branchesFromCache.ExpiredAt < DateTimeOffset.Now)
return branchesFromCache.Branches;

var schoolModel = await SchoolApi.GetSchoolModel(request.SchoolId);
var newSchoolBranches = await OptivulcanApi.GetBranches(schoolModel.TimetableUrl!);
var branchCache = new BranchCache
{
Branches = newSchoolBranches
}.CreateTimestamp();
await _branchRepository.Create(request.SchoolId, branchCache);
var newBranches = await CacheBranch.Create(_branchRepository, request.SchoolId);

return newSchoolBranches;
return newBranches.Branches;
}
}
26 changes: 26 additions & 0 deletions src/Vulder.Timetable.Application/Cache/CacheBranch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Optivulcan;
using Vulder.Timetable.Core.ProjectAggregate.Branch;
using Vulder.Timetable.Infrastructure.Api;
using Vulder.Timetable.Infrastructure.Redis.Interfaces;

namespace Vulder.Timetable.Application.Cache;

public class CacheBranch
{
public static async Task<BranchCache> Create(IBranchRepository branchRepository, Guid schoolId)
{
var schoolModel = await SchoolApi.GetSchoolModel(schoolId);
var newSchoolBranches = await OptivulcanApi.GetBranches(schoolModel.TimetableUrl!);

var branchCache = new BranchCache
{
Branches = newSchoolBranches
}.CreateTimestamp();

if (branchCache.Branches == null) throw new Exception("Branches");

await branchRepository.Create(schoolId, branchCache);

return branchCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using MediatR;
using Vulder.Timetable.Application.Cache;
using Vulder.Timetable.Core.Models;
using Vulder.Timetable.Infrastructure.Redis.Interfaces;

namespace Vulder.Timetable.Application.Classname;

public class ResolveClassRequestHandler : IRequestHandler<ResolveClassRequestModel, string>
{
private readonly IBranchRepository _branchRepository;

public ResolveClassRequestHandler(IBranchRepository branchRepository)
{
_branchRepository = branchRepository;
}

public async Task<string> Handle(ResolveClassRequestModel request, CancellationToken cancellationToken)
{
if (request.Path == null)
throw new Exception("Path param is null");

var branchesFromCache = await _branchRepository.GetBranchById(request.SchoolId);
if (branchesFromCache != null && branchesFromCache.ExpiredAt < DateTimeOffset.Now)
return GetClassFromCollection(branchesFromCache.Branches!, request.Path);

var branches = await CacheBranch.Create(_branchRepository, request.SchoolId);

return GetClassFromCollection(branches.Branches!, request.Path);
}

private static string GetClassFromCollection(IEnumerable<Optivulcan.Pocos.Branch> branches, string path)
{
var branch = branches.FirstOrDefault(x => x.Url == path)?.Name;
if (branch == null)
throw new Exception("Couldn't resolve class");

return branch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

namespace Vulder.Timetable.Application.Timetable.GetTimetable;

public class GetTimetableRequestHandler : IRequestHandler<GetTimetableRequestModel, Optivulcan.Pocos.Timetable>
public class TimetableRequestHandler : IRequestHandler<TimetableRequestModel, Optivulcan.Pocos.Timetable>
{
private readonly ITimetableRepository _timetableRepository;

public GetTimetableRequestHandler(ITimetableRepository timetableRepository)
public TimetableRequestHandler(ITimetableRepository timetableRepository)
{
_timetableRepository = timetableRepository;
}

public async Task<Optivulcan.Pocos.Timetable> Handle(GetTimetableRequestModel request,
public async Task<Optivulcan.Pocos.Timetable> Handle(TimetableRequestModel request,
CancellationToken cancellationToken)
{
var timetableFromCache = await _timetableRepository.GetTimetableById(request.SchoolId, request.ClassName);
var timetableFromCache = await _timetableRepository.GetTimetableById(request.SchoolId, request.Class);
if (timetableFromCache?.Timetable != null && timetableFromCache.ExpiredAt < DateTimeOffset.Now)
return timetableFromCache.Timetable;

Expand All @@ -31,7 +31,7 @@ public GetTimetableRequestHandler(ITimetableRepository timetableRepository)
Timetable = newTimetable
}.CreateTimestamp();

await _timetableRepository.Create(request.SchoolId, request.ClassName, timetableCache);
await _timetableRepository.Create(request.SchoolId, request.Class, timetableCache);

return newTimetable;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Vulder.Timetable.Core/Models/ResolveClassRequestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MediatR;

namespace Vulder.Timetable.Core.Models;

public class ResolveClassRequestModel : IRequest<string>
{
public Guid SchoolId { get; set; }
public string? Path { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Vulder.Timetable.Core.Models;

public class GetTimetableRequestModel : IRequest<Optivulcan.Pocos.Timetable>
public class TimetableRequestModel : IRequest<Optivulcan.Pocos.Timetable>
{
public Guid? SchoolId { get; set; }
public string? ClassName { get; set; }
public string? Class { get; set; }
public string? ShortPath { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async void GetTimetable_GET_200_StatusCode()
using var client = application.CreateClient();
using var getBranchesResponse =
await client.GetAsync(
$"timetable/GetTimetable?schoolId={_schoolTestModel.Id}&className={ClassName}&shortPath={ShortUrl}");
$"timetable/Timetable?schoolId={_schoolTestModel.Id}&shortPath={ShortUrl}");

Assert.Equal(HttpStatusCode.OK, getBranchesResponse.StatusCode);
}
Expand Down