added delete support for assignments

This commit is contained in:
2023-08-07 18:41:04 -06:00
parent 224664c7a3
commit 7a2ee58617
9 changed files with 208 additions and 40 deletions

View File

@@ -127,9 +127,14 @@ public static partial class CoursePlannerSyncronizationExtensions
if (!quiet)
{
if (!dueDatesSame)
{
Console.WriteLine(
$"Due dates different for {localAssignment.Name}, local: {localAssignment.DueAt}, in canvas {canvasAssignment.DueAt}"
);
Console.WriteLine(JsonSerializer.Serialize(localAssignment.DueAt));
Console.WriteLine(JsonSerializer.Serialize(canvasAssignment.DueAt));
}
if (!descriptionSame)
{

View File

@@ -16,8 +16,10 @@ public static class CoursePlannerExtensions
.OrderBy(a => a.DueAt)
.DistinctBy(a => a.Id)
.Select(a => a.validateSubmissionTypes())
.Select(a => a.validateDates())
.ToArray()
}
);
).ToArray();
var cleanStartDay = new DateTime(
incomingCourse.StartDate.Year,
@@ -109,4 +111,13 @@ public static class CoursePlannerExtensions
};
return assignment;
}
public static LocalAssignment validateDates(this LocalAssignment assignment)
{
return assignment with
{
DueAt=assignment.DueAt.AddMilliseconds(0).AddMilliseconds(0),
LockAt=assignment.LockAt?.AddMilliseconds(0).AddMilliseconds(0)
};
}
}

View File

@@ -6,7 +6,7 @@ namespace Management.Services.Canvas;
public class CanvasAssignmentService
{
private IWebRequestor webRequestor;
private readonly IWebRequestor webRequestor;
private readonly CanvasServiceUtils utils;
public CanvasAssignmentService(IWebRequestor webRequestor, CanvasServiceUtils utils)
@@ -34,7 +34,7 @@ public class CanvasAssignmentService
string htmlDescription
)
{
System.Console.WriteLine($"creating assignment: {localAssignment.Name}");
Console.WriteLine($"creating assignment: {localAssignment.Name}");
var url = $"courses/{courseId}/assignments";
var request = new RestRequest(url);
var body = new CanvasAssignmentCreationRequest()
@@ -62,7 +62,7 @@ public class CanvasAssignmentService
public async Task Update(ulong courseId, LocalAssignment localAssignment, string htmlDescription)
{
System.Console.WriteLine($"updating assignment: {localAssignment.Name}");
Console.WriteLine($"updating assignment: {localAssignment.Name}");
var url = $"courses/{courseId}/assignments/{localAssignment.CanvasId}";
var request = new RestRequest(url);
var body = new CanvasAssignmentCreationRequest()
@@ -82,6 +82,19 @@ public class CanvasAssignmentService
await CreateRubric(courseId, localAssignment);
}
public async Task Delete(ulong courseId, LocalAssignment assignment)
{
Console.WriteLine($"deleting assignment from canvas {assignment.Name}");
var url = $"courses/{courseId}/assignments/{assignment.CanvasId}";
var request = new RestRequest(url);
var response = await webRequestor.DeleteAsync(request);
if (!response.IsSuccessful)
{
Console.WriteLine(url);
throw new Exception("Failed to delete assignment");
}
}
public async Task CreateRubric(ulong courseId, LocalAssignment localAssignment)
{
if (localAssignment.CanvasId == null)
@@ -101,7 +114,7 @@ public class CanvasAssignmentService
{
description = rubricItem.Label,
points = rubricItem.Points,
ratings = ratings
ratings
};
i++;
}
@@ -130,9 +143,9 @@ public class CanvasAssignmentService
var rubricCreationRequest = new RestRequest(creationUrl);
rubricCreationRequest.AddBody(body);
rubricCreationRequest.AddHeader("Content-Type", "application/json");
var (rubricCreationResponse, creationResponse) =
await webRequestor.PostAsync<CanvasRubricCreationResponse>(rubricCreationRequest);
var (rubricCreationResponse, _) = await webRequestor.PostAsync<CanvasRubricCreationResponse>(
rubricCreationRequest
);
if (rubricCreationResponse == null)
throw new Exception("failed to create rubric before association");
@@ -145,8 +158,6 @@ public class CanvasAssignmentService
var pointAdjustmentRequest = new RestRequest(adjustmentUrl);
pointAdjustmentRequest.AddBody(assignmentPointCorrectionBody);
pointAdjustmentRequest.AddHeader("Content-Type", "application/json");
var (updatedAssignment, adjustmentResponse) = await webRequestor.PutAsync<CanvasAssignment>(
pointAdjustmentRequest
);
var (_, _) = await webRequestor.PutAsync<CanvasAssignment>(pointAdjustmentRequest);
}
}

