data types are better

This commit is contained in:
2023-07-16 01:08:30 -06:00
parent ed1963c67b
commit d691f817b7
13 changed files with 196 additions and 99 deletions

View File

@@ -4,8 +4,8 @@
@using CanvasModel.Courses
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@inject CanvasService canvasService
@inject CoursePlanner configurationManagement
@inject CanvasService canvas
@inject CoursePlanner planner
@inject ProtectedLocalStorage BrowserStorage
@code
@@ -30,10 +30,11 @@
}
private ulong? selectedCourseId {
get => configurationManagement.Course?.Id;
get => planner.Course?.Id;
set
{
configurationManagement.Course = courses?.First(c => c.Id == value);
planner.Course = courses?.First(c => c.Id == value);
updateModules();
}
}
@@ -42,7 +43,7 @@
protected override async Task OnInitializedAsync()
{
terms = await canvasService.GetCurrentTermsFor();
terms = await canvas.GetCurrentTermsFor();
readTermFromConfig();
readDaysFromConfig();
}
@@ -54,8 +55,7 @@
var storedConfiguration = await BrowserStorage.GetAsync<SemesterCalendarConfig>(semesterConfigurationKey);
if (storedConfiguration.Success)
{
Console.WriteLine(JsonSerializer.Serialize(storedConfiguration.Value));
configurationManagement.SemesterCalendar = storedConfiguration.Value;
planner.SemesterCalendar = storedConfiguration.Value;
}
else
{
@@ -71,9 +71,7 @@
{
loadingCourses = true;
courses = await canvasService.GetCourses((ulong)selectedTermId);
System.Console.WriteLine(courses);
System.Console.WriteLine(selectedTermId);
courses = await canvas.GetCourses((ulong)selectedTermId);
loadingCourses = false;
}
else
@@ -81,12 +79,21 @@
StateHasChanged();
}
private async Task updateModules()
{
if(planner.Course != null)
{
planner.Modules = await canvas.GetModules(planner.Course.Id);
}
StateHasChanged();
}
private void readTermFromConfig()
{
if (terms == null || configurationManagement.SemesterCalendar == null) return;
if (terms == null || planner.SemesterCalendar == null) return;
foreach (var term in terms)
{
var termInConfiguration = configurationManagement.SemesterCalendar.StartDate == term.StartAt;
var termInConfiguration = planner.SemesterCalendar.StartDate == term.StartAt;
if (termInConfiguration)
{
selectedTermId = term.Id;
@@ -96,24 +103,22 @@
private void readDaysFromConfig()
{
if (terms == null || configurationManagement.SemesterCalendar == null) return;
if (terms == null || planner.SemesterCalendar == null) return;
days = configurationManagement.SemesterCalendar.Days.ToList();
days = planner.SemesterCalendar.Days.ToList();
}
public async void HandleSave()
{
saved = true;
configurationManagement.SetConfiguration(
planner.SetConfiguration(
selectedTerm ?? throw new Exception("cannot save configuration without selecting term"),
days.ToArray()
);
await BrowserStorage.SetAsync(
semesterConfigurationKey,
configurationManagement.SemesterCalendar ?? throw new Exception("Semester Calendar configuration not properly configured")
planner.SemesterCalendar ?? throw new Exception("Semester Calendar configuration not properly configured")
);
Console.WriteLine(JsonSerializer.Serialize(await BrowserStorage.GetAsync<SemesterCalendarConfig>(semesterConfigurationKey)));
}
}
<PageTitle>Index</PageTitle>
@@ -182,8 +187,12 @@
</div>
</div>
}
@if(planner.Modules != null)
{
<div>@JsonSerializer.Serialize(planner.Modules)</div>
}
@if (configurationManagement.SemesterCalendar is not null)
@if (planner.SemesterCalendar is not null)
{
<div class="text-center">Config complete</div>
}

View File

@@ -1,6 +1,9 @@
global using System.Text.Json.Serialization;
global using System.Text.Json;
global using System.ComponentModel.DataAnnotations;
global using CanvasModel.EnrollmentTerms;
global using CanvasModel.Courses;
global using CanvasModel;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

View File

@@ -3,38 +3,29 @@
@code {
[Parameter, EditorRequired]
public int ModuleIndex { get; set; }
public CourseModule Module { get; set; } = default!;
private bool showAddAssignment { get; set; } = false;
private CourseModule? module
{
get
{
return configurationManagement.Modules.ElementAtOrDefault(ModuleIndex);
}
}
}
@if (module != null)
<h3 class="text-center">@Module.Name</h3>
<button class="btn btn-primary" @onclick="() => showAddAssignment = true">Add Assignment</button>
@if (showAddAssignment)
{
<h3 class="text-center">@module.Name</h3>
<button class="btn btn-primary" @onclick="() => showAddAssignment = true">Add Assignment</button>
@if (showAddAssignment)
{
<div class="ms-5 ">
@* <div class="ms-5 ">
<div class="bg-light border rounded m-3 p-3">
<NewAssignment ModuleIndex="ModuleIndex" OnSubmit="() => showAddAssignment = false" />
</div>
</div>
}
<h5>Assignments</h5>
<div class="row">
@foreach (var a in module.Assignments)
{
<AssignmentCard assignment="a" />
}
</div>
</div> *@
}
<h5>Assignments</h5>
<div class="row">
@* @foreach (var a in module.Assignments)
{
<AssignmentCard assignment="a" />
} *@
</div>

View File

@@ -34,10 +34,10 @@ else
<NewModule OnSubmit="() => showNewModule = false" />
}
@foreach (var i in configurationManagement.Modules.Select((_value, i) => i))
@foreach (var module in configurationManagement.Modules)
{
<hr>
<ModuleDetail ModuleIndex="i" />
<ModuleDetail Module="module" />
}
<hr>

View File

@@ -1,4 +1,5 @@
@inject CoursePlanner configurationManagement
@inject CoursePlanner planner
@inject CanvasService canvas
@code {
@@ -11,8 +12,11 @@
private async Task submitHandler()
{
var module = new CourseModule(Name: Name, Assignments: new LocalAssignment[] { });
configurationManagement.Modules = configurationManagement.Modules.Append(module);
if(planner.Course != null && Name != "")
{
await canvas.CreateModule(planner.Course.Id, Name);
planner.Modules = await canvas.GetModules(planner.Course.Id);
}
Name = "";
await OnSubmit.InvokeAsync();
}

View File

@@ -52,6 +52,7 @@ public class StorageManagement
{
// var courses =
planner.Course = await canvas.GetCourse(storedCourseId.Value);
planner.Modules = await canvas.GetModules(planner.Course.Id);
}
else
{

View File

@@ -1,21 +1,19 @@
using CanvasModel.EnrollmentTerms;
using CanvasModel.Courses;
using CanvasModel;
public class CoursePlanner
{
public void SetConfiguration(
EnrollmentTermModel canvasTerm,
DayOfWeek[] daysOfWeek
)
public void SetConfiguration(EnrollmentTermModel canvasTerm, DayOfWeek[] daysOfWeek)
{
var start = canvasTerm.StartAt ?? throw new Exception($"Canvas Term must have a start date. Term: {canvasTerm.Name}");
var end = canvasTerm.EndAt ?? throw new Exception($"Canvas Term must have a end date. Term: {canvasTerm.Name}");
var start =
canvasTerm.StartAt
?? throw new Exception($"Canvas Term must have a start date. Term: {canvasTerm.Name}");
var end =
canvasTerm.EndAt
?? throw new Exception($"Canvas Term must have a end date. Term: {canvasTerm.Name}");
SemesterCalendar = new SemesterCalendarConfig(
StartDate: start,
EndDate: end,
Days: daysOfWeek
);
SemesterCalendar = new SemesterCalendarConfig(StartDate: start, EndDate: end, Days: daysOfWeek);
}
public SemesterCalendarConfig? SemesterCalendar { get; set; } = null;

View File

@@ -1,8 +1,8 @@
using CanvasModel.Enrollments;
namespace CanvasModel.Courses;
public record CourseModel
(
public record CourseModel(
[property: JsonPropertyName("id")] ulong Id,
[property: JsonPropertyName("sis_course_id")] string SisCourseId,
[property: JsonPropertyName("uuid")] string Uuid,
@@ -33,21 +33,27 @@ public record CourseModel
[property: JsonPropertyName("needs_grading_count")] uint? NeedsGradingCount = null,
[property: JsonPropertyName("term")] TermModel? Term = null,
[property: JsonPropertyName("course_progress")] CourseProgressModel? CourseProgress = null,
[property: JsonPropertyName("apply_assignment_group_weights")] bool? ApplyAssignmentGroupWeights = null,
[property: JsonPropertyName("is_public")] bool? Is= null,
[property: JsonPropertyName("apply_assignment_group_weights")]
bool? ApplyAssignmentGroupWeights = null,
[property: JsonPropertyName("is_public")] bool? Is = null,
[property: JsonPropertyName("is_public_to_auth_users")] bool? IsPublicToAuthUsers = null,
[property: JsonPropertyName("public_syllabus")] bool? PublicSyllabus = null,
[property: JsonPropertyName("public_syllabus_to_auth")] bool? PublicSyllabusToAuth = null,
[property: JsonPropertyName("public_description")] string? PublicDescription = null,
[property: JsonPropertyName("hide_final_grades")] bool? HideFinalGrades = null,
[property: JsonPropertyName("allow_student_assignment_edits")] bool? AllowStudentAssignmentEdits = null,
[property: JsonPropertyName("allow_student_assignment_edits")]
bool? AllowStudentAssignmentEdits = null,
[property: JsonPropertyName("allow_wiki_comments")] bool? AllowWikiComments = null,
[property: JsonPropertyName("allow_student_forum_attachments")] bool? AllowStudentForumAttachments = null,
[property: JsonPropertyName("allow_student_forum_attachments")]
bool? AllowStudentForumAttachments = null,
[property: JsonPropertyName("open_enrollment")] bool? OpenEnrollment = null,
[property: JsonPropertyName("self_enrollment")] bool? SelfEnrollment = null,
[property: JsonPropertyName("restrict_enrollments_to_courses")] bool? RestrictEnrollmentsToCourseDates = null,
[property: JsonPropertyName("restrict_enrollments_to_courses")]
bool? RestrictEnrollmentsToCourseDates = null,
[property: JsonPropertyName("access_restricted_by_date")] bool? AccessRestrictedByDate = null,
[property: JsonPropertyName("blueprint")] bool? Blueprint = null,
[property: JsonPropertyName("blueprint_restrictions")] Dictionary<string, bool>? BlueprintRestrictions = null,
[property: JsonPropertyName("blueprint_restrictions_by_object_type")] Dictionary<string, Dictionary<string, bool>>? BlueprintRestrictionsByObjectType = null
[property: JsonPropertyName("blueprint_restrictions")]
Dictionary<string, bool>? BlueprintRestrictions = null,
[property: JsonPropertyName("blueprint_restrictions_by_object_type")]
Dictionary<string, Dictionary<string, bool>>? BlueprintRestrictionsByObjectType = null
);

View File

@@ -1,12 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace CanvasModel;
public record CourseModule(
[property: Required]
[property: StringLength(50, ErrorMessage = "Name too long (50 character limit).")]
string Name,
IEnumerable<LocalAssignment>? Assignments = null
)
{
[JsonInclude]
public IEnumerable<LocalAssignment> Assignments = Assignments ?? new LocalAssignment[] { };
}
[property: JsonPropertyName("id")] ulong Id,
[property: JsonPropertyName("name")] string Name
// [property: JsonPropertyName("start_at")] DateTime StartAt,
// [property: JsonPropertyName("end_at")] DateTime EndAt,
// [property: JsonPropertyName("description")] string Description
);

View File

@@ -1,3 +1,4 @@
using CanvasModel;
using CanvasModel.Courses;
using CanvasModel.EnrollmentTerms;
using RestSharp;
@@ -40,35 +41,59 @@ public class CanvasService : ICanvasService
public async Task<CourseModel> GetCourse(ulong courseId)
{
var url = $"course/${courseId}";
var url = $"course/{courseId}";
var request = new RestRequest(url);
var response = await webRequestor.GetAsync<CourseModel>(request);
var (data, response) = await webRequestor.GetAsync<CourseModel>(request);
if (response.Data == null)
if (data == null)
{
System.Console.WriteLine(response.Content);
System.Console.WriteLine(response.ResponseUri);
throw new Exception("error getting course from canvas");
}
return response.Data;
return data;
}
public async Task<IEnumerable<CourseModule>> GetModules(ulong courseId)
{
var url = $"courses/{courseId}/modules";
var request = new RestRequest(url);
var modules = await PaginatedRequest<IEnumerable<CourseModule>>(request);
return modules.SelectMany(c => c).ToArray();
}
public async Task CreateModule(ulong courseId, string name)
{
var url = $"courses/{courseId}/modules";
var request = new RestRequest(url);
request.AddParameter("module[name]", name);
await webRequestor.PostAsync(request);
}
private async Task<IEnumerable<T>> PaginatedRequest<T>(RestRequest request)
{
var requestCount = 1;
request.AddQueryParameter("per_page", "100");
RestResponse<T> response = await webRequestor.GetAsync<T>(request);
var (data, response) = await webRequestor.GetAsync<T>(request);
var returnData = response.Data != null ? new T[] { response.Data } : new T[] { };
if (response.ErrorMessage?.Length > 0)
{
System.Console.WriteLine("error in response");
System.Console.WriteLine(response.ErrorMessage);
throw new Exception("error in response");
}
var returnData = data != null ? new T[] { data } : new T[] { };
var nextUrl = getNextUrl(response.Headers);
while (nextUrl is not null)
{
requestCount += 1;
RestRequest nextRequest = new RestRequest(nextUrl);
var nextResponse = await webRequestor.GetAsync<T>(nextRequest);
if (nextResponse.Data is not null)
returnData = returnData.Append(nextResponse.Data).ToArray();
var (nextData, nextResponse) = await webRequestor.GetAsync<T>(nextRequest);
if (nextData is not null)
returnData = returnData.Append(nextData).ToArray();
nextUrl = getNextUrl(nextResponse.Headers);
}

View File

@@ -2,6 +2,8 @@ using RestSharp;
public interface IWebRequestor
{
Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request);
Task<RestResponse<T>> GetAsync<T>(RestRequest request);
Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request);
Task<(T?, RestResponse)> GetAsync<T>(RestRequest request);
Task<RestResponse> PostAsync(RestRequest request);
Task<(T?, RestResponse)> PostAsync<T>(RestRequest request);
}

View File

@@ -13,16 +13,67 @@ public class WebRequestor : IWebRequestor
?? throw new Exception("CANVAS_TOKEN not in environment");
client = new RestClient(BaseUrl);
client.AddDefaultHeader("Authorization", $"Bearer {token}");
}
public async Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request)
public async Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request)
{
return await client.ExecuteGetAsync<T[]>(request);
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T[]>(response), response);
}
public async Task<RestResponse<T>> GetAsync<T>(RestRequest request)
public async Task<(T?, RestResponse)> GetAsync<T>(RestRequest request)
{
return await client.ExecuteGetAsync<T>(request);
var response = await client.ExecuteGetAsync(request);
return (Deserialize<T>(response), response);
}
public async Task<RestResponse> PostAsync(RestRequest request)
{
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");
throw new Exception("error with response");
}
return response;
}
public async Task<(T?, RestResponse)> PostAsync<T>(RestRequest request)
{
var response = await client.ExecutePostAsync(request);
return (Deserialize<T>(response), response);
}
public 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");
throw new Exception("error with response");
}
try
{
var data = JsonSerializer.Deserialize<T>(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 (JsonException ex)
{
System.Console.WriteLine(response.ResponseUri);
System.Console.WriteLine(response.Content);
Console.WriteLine($"An error occurred during deserialization: {ex.Message}");
throw ex;
}
}
}

View File

@@ -17,3 +17,11 @@ Authorization: Bearer {{$dotenv CANVAS_TOKEN}}
GET https://snow.instructure.com/api/v1/courses?enrollment_term_id=751&per_page=100
Authorization: Bearer {{$dotenv CANVAS_TOKEN}}
###
GET https://snow.instructure.com/api/v1/courses/855351/modules
Authorization: Bearer {{$dotenv CANVAS_TOKEN}}
###
GET https://snow.instructure.com/api/v1/courses/872095/modules
Authorization: Bearer {{$dotenv CANVAS_TOKEN}}