diff --git a/Management/Features/Configuration/CoursePlanner.cs b/Management/Features/Configuration/CoursePlanner.cs index 44544d4..d5e98a8 100644 --- a/Management/Features/Configuration/CoursePlanner.cs +++ b/Management/Features/Configuration/CoursePlanner.cs @@ -123,18 +123,39 @@ public class CoursePlanner }); var modules = await Task.WhenAll(moduleTasks); - LocalCourse = LocalCourse with - { - Modules = modules - }; + LocalCourse = LocalCourse with { Modules = modules }; } private async Task ensureAssignmentInCanvas_returnUpdated( LocalAssignment localAssignment ) { - // var canvasAssignment = await canvas. - return localAssignment; + if ( + LocalCourse == null + || LocalCourse.CanvasId == null + || CanvasAssignments == null + || CanvasModules == null + ) + throw new Exception( + "cannot create canvas assignment if local course is null or other values not set" + ); + ulong canvasId = LocalCourse.CanvasId ?? throw new Exception("no canvas id to create course"); + var canvasAssignment = await canvas.CreateAssignment( + courseId: canvasId, + name: localAssignment.name, + submissionTypes: localAssignment.submission_types, + description: localAssignment.description, + dueAt: localAssignment.due_at, + lockAt: localAssignment.lock_at, + pointsPossible: localAssignment.points_possible + ); + + Console.WriteLine(JsonSerializer.Serialize(canvasAssignment)); + + return localAssignment with + { + canvasId = canvasAssignment.Id + }; } public void Clear() diff --git a/Management/Models/CanvasModels/Assignments/CanvasAssignment.cs b/Management/Models/CanvasModels/Assignments/CanvasAssignment.cs index e2d4dbf..9dfa293 100644 --- a/Management/Models/CanvasModels/Assignments/CanvasAssignment.cs +++ b/Management/Models/CanvasModels/Assignments/CanvasAssignment.cs @@ -5,7 +5,6 @@ namespace CanvasModel.Assignments; public record CanvasAssignment ( - [property: JsonPropertyName("id")] ulong Id, diff --git a/Management/Services/CanvasRequests/CanvasAssignmentCreationRequest.cs b/Management/Services/CanvasRequests/CanvasAssignmentCreationRequest.cs new file mode 100644 index 0000000..1f37595 --- /dev/null +++ b/Management/Services/CanvasRequests/CanvasAssignmentCreationRequest.cs @@ -0,0 +1,9 @@ +public record CanvasAssignmentCreationRequest +{ + public string? name { get; set; } + public IEnumerable submission_types { get; set; } = Enumerable.Empty(); + public string? description { get; set; } + public DateTime? lock_at { get; set; } + public DateTime? due_at { get; set; } + public int? points_possible { get; set; } +} diff --git a/Management/Services/CanvasService.cs b/Management/Services/CanvasService.cs index ac6eb74..f989b8b 100644 --- a/Management/Services/CanvasService.cs +++ b/Management/Services/CanvasService.cs @@ -56,18 +56,42 @@ public class CanvasService : ICanvasService public async Task> GetAssignments(ulong courseId) { - var url = $"courses/{courseId}/assignments "; + var url = $"courses/{courseId}/assignments"; var request = new RestRequest(url); var assignmentResponse = await PaginatedRequest>(request); return assignmentResponse.SelectMany(c => c); } - // public async Task CreateAssignment( - - // ) - // { - - // } + public async Task CreateAssignment( + ulong courseId, + string name, + IEnumerable submissionTypes, + string? description, + DateTime? dueAt, + DateTime? lockAt, + int? pointsPossible + ) + { + System.Console.WriteLine($"creating assignment: {name}"); + var url = $"courses/{courseId}/assignments"; + var request = new RestRequest(url); + var body = new CanvasAssignmentCreationRequest() + { + name = name, + submission_types = submissionTypes.Select(t => t.ToString()), + description = description, + due_at = dueAt, + lock_at = lockAt, + points_possible = pointsPossible + }; + request.AddHeader("Content-Type", "application/json"); + var bodyObj = new { assignment = body }; + request.AddBody(bodyObj); + var (canvasAssignment, response) = await webRequestor.PostAsync(request); + if (canvasAssignment == null) + throw new Exception("created canvas assignment was null"); + return canvasAssignment; + } public async Task> GetModules(ulong courseId) { diff --git a/Management/Services/WebRequestor.cs b/Management/Services/WebRequestor.cs index b29933c..3eb3c8d 100644 --- a/Management/Services/WebRequestor.cs +++ b/Management/Services/WebRequestor.cs @@ -15,7 +15,6 @@ public class WebRequestor : IWebRequestor client.AddDefaultHeader("Authorization", $"Bearer {token}"); } - public async Task<(T[]?, RestResponse)> GetManyAsync(RestRequest request) { var response = await client.ExecuteGetAsync(request); @@ -55,19 +54,29 @@ public class WebRequestor : IWebRequestor System.Console.WriteLine(response.ResponseUri); System.Console.WriteLine(response.ErrorMessage); System.Console.WriteLine("error with response"); - throw new Exception("error with response"); + // Console.WriteLine(JsonSerializer.Serialize(response)); + Console.WriteLine(JsonSerializer.Serialize(response.Request?.Parameters)); + throw new Exception($"error with response to {response.ResponseUri} {response.StatusCode}"); } try { - var data = JsonSerializer.Deserialize(response.Content!); - - if (data == null) + try { - System.Console.WriteLine(response.Content); - System.Console.WriteLine(response.ResponseUri); - System.Console.WriteLine("could not parse response, got empty object"); + var data = JsonSerializer.Deserialize(response.Content!); + + if (data == null) + { + System.Console.WriteLine(response.Content); + System.Console.WriteLine(response.ResponseUri); + System.Console.WriteLine("could not parse response, got empty object"); + } + return data; + } + catch (System.NotSupportedException exception) + { + Console.WriteLine(response.Content); + throw exception; } - return data; } catch (JsonException ex) { diff --git a/requests/assignment.http b/requests/assignment.http index 999e099..3d3c07d 100644 --- a/requests/assignment.http +++ b/requests/assignment.http @@ -11,9 +11,15 @@ Authorization: Bearer {{$dotenv CANVAS_TOKEN}} ### POST https://snow.instructure.com/api/v1/courses/872095/assignments +Content-Type: application/json Authorization: Bearer {{$dotenv CANVAS_TOKEN}} -{"assignment":{"name:":"New Assignment","description:":"This is a new assignment"}} +{ + "assignment": { + "name:": "New Assignment", + "description:": "This is a new assignment" + } +} ### POST https://snow.instructure.com/api/v1/courses/705168/assignments