Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Add generic Canvas pages #95

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Epsilon.Abstractions/Component/CompetenceComponentFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Epsilon.Abstractions.Component;

public abstract class CompetenceComponentFetcher<TComponent> : ICompetenceComponentFetcher<TComponent> where TComponent : ICompetenceComponent
{
public async Task<ICompetenceComponent> FetchUnknown(DateTime startDate, DateTime endDate) => await Fetch(startDate, endDate);
public async Task<ICompetenceComponent> FetchUnknown(string componentName, DateTime startDate, DateTime endDate) => await Fetch(componentName,startDate, endDate);

public abstract Task<TComponent> Fetch(DateTime startDate, DateTime endDate);
public abstract Task<TComponent> Fetch(string componentName, DateTime startDate, DateTime endDate);
}
1 change: 0 additions & 1 deletion Epsilon.Abstractions/Component/CompetenceProfile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Epsilon.Abstractions.Model;
using Epsilon.Canvas.Abstractions.Model;

namespace Epsilon.Abstractions.Component;

Expand Down
4 changes: 2 additions & 2 deletions Epsilon.Abstractions/Component/ICompetenceComponentFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public interface ICompetenceComponentFetcher
{
public Task<ICompetenceComponent> FetchUnknown(DateTime startDate, DateTime endDate);
public Task<ICompetenceComponent> FetchUnknown(string componentName, DateTime startDate, DateTime endDate);
}

