-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class resolver for timetable (#20)
- Loading branch information
Showing
9 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Vulder.Timetable.Application/Classname/ResolveClassRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters