finished CRUD on pages

This commit is contained in:
2024-01-12 16:31:17 -07:00
parent 550a42f874
commit 324bb94eda
5 changed files with 116 additions and 23 deletions

View File

@@ -55,6 +55,39 @@
private async Task HandleDelete() private async Task HandleDelete()
{ {
if (planner.LocalCourse != null && pageContext.Page != null)
{
var page = pageContext.Page;
var currentModule = planner
.LocalCourse
.Modules
.First(m =>
m.Pages.Contains(page)
) ?? throw new Exception("handling page delete, could not find module");
var newModules = planner.LocalCourse.Modules.Select(m =>
m.Name == currentModule.Name
? m with
{
Pages = m.Pages.Where(p => p != page).ToArray()
}
: m
)
.ToArray();
planner.LocalCourse = planner.LocalCourse with
{
Modules = newModules
};
if (pageInCanvas != null && planner.LocalCourse.Settings.CanvasId != null)
{
ulong courseId = planner.LocalCourse.Settings.CanvasId ?? throw new Exception("cannot delete if no course id");
await canvas.Pages.Delete(courseId, pageInCanvas.PageId);
}
Navigation.NavigateTo("/course/" + planner.LocalCourse?.Settings.Name);
}
} }
private void handleNameChange(ChangeEventArgs e) private void handleNameChange(ChangeEventArgs e)
@@ -94,11 +127,10 @@
return; return;
deletingPageFromCanvas = true; deletingPageFromCanvas = true;
@* await canvas.Pages.Delete( await canvas.Pages.Delete(
(ulong)planner.LocalCourse.Settings.CanvasId, (ulong)planner.LocalCourse.Settings.CanvasId,
pageInCanvas.Id, pageInCanvas.PageId
assignmentContext.Assignment.Name );
); *@
await planner.LoadCanvasData(); await planner.LoadCanvasData();
deletingPageFromCanvas = false; deletingPageFromCanvas = false;
StateHasChanged(); StateHasChanged();

View File