public interface ICompetenceComponentFetcher<TComponent> : ICompetenceComponentFetcher
where TComponent : ICompetenceComponent
{
public Task<TComponent> Fetch(DateTime startDate, DateTime endDate);
public Task<TComponent> Fetch(string componentName, DateTime startDate, DateTime endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Epsilon.Abstractions.Component;

public record PersonaPage(string PersonaHtml) : IWordCompetenceComponent
public record Page(string Html) : IWordCompetenceComponent
{
public void AddToWordDocument(MainDocumentPart mainDocumentPart)
{
var personaHtmlBuffer = Encoding.UTF8.GetPreamble().Concat(Encoding.UTF8.GetBytes($"<html>{PersonaHtml}</html>")).ToArray();
using var personaHtmlStream = new MemoryStream(personaHtmlBuffer);
var htmlBuffer = Encoding.UTF8.GetPreamble().Concat(Encoding.UTF8.GetBytes($"<html>{Html}</html>")).ToArray();
using var htmlStream = new MemoryStream(htmlBuffer);

var formatImportPart = mainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);
formatImportPart.FeedData(personaHtmlStream);
formatImportPart.FeedData(htmlStream);

mainDocumentPart.Document.AppendChild(new Body(
new AltChunk
Expand Down
3 changes: 3 additions & 0 deletions Epsilon.Abstractions/Model/StudentSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Epsilon.Abstractions.Model;

public record StudentSettings(Dictionary<string, string> PageMapping);
4 changes: 2 additions & 2 deletions Epsilon.Abstractions/Service/ICompetenceComponentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Epsilon.Abstractions.Service;

public interface ICompetenceComponentService
{
IAsyncEnumerable<ICompetenceComponent> GetComponents(DateTime startDate, DateTime endDate);
IAsyncEnumerable<ICompetenceComponent> GetComponents(string name, DateTime startDate, DateTime endDate);

IAsyncEnumerable<TComponent> GetComponents<TComponent>(DateTime startDate, DateTime endDate) where TComponent : ICompetenceComponent;
IAsyncEnumerable<TComponent> GetComponents<TComponent>(string name, DateTime startDate, DateTime endDate) where TComponent : ICompetenceComponent;

Task<ICompetenceComponent?> GetComponent(string name, DateTime startDate, DateTime endDate);

Expand Down
2 changes: 1 addition & 1 deletion Epsilon.Abstractions/Service/ICompetenceDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace Epsilon.Abstractions.Service;

public interface ICompetenceDocumentService
{
Task<Stream> WriteDocument(Stream stream, DateTime startDate, DateTime endDate);
Task<Stream> WriteDocument(Stream stream, string name, DateTime startDate, DateTime endDate);
}
1 change: 0 additions & 1 deletion Epsilon.Abstractions/Service/IFilterService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Epsilon.Abstractions.Component;
using Epsilon.Canvas.Abstractions.Model;

namespace Epsilon.Abstractions.Service;
Expand Down
4 changes: 2 additions & 2 deletions Epsilon.Host.WebApi/Controllers/DocumentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public DocumentController(ICompetenceDocumentService competenceDocumentService)
}

[HttpGet("word")]
public async Task<IActionResult> DownloadWordDocument(DateTime startDate, DateTime endDate)
public async Task<IActionResult> DownloadWordDocument(string name, DateTime startDate, DateTime endDate)
{
var stream = new MemoryStream();
await _competenceDocumentService.WriteDocument(stream, startDate, endDate);
await _competenceDocumentService.WriteDocument(stream, name, startDate, endDate);

return File(
stream,
Expand Down
4 changes: 2 additions & 2 deletions Epsilon.Host.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
builder.Services.AddScoped<ICompetenceComponentService, CompetenceComponentService>(static (services) => new CompetenceComponentService(
new Dictionary<string, ICompetenceComponentFetcher>
{
{ "persona_page", services.GetRequiredService<ICompetenceComponentFetcher<PersonaPage>>() },
{ "front_page", services.GetRequiredService<ICompetenceComponentFetcher<Page>>() },
{ "competence_profile", services.GetRequiredService<ICompetenceComponentFetcher<CompetenceProfile>>() },
{ "kpi_matrix", services.GetRequiredService<ICompetenceComponentFetcher<KpiMatrixCollection>>() },
}
));

builder.Services.AddScoped<ICompetenceComponentFetcher<PersonaPage>, PersonaPageComponentFetcher>();
builder.Services.AddScoped<ICompetenceComponentFetcher<Page>, PageComponentFetcher>();
builder.Services.AddScoped<ICompetenceComponentFetcher<CompetenceProfile>, CompetenceProfileComponentFetcher>();
builder.Services.AddScoped<ICompetenceComponentFetcher<KpiMatrixCollection>, KpiMatrixComponentFetcher>();

Expand Down
2 changes: 1 addition & 1 deletion Epsilon/Component/CompetenceProfileComponentFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ IConfiguration configuration
_configuration = configuration;
}

public override async Task<CompetenceProfile> Fetch(DateTime startDate, DateTime endDate)
public override async Task<CompetenceProfile> Fetch(string componentName, DateTime startDate, DateTime endDate)
{
var studentId = _configuration["Canvas:StudentId"];
var outcomesQuery = GetAllUserCoursesSubmissionOutcomes.Replace("$studentIds", $"{studentId}", StringComparison.InvariantCulture);
Expand Down
2 changes: 1 addition & 1 deletion Epsilon/Component/KpiMatrixComponentFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ IConfiguration configuration
_configuration = configuration;
}

public override async Task<KpiMatrixCollection> Fetch(DateTime startDate, DateTime endDate)
public override async Task<KpiMatrixCollection> Fetch(string componentName, DateTime startDate, DateTime endDate)
{
var studentId = _configuration["Canvas:StudentId"];
var outcomesQuery = GetUserKpiMatrixOutcomes.Replace("$studentIds", $"{studentId}", StringComparison.InvariantCultureIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,40 @@

namespace Epsilon.Component;

public class PersonaPageComponentFetcher : CompetenceComponentFetcher<PersonaPage>
public class PageComponentFetcher : CompetenceComponentFetcher<Page>
{
private readonly IPageHttpService _pageHttpService;
private readonly IFileHttpService _fileHttpService;
private readonly CanvasSettings _canvasSettings;
// private readonly StudentSettings _studentSettings;

public PersonaPageComponentFetcher(
public PageComponentFetcher(
IPageHttpService pageHttpService,
IFileHttpService fileHttpService,
IOptions<CanvasSettings> canvasSettings
)
// IOptions<StudentSettings> studentSettings
)
{
_pageHttpService = pageHttpService;
_fileHttpService = fileHttpService;
_canvasSettings = canvasSettings.Value;
// _studentSettings = studentSettings.Value;
}

public override async Task<PersonaPage> Fetch(DateTime startDate, DateTime endDate)
public override async Task<Page> Fetch(string componentName, DateTime startDate, DateTime endDate)
{
var courseId = _canvasSettings.CourseId;
var personaHtml = await _pageHttpService.GetPageByName(courseId, "front_page");
var htmlString = await _pageHttpService.GetPageByName(courseId, componentName);

var updatedPersonaHtml = await GetPersonaHtmlDocument(personaHtml);
var updatedPersonaHtml = await GetHtmlDocument(htmlString);

var personaPage = new PersonaPage(updatedPersonaHtml.Text);
var page = new Page(updatedPersonaHtml.Text);

return personaPage;
return page;
}

private async Task<HtmlDocument> GetPersonaHtmlDocument(string htmlString)

private async Task<HtmlDocument> GetHtmlDocument(string htmlString)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlString);
Expand All @@ -62,5 +66,4 @@ private async Task<HtmlDocument> GetPersonaHtmlDocument(string htmlString)

return htmlDoc;
}

}
10 changes: 5 additions & 5 deletions Epsilon/Service/CompetenceComponentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public CompetenceComponentService(IDictionary<string, ICompetenceComponentFetche
_componentFetchers = componentFetchers;
}

public async IAsyncEnumerable<ICompetenceComponent> GetComponents(DateTime startDate, DateTime endDate)
public async IAsyncEnumerable<ICompetenceComponent> GetComponents(string name, DateTime startDate, DateTime endDate)
{
foreach (var componentFetcher in _componentFetchers.Values)
{
yield return await componentFetcher.FetchUnknown(startDate, endDate);
yield return await componentFetcher.FetchUnknown(name, startDate, endDate);
}
}

public async IAsyncEnumerable<TComponent> GetComponents<TComponent>(DateTime startDate, DateTime endDate) where TComponent : ICompetenceComponent
public async IAsyncEnumerable<TComponent> GetComponents<TComponent>(string name, DateTime startDate, DateTime endDate) where TComponent : ICompetenceComponent
{
await foreach (var component in GetComponents(startDate, endDate))
await foreach (var component in GetComponents(name,startDate, endDate))
{
if (component is TComponent componentOfT)
{
Expand All @@ -35,7 +35,7 @@ public async IAsyncEnumerable<TComponent> GetComponents<TComponent>(DateTime sta
{
if (_componentFetchers.TryGetValue(name, out var fetcher))
{
return await fetcher.FetchUnknown(startDate, endDate);
return await fetcher.FetchUnknown(name, startDate, endDate);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions Epsilon/Service/CompetenceDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public CompetenceDocumentService(ICompetenceComponentService competenceComponent
_competenceComponentService = competenceComponentService;
}

public async Task<Stream> WriteDocument(Stream stream, DateTime startDate, DateTime endDate)
public async Task<Stream> WriteDocument(Stream stream, string name, DateTime startDate, DateTime endDate)
{
var startPosition = stream.Position;

var components = await _competenceComponentService.GetComponents<IWordCompetenceComponent>(startDate, endDate).ToListAsync();
var components = await _competenceComponentService.GetComponents<IWordCompetenceComponent>(name, startDate, endDate).ToListAsync();
using var document = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);

document.AddMainDocumentPart();
Expand Down
1 change: 0 additions & 1 deletion Epsilon/Service/FilterService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using DocumentFormat.OpenXml;
using Epsilon.Abstractions.Service;
using Epsilon.Canvas.Abstractions.Model;
using Epsilon.Canvas.Abstractions.Model.GraphQl;
Expand Down