View File

@@ -8,4 +8,5 @@ public interface IWebRequestor
Task<(T?, RestResponse)> PostAsync<T>(RestRequest request);
Task<RestResponse> PutAsync(RestRequest request);
Task<(T?, RestResponse)> PutAsync<T>(RestRequest request);
Task<RestResponse> DeleteAsync(RestRequest request);
}

View File

@@ -18,13 +18,13 @@ public class WebRequestor : IWebRequestor
public async Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request)
{
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T[]>(response), response);
return (deserialize<T[]>(response), response);
}
public async Task<(T?, RestResponse)> GetAsync<T>(RestRequest request)
{
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T>(response), response);
return (deserialize<T>(response), response);
}
public async Task<RestResponse> PostAsync(RestRequest request)
@@ -32,9 +32,9 @@ public class WebRequestor : IWebRequestor
var response = await client.ExecutePostAsync(request);
if (!response.IsSuccessful)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine("error with response");
Console.WriteLine(response.Content);
Console.WriteLine(response.ResponseUri);
Console.WriteLine("error with response");
throw new Exception("error with response");
}
return response;
@@ -43,7 +43,7 @@ public class WebRequestor : IWebRequestor
public async Task<(T?, RestResponse)> PostAsync<T>(RestRequest request)
{
var response = await client.ExecutePostAsync(request);
return (Deserialize<T>(response), response);
return (deserialize<T>(response), response);
}
public async Task<RestResponse> PutAsync(RestRequest request)
@@ -51,9 +51,9 @@ public class WebRequestor : IWebRequestor
var response = await client.ExecutePutAsync(request);
// if (!response.IsSuccessful)
// {
// System.Console.WriteLine(response.Content);
// System.Console.WriteLine(response.ResponseUri);
// System.Console.WriteLine("error with response");
// Console.WriteLine(response.Content);
// Console.WriteLine(response.ResponseUri);
// Console.WriteLine("error with response");
// throw new Exception("error with response");
// }
return response;
@@ -62,17 +62,22 @@ public class WebRequestor : IWebRequestor
public async Task<(T?, RestResponse)> PutAsync<T>(RestRequest request)
{
var response = await client.ExecutePutAsync(request);
return (Deserialize<T>(response), response);
return (deserialize<T>(response), response);
}
private T? Deserialize<T>(RestResponse response)
public async Task<RestResponse> DeleteAsync(RestRequest request)
{
return await client.DeleteAsync(request);
}
private static T? deserialize<T>(RestResponse response)
{
if (!response.IsSuccessful)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.ErrorMessage);
System.Console.WriteLine("error with response");
Console.WriteLine(response.Content);
Console.WriteLine(response.ResponseUri);
Console.WriteLine(response.ErrorMessage);
Console.WriteLine("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}");
@@ -83,21 +88,21 @@ public class WebRequestor : IWebRequestor
if (data == null)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine("could not parse response, got empty object");
Console.WriteLine(response.Content);
Console.WriteLine(response.ResponseUri);
Console.WriteLine("could not parse response, got empty object");
}
return data;
}
catch (System.NotSupportedException )
catch (NotSupportedException)
{
Console.WriteLine(response.Content);
throw;
}
catch (JsonException)
{
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.Content);
Console.WriteLine(response.ResponseUri);
Console.WriteLine(response.Content);
Console.WriteLine($"An error occurred during deserialization");
throw;
}