diff --git a/Management.Web/Pages/CoursePageForm/CoursePageForm.razor b/Management.Web/Pages/CoursePageForm/CoursePageForm.razor index ed479d4..0eea06e 100644 --- a/Management.Web/Pages/CoursePageForm/CoursePageForm.razor +++ b/Management.Web/Pages/CoursePageForm/CoursePageForm.razor @@ -55,6 +55,39 @@ 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) @@ -94,11 +127,10 @@ return; deletingPageFromCanvas = true; - @* await canvas.Pages.Delete( + await canvas.Pages.Delete( (ulong)planner.LocalCourse.Settings.CanvasId, - pageInCanvas.Id, - assignmentContext.Assignment.Name - ); *@ + pageInCanvas.PageId + ); await planner.LoadCanvasData(); deletingPageFromCanvas = false; StateHasChanged(); diff --git a/Management/Features/Configuration/PageEditorContext.cs b/Management/Features/Configuration/PageEditorContext.cs index 4479fc8..8404205 100644 --- a/Management/Features/Configuration/PageEditorContext.cs +++ b/Management/Features/Configuration/PageEditorContext.cs @@ -99,7 +99,7 @@ public class PageEditorContext( logger.Log("cannot add page to canvas, no course stored in planner"); 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); - if(canvasPageId != null) + if(canvasPage != null) { - await canvas.CreateModuleItem( + await canvas.CreatePageModuleItem( (ulong)courseCanvasId, canvasModule.Id, Page.Name, - "Page", - (ulong)canvasPageId + canvasPage ); await planner.LocalCourse.Modules.First().SortModuleItems( @@ -131,13 +130,48 @@ public class PageEditorContext( 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) { return course.Modules.FirstOrDefault( diff --git a/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs b/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs index 25bf87d..2e71a16 100644 --- a/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs +++ b/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs @@ -1,9 +1,10 @@ +using CanvasModel.Pages; using LocalModels; using Management.Services.Canvas; public static class PageSynchronizationExtension { - public static async Task AddPageToCanvas( + public static async Task AddPageToCanvas( this LocalCourse localCourse, LocalCoursePage localPage, CanvasService canvas @@ -16,7 +17,7 @@ public static class PageSynchronizationExtension } ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId; - var canvasPageId = await canvas.Pages.Create(courseCanvasId, localPage); - return canvasPageId; + var canvasPage = await canvas.Pages.Create(courseCanvasId, localPage); + return canvasPage; } } diff --git a/Management/Services/Canvas/CanvasCoursePageService.cs b/Management/Services/Canvas/CanvasCoursePageService.cs index bf265ad..fdbd730 100644 --- a/Management/Services/Canvas/CanvasCoursePageService.cs +++ b/Management/Services/Canvas/CanvasCoursePageService.cs @@ -30,7 +30,7 @@ public class CanvasCoursePageService( } - public async Task Create( + public async Task Create( ulong canvasCourseId, LocalCoursePage localCourse ) @@ -49,22 +49,22 @@ public class CanvasCoursePageService( if (canvasPage == null) throw new Exception("created canvas course page was null"); - return canvasPage.PageId; + return canvasPage; } public async Task Update( ulong courseId, - string canvasPageId, - LocalCoursePage localCourse + ulong canvasPageId, + LocalCoursePage localCoursePage ) { - log.Log($"updating course page: {localCourse.Name}"); + log.Log($"updating course page: {localCoursePage.Name}"); var url = $"courses/{courseId}/pages/{canvasPageId}"; var request = new RestRequest(url); var body = new { - title = localCourse.Name, - body = localCourse.GetBodyHtml() + title = localCoursePage.Name, + body = localCoursePage.GetBodyHtml() }; var bodyObj = new { wiki_page = body }; request.AddBody(bodyObj); @@ -72,7 +72,7 @@ public class CanvasCoursePageService( 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}"); var url = $"courses/{courseId}/pages/{canvasPageId}"; diff --git a/Management/Services/Canvas/CanvasService.cs b/Management/Services/Canvas/CanvasService.cs index 668f919..e7fd154 100644 --- a/Management/Services/Canvas/CanvasService.cs +++ b/Management/Services/Canvas/CanvasService.cs @@ -5,6 +5,7 @@ using CanvasModel.Courses; using CanvasModel.EnrollmentTerms; using CanvasModel.Modules; using RestSharp; +using CanvasModel.Pages; namespace Management.Services.Canvas; @@ -145,6 +146,31 @@ public class CanvasService( var request = new RestRequest(url); request.AddBody(body); + var (newItem, _response) = await webRequestor.PostAsync(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(request); if (newItem == null) throw new Exception("something went wrong updating module item with string content id");