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

View File

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

View File

@@ -3,38 +3,29 @@
@code { @code {
[Parameter, EditorRequired] [Parameter, EditorRequired]
public int ModuleIndex { get; set; } public CourseModule Module { get; set; } = default!;
private bool showAddAssignment { get; set; } = false; 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> @* <div class="ms-5 ">
<button class="btn btn-primary" @onclick="() => showAddAssignment = true">Add Assignment</button>
@if (showAddAssignment)
{
<div class="ms-5 ">
<div class="bg-light border rounded m-3 p-3"> <div class="bg-light border rounded m-3 p-3">
<NewAssignment ModuleIndex="ModuleIndex" OnSubmit="() => showAddAssignment = false" /> <NewAssignment ModuleIndex="ModuleIndex" OnSubmit="() => showAddAssignment = false" />
</div> </div>
</div> </div> *@
}
<h5>Assignments</h5>
<div class="row">
@foreach (var a in module.Assignments)
{
<AssignmentCard assignment="a" />
}
</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" /> <NewModule OnSubmit="() => showNewModule = false" />
} }
@foreach (var i in configurationManagement.Modules.Select((_value, i) => i)) @foreach (var module in configurationManagement.Modules)
{ {
<hr> <hr>
<ModuleDetail ModuleIndex="i" /> <ModuleDetail Module="module" />
} }
<hr> <hr>

View File

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

View File

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

View File

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

View File

@@ -1,8 +1,8 @@
using CanvasModel.Enrollments; using CanvasModel.Enrollments;
namespace CanvasModel.Courses; namespace CanvasModel.Courses;
public record CourseModel
( public record CourseModel(
[property: JsonPropertyName("id")] ulong Id, [property: JsonPropertyName("id")] ulong Id,
[property: JsonPropertyName("sis_course_id")] string SisCourseId, [property: JsonPropertyName("sis_course_id")] string SisCourseId,
[property: JsonPropertyName("uuid")] string Uuid, [property: JsonPropertyName("uuid")] string Uuid,
@@ -33,21 +33,27 @@ public record CourseModel
[property: JsonPropertyName("needs_grading_count")] uint? NeedsGradingCount = null, [property: JsonPropertyName("needs_grading_count")] uint? NeedsGradingCount = null,
[property: JsonPropertyName("term")] TermModel? Term = null, [property: JsonPropertyName("term")] TermModel? Term = null,
[property: JsonPropertyName("course_progress")] CourseProgressModel? CourseProgress = null, [property: JsonPropertyName("course_progress")] CourseProgressModel? CourseProgress = null,
[property: JsonPropertyName("apply_assignment_group_weights")] bool? ApplyAssignmentGroupWeights = null, [property: JsonPropertyName("apply_assignment_group_weights")]
[property: JsonPropertyName("is_public")] bool? Is= null, bool? ApplyAssignmentGroupWeights = null,
[property: JsonPropertyName("is_public")] bool? Is = null,
[property: JsonPropertyName("is_public_to_auth_users")] bool? IsPublicToAuthUsers = null, [property: JsonPropertyName("is_public_to_auth_users")] bool? IsPublicToAuthUsers = null,
[property: JsonPropertyName("public_syllabus")] bool? PublicSyllabus = null, [property: JsonPropertyName("public_syllabus")] bool? PublicSyllabus = null,
[property: JsonPropertyName("public_syllabus_to_auth")] bool? PublicSyllabusToAuth = null, [property: JsonPropertyName("public_syllabus_to_auth")] bool? PublicSyllabusToAuth = null,
[property: JsonPropertyName("public_description")] string? PublicDescription = null, [property: JsonPropertyName("public_description")] string? PublicDescription = null,
[property: JsonPropertyName("hide_final_grades")] bool? HideFinalGrades = 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_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("open_enrollment")] bool? OpenEnrollment = null,
[property: JsonPropertyName("self_enrollment")] bool? SelfEnrollment = 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("access_restricted_by_date")] bool? AccessRestrictedByDate = null,
[property: JsonPropertyName("blueprint")] bool? Blueprint = null, [property: JsonPropertyName("blueprint")] bool? Blueprint = null,
[property: JsonPropertyName("blueprint_restrictions")] Dictionary<string, bool>? BlueprintRestrictions = null, [property: JsonPropertyName("blueprint_restrictions")]
[property: JsonPropertyName("blueprint_restrictions_by_object_type")] Dictionary<string, Dictionary<string, bool>>? BlueprintRestrictionsByObjectType = null 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; using System.ComponentModel.DataAnnotations;
namespace CanvasModel;
public record CourseModule( public record CourseModule(
[property: Required] [property: JsonPropertyName("id")] ulong Id,
[property: StringLength(50, ErrorMessage = "Name too long (50 character limit).")] [property: JsonPropertyName("name")] string Name
string Name, // [property: JsonPropertyName("start_at")] DateTime StartAt,
IEnumerable<LocalAssignment>? Assignments = null // [property: JsonPropertyName("end_at")] DateTime EndAt,
) // [property: JsonPropertyName("description")] string Description
{ );
[JsonInclude]
public IEnumerable<LocalAssignment> Assignments = Assignments ?? new LocalAssignment[] { };
}

View File

@@ -1,3 +1,4 @@
using CanvasModel;
using CanvasModel.Courses; using CanvasModel.Courses;
using CanvasModel.EnrollmentTerms; using CanvasModel.EnrollmentTerms;
using RestSharp; using RestSharp;
@@ -40,35 +41,59 @@ public class CanvasService : ICanvasService
public async Task<CourseModel> GetCourse(ulong courseId) public async Task<CourseModel> GetCourse(ulong courseId)
{ {
var url = $"course/${courseId}"; var url = $"course/{courseId}";
var request = new RestRequest(url); 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.Content);
System.Console.WriteLine(response.ResponseUri); System.Console.WriteLine(response.ResponseUri);
throw new Exception("error getting course from canvas"); 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) private async Task<IEnumerable<T>> PaginatedRequest<T>(RestRequest request)
{ {
var requestCount = 1; var requestCount = 1;
request.AddQueryParameter("per_page", "100"); 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); var nextUrl = getNextUrl(response.Headers);
while (nextUrl is not null) while (nextUrl is not null)
{ {
requestCount += 1; requestCount += 1;
RestRequest nextRequest = new RestRequest(nextUrl); RestRequest nextRequest = new RestRequest(nextUrl);
var nextResponse = await webRequestor.GetAsync<T>(nextRequest); var (nextData, nextResponse) = await webRequestor.GetAsync<T>(nextRequest);
if (nextResponse.Data is not null) if (nextData is not null)
returnData = returnData.Append(nextResponse.Data).ToArray(); returnData = returnData.Append(nextData).ToArray();
nextUrl = getNextUrl(nextResponse.Headers); nextUrl = getNextUrl(nextResponse.Headers);
} }

View File

@@ -2,6 +2,8 @@ using RestSharp;
public interface IWebRequestor public interface IWebRequestor
{ {
Task<RestResponse<T[]>> GetManyAsync<T>(RestRequest request); Task<(T[]?, RestResponse)> GetManyAsync<T>(RestRequest request);
Task<RestResponse<T>> GetAsync<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"); ?? throw new Exception("CANVAS_TOKEN not in environment");
client = new RestClient(BaseUrl); client = new RestClient(BaseUrl);
client.AddDefaultHeader("Authorization", $"Bearer {token}"); 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 GET https://snow.instructure.com/api/v1/courses?enrollment_term_id=751&per_page=100
Authorization: Bearer {{$dotenv CANVAS_TOKEN}} 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}}