@@ -99,7 +99,7 @@ public class PageEditorContext(
logger.Log("cannot add page to canvas, no course stored in planner"); logger.Log("cannot add page to canvas, no course stored in planner");
return; return;
} }
var canvasPageId = await planner.LocalCourse.AddPageToCanvas(Page, canvas); var canvasPage = await planner.LocalCourse.AddPageToCanvas(Page, canvas);
@@ -112,14 +112,13 @@ public class PageEditorContext(
var canvasModule = getCurrentCanvasModule(Page, planner.LocalCourse); var canvasModule = getCurrentCanvasModule(Page, planner.LocalCourse);
if(canvasPageId != null) if(canvasPage != null)
{ {
await canvas.CreateModuleItem( await canvas.CreatePageModuleItem(
(ulong)courseCanvasId, (ulong)courseCanvasId,
canvasModule.Id, canvasModule.Id,
Page.Name, Page.Name,
"Page", canvasPage
(ulong)canvasPageId
); );
await planner.LocalCourse.Modules.First().SortModuleItems( await planner.LocalCourse.Modules.First().SortModuleItems(
@@ -131,12 +130,47 @@ public class PageEditorContext(
logger.Log($"finished adding page {Page.Name} to canvas"); logger.Log($"finished adding page {Page.Name} to canvas");
} }
public async Task UpdateInCanvas(ulong pageId) public async Task UpdateInCanvas(ulong canvasPageId)
{ {
logger.Log("started to update page in canvas");
if (Page == null)
{
logger.Log("cannot update null page in canvas");
return;
} }
await planner.LoadCanvasData();
if (planner.CanvasPages == null)
{
logger.Log("cannot update page in canvas, failed to retrieve current pages");
return;
}
if (planner.LocalCourse == null)
{
logger.Log("cannot update page in canvas, no course stored in planner");
return;
}
if (planner.LocalCourse.Settings.CanvasId == null)
{
logger.Log("Cannot update page with null local course canvas id");
return;
}
var assignmentInCanvas = planner.CanvasPages?.FirstOrDefault(p => p.PageId == canvasPageId);
if (assignmentInCanvas == null)
{
logger.Log("cannot update page in canvas, could not find canvas page with id: " + canvasPageId);
return;
}
await canvas.Pages.Update(
courseId: (ulong)planner.LocalCourse.Settings.CanvasId,
canvasPageId: canvasPageId,
localCoursePage: Page
);
}
private static LocalModule getCurrentLocalModule(LocalCoursePage page, LocalCourse course) private static LocalModule getCurrentLocalModule(LocalCoursePage page, LocalCourse course)
{ {

View File

@@ -1,9 +1,10 @@
using CanvasModel.Pages;
using LocalModels; using LocalModels;
using Management.Services.Canvas; using Management.Services.Canvas;
public static class PageSynchronizationExtension public static class PageSynchronizationExtension
{ {
public static async Task<ulong?> AddPageToCanvas( public static async Task<CanvasPage?> AddPageToCanvas(
this LocalCourse localCourse, this LocalCourse localCourse,
LocalCoursePage localPage, LocalCoursePage localPage,
CanvasService canvas CanvasService canvas
@@ -16,7 +17,7 @@ public static class PageSynchronizationExtension
} }
ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId; ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId;
var canvasPageId = await canvas.Pages.Create(courseCanvasId, localPage); var canvasPage = await canvas.Pages.Create(courseCanvasId, localPage);
return canvasPageId; return canvasPage;
} }
} }

View File

@@ -30,7 +30,7 @@ public class CanvasCoursePageService(
} }
public async Task<ulong> Create( public async Task<CanvasPage> Create(
ulong canvasCourseId, ulong canvasCourseId,
LocalCoursePage localCourse LocalCoursePage localCourse
) )
@@ -49,22 +49,22 @@ public class CanvasCoursePageService(
if (canvasPage == null) if (canvasPage == null)
throw new Exception("created canvas course page was null"); throw new Exception("created canvas course page was null");
return canvasPage.PageId; return canvasPage;
} }
public async Task Update( public async Task Update(
ulong courseId, ulong courseId,
string canvasPageId, ulong canvasPageId,
LocalCoursePage localCourse LocalCoursePage localCoursePage
) )
{ {
log.Log($"updating course page: {localCourse.Name}"); log.Log($"updating course page: {localCoursePage.Name}");
var url = $"courses/{courseId}/pages/{canvasPageId}"; var url = $"courses/{courseId}/pages/{canvasPageId}";
var request = new RestRequest(url); var request = new RestRequest(url);
var body = new var body = new
{ {
title = localCourse.Name, title = localCoursePage.Name,
body = localCourse.GetBodyHtml() body = localCoursePage.GetBodyHtml()
}; };
var bodyObj = new { wiki_page = body }; var bodyObj = new { wiki_page = body };
request.AddBody(bodyObj); request.AddBody(bodyObj);
@@ -72,7 +72,7 @@ public class CanvasCoursePageService(
await webRequestor.PutAsync(request); await webRequestor.PutAsync(request);
} }
public async Task Delete(ulong courseId, string canvasPageId) public async Task Delete(ulong courseId, ulong canvasPageId)
{ {
log.Log($"deleting page from canvas {canvasPageId}"); log.Log($"deleting page from canvas {canvasPageId}");
var url = $"courses/{courseId}/pages/{canvasPageId}"; var url = $"courses/{courseId}/pages/{canvasPageId}";

View File

@@ -5,6 +5,7 @@ using CanvasModel.Courses;
using CanvasModel.EnrollmentTerms; using CanvasModel.EnrollmentTerms;
using CanvasModel.Modules; using CanvasModel.Modules;
using RestSharp; using RestSharp;
using CanvasModel.Pages;
namespace Management.Services.Canvas; namespace Management.Services.Canvas;
@@ -145,6 +146,31 @@ public class CanvasService(
var request = new RestRequest(url); var request = new RestRequest(url);
request.AddBody(body); request.AddBody(body);
var (newItem, _response) = await webRequestor.PostAsync<CanvasModuleItem>(request);
if (newItem == null)
throw new Exception("something went wrong updating module item with string content id");
}
public async Task CreatePageModuleItem(
ulong canvasCourseId,
ulong canvasModuleId,
string title,
CanvasPage canvasPage
)
{
logger.Log($"creating new module item {title}");
var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items";
var body = new
{
module_item = new
{
title,
type = "Page",
page_url = canvasPage.Url,
}
};
var request = new RestRequest(url);
request.AddBody(body);
var (newItem, _response) = await webRequestor.PostAsync<CanvasModuleItem>(request); var (newItem, _response) = await webRequestor.PostAsync<CanvasModuleItem>(request);
if (newItem == null) if (newItem == null)
throw new Exception("something went wrong updating module item with string content id"); throw new Exception("something went wrong updating module item with string content